Thursday, September 9, 2010

cast to generic type at runtime

Programmer Question

Ok, i don't think this is possible so i thought i would ask to make sure. I'm in the process of creating a function reflects over the properties in a class and adds them to this data structure i have. Some of the properties are generic types.



So say we have DataType(of T) that as a .Value property of type T:



Dim properties = GetType(GenFormMaintenancePOCO).GetFields(Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance)
For Each prop As fieldinfo In properties
Collection.Add(prop.Name,prop.GetValue(poco))
Next


In The collection.Add for primitive types(int,string,ect...) i just want to add the type...but in the case of generic i want to add the DataType(Of T).Value. I hope there is some work around but i don't think there is a way because the type of T can not be determined at compile time right? Ideally DirectCast(prop.getvalue(poco),DataType(Of T)).Value would be possible.



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails