|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
using Generics dynamically (?)This seems to be a bit of a contradiction, but how can I construct a
Generic class using a System.Type variable that is assigned at run time? Is there some parallel to the Generics concept that extends to having strictly-typed classes at run-time? I would gladly give up the compile-time errors if I could get rid of all these CType()s :) "Gazarsgo" <gazar***@gmail.com> schrieb: Maybe you are looking for 'Type.MakeGenericType' and > This seems to be a bit of a contradiction, but how can I construct a > Generic class using a System.Type variable that is assigned at run > time? 'Activator.CreateInstance'? > Is there some parallel to the Generics concept that extends to Strict typing is a compile-time feature.> having strictly-typed classes at run-time? -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Gazarsgo,
In addition to the other comments. | Is there some parallel to the Generics concept that extends to That is the major point behind Generics. You have strictly-typed classes at | having strictly-typed classes at run-time? compile & runtime For example: ' create a loosely typed collection of integers in .NET 1.x Dim aList As New ArrayList aList.Add(1) aList.Add(2) aList.Add("Silly Me") ' allowed as its loosely typed! ' create a strictly-typed collection of integers in .NET 2.0 Dim aList As New List(Of Integer) aList.Add(1) aList.Add(2) aList.Add("Silly Me") ' Compile error as its strictly typed! | This seems to be a bit of a contradiction, but how can I construct a You should be able to use Type.MakeGenericType & Activator.CreateInstance,| Generic class using a System.Type variable that is assigned at run | time? something like: Dim aType As Type = GetType(String) Dim theCollectionType As Type = GetType(System.Collections.ObjectModel.Collection(Of )) Dim myColType As Type = theCollectionType.MakeGenericType(aType) Dim mycol As Object = Activator.CreateInstance(myColType) HOWEVER: Without knowing the actual type at compile time, you will only be able to use late binding, Reflection, & object variables with the created type, plus possibly a handful of interfaces. | I would gladly give up the compile-time errors if I could get rid of ?? CType doesn't help with have a variable of System.Type per se. However | all these CType()s :) Generics should reduce or elimate the "need" for CType. FWIW: This is a good FAQ on Generics: http://msdn.microsoft.com/vstudio/default.aspx?pull=/library/en-us/dndotnet/html/Fundamentals.asp -- Show quoteHide quoteHope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Gazarsgo" <gazar***@gmail.com> wrote in message news:1138205218.213763.113880@o13g2000cwo.googlegroups.com... | This seems to be a bit of a contradiction, but how can I construct a | Generic class using a System.Type variable that is assigned at run | time? Is there some parallel to the Generics concept that extends to | having strictly-typed classes at run-time? | | I would gladly give up the compile-time errors if I could get rid of | all these CType()s :) | Gazargsgo,
Never tried and never will, however are you not just looking at "object". Cor Cor Ligthert [MVP] wrote:
> Never tried and never will, however are you not just looking at "object". I am trying to implement an alternative to Object references usingGenerics, so as to have my class functions return the specific type rather than an Object reference, and to avoid boxing where possible. Herfried K. Wagner [MVP] wrote: > Strict typing is a compile-time feature. Maybe this is my own ignorance, but by "strict typing" I meant to referto type safety, whether implemented via static typing at compile time or dynamic typing at runtime. It looks like Type.MakeGenericType and Activator.CreateInstance are exactly what I was looking for, thanks for the extended code snippet Jay.
date/time fields
Conditional statements SystemIndexOutOfRangeException error CheckForIllegalCrossThreadCalls ? File upload to website and rezise at the same time ADO.NET 2.0 TableAdapter Configuration Wizard Strange maximize behaviour Queue of Threads timing accuracy of VB.Net deleting files greater than x days |
|||||||||||||||||||||||