How to turn a Type instance into a generic type argument?

If ty is known at compile-time, why don't just void Foo() { var result = serializer. Deserialize(inputContext); } Otherwise MethodInfo genericDeserializeMethod = serializer.GetType(). GetMethod("Deserialize"); MethodInfo closedDeserializeMethod = genericDeserializeMethod.

MakeGenericMethod(ty); closedDeserializeMethod. Invoke(serializer, new object { inputContext }).

If ty is known at compile-time, why don't just void Foo() { var result = serializer. Deserialize(inputContext); } Otherwise, MethodInfo genericDeserializeMethod = serializer.GetType(). GetMethod("Deserialize"); MethodInfo closedDeserializeMethod = genericDeserializeMethod.

MakeGenericMethod(ty); closedDeserializeMethod. Invoke(serializer, new object { inputContext }).

If you only know the Type at runtime (not compile time), and it doesn't have a non-generic API, then you might have to use MakeGenericMethod: void Foo(Type ty) { object result = typeof(ContainingClass). GetMethod("Bar").. MakeGenericMethod(ty). Invoke(null, new object {inputContent}); } public static T Bar(SomeType inputContent) { return serializer.

Deserialize(inputContent); }.

– aleemb May 13 '09 at 8:14 ContainingClass is the class that has the Bar method, and "Bar" is used to find the Bar method by name. You could also (per Anton's answer) go straight to the "Serialize" method on the serializer. – Marc Gravell?

May 13 '09 at 8:28 Got it now. This works: var result = typeof(JavaScriptSerializer). GetMethod("Deserialize") .

MakeGenericMethod(JsonDataType). Invoke(serializer, new object { inputContent }); calling Invoke(null, ...) throws a TargetException ("non-static method required") – aleemb May 13 '09 at 8:43.

Use void Foo(){ var result = serializer. Deserialize(inputContent); } With the following call Foo().

In this case, just do this: void Foo() { var result = serializer. Deserialize(inputContent); } Foo(); Otherwise, you need to call the generic method late-bound, since you have to get the correct generic method for it first (it is not known at compile time). Have a look at the MethodInfo.

MakeGenericMethod method.

Like Lucero said, void Foo() { var result = serializer. Deserialize(inputContent); } Foo(); typeof(Person) is not the same thing as Person. Person is a compile-time type, whereas typeof(Person) is an expression that returns a Type instance representing the runtime type information of Person.

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