Is it possible to use generics with the IL Generator?
DynamicMethod method = new DynamicMethod(
"GetStuff", typeof(int), new Type[] { typeof(object) });
ILGenerator il = method.GetILGenerator();
... etc
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, it is possible, but not with the
DynamicMethodclass. If you are restricted to using this class, you’re out of luck. If you can instead use aMethodBuilderobject, read on.Emitting the body of a generic method is, for most intents and purposes, no different from emitting the body of other methods, except that you can make local variables of the generic types. Here is an example of creating a generic method using
MethodBuilderwith the generic argument T and creating a local of type T:To emit a call to that generic method from another method, use this code. Assuming
methodis aMethodInfoorMethodBuilderobject that describes a generic method definition, you can emit a call to that method with the single generic parameterintas follows: