Call Generic method using runtime type and cast return object?

The type IS determined at runtime. It's the type of the reference variable that is object, the actual instance is strongly typed That's the best that can be done, considering that you're using reflection to dynamically get access to a method that the compiler has no type information for -- it might not even exist in the build environment EDIT: If you know something about the type returned by Deserialize then you can take advantage of delegate variance. For example: Type tType = Type.

GetType(pLoadOut. Type); MethodInfo method = typeof(ApiSerialiseHelper). GetMethod("Deserialise", new Type { typeof(string) }); MethodInfo generic = method.

MakeGenericMethod(tType); Converter deser = (Converter)Delegate. CreateDelegate(typeof(Converter),generic); ISomething result = deser(pLoadOut. Data).

The type IS determined at runtime. It's the type of the reference variable that is object, the actual instance is strongly typed. That's the best that can be done, considering that you're using reflection to dynamically get access to a method that the compiler has no type information for -- it might not even exist in the build environment.

EDIT: If you know something about the type returned by Deserialize, then you can take advantage of delegate variance. For example: Type tType = Type. GetType(pLoadOut.

Type); MethodInfo method = typeof(ApiSerialiseHelper). GetMethod("Deserialise", new Type { typeof(string) }); MethodInfo generic = method. MakeGenericMethod(tType); Converter deser = (Converter)Delegate.

CreateDelegate(typeof(Converter),generic); ISomething result = deser(pLoadOut. Data).

If the returned objects have a common ancestor or interface, then you can cast to them. If they don't, then they should, e.g. If all possible return types have a method named DoSomething(), then make an interface that has a method DoSomething().

And that interface must be known at compile-time, even if the most derived type is not. – Ben Voigt Dec 31 '10 at 14:25.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions