Casting objects returned by reflection to a generic object
-
The story behind this question is simple: I'm trying to build a serializer that serializes a generic class into a custom format. public class NGConfigUnit { protected T _value; ......... ....... public T Value { get { return _value; } set { _value = value; _isDirty = true; } } } Problem is that when I access the class via reflection the values are returned as object. Even though I can check the value of T using reflection and generate an instance using a factory, I cannot reference a generic instance dynamically i.e. I cannot write something like (no support for this C#): NGConfigUnit<typeof(myval> unit = ..... This blocks me from accessing fields and properties explicitly, which is crucial for the rest of my code. Any idea how to reach am alternative? Thanks, Omer
-
The story behind this question is simple: I'm trying to build a serializer that serializes a generic class into a custom format. public class NGConfigUnit { protected T _value; ......... ....... public T Value { get { return _value; } set { _value = value; _isDirty = true; } } } Problem is that when I access the class via reflection the values are returned as object. Even though I can check the value of T using reflection and generate an instance using a factory, I cannot reference a generic instance dynamically i.e. I cannot write something like (no support for this C#): NGConfigUnit<typeof(myval> unit = ..... This blocks me from accessing fields and properties explicitly, which is crucial for the rest of my code. Any idea how to reach am alternative? Thanks, Omer
-
You are probably getting a version mismatch. Try running the scenario WITHOUT rebuilding you project.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))I'm afraid my question was not understood. The generic parameter type to be returned by reflection is known only at runtime, so I cannot write in my code something like: NGConfigUnit unit = fieldInfo.GetValue(obj, null) ..... - I simply don't know if the value returned shall be NGConfigUnit or NGConfigUnit or whatever ... So what I need is a way to invoke NGConfigUnit methods not using reflection and w/o knowing what T is in advance. I doubt if there's such a way, but suggestions are welcomed. Thanks, Omer