On 4/1/09, Philip_L <[email protected]> wrote:
>
>  I'm trying to emit a class that dynamically overrides the following
>  class signature:
>
>     public class ClassWithGenericTypeDefinitionReturnType
>     {
>         public virtual List<T> DoSomething<T>()
>         {
>             return new List<T>();
>         }
>     }
>
>  I don't know why, but the problem is that I can't seem to import the
>  List<T> type as a type reference..

Ah I see. It has nothing to do with List<>, but with List<T>, when T
is defined on a method.

To begin with, I've fixed to throw a proper exception instead of a NRE
in this case. Now to fix it.

The issue is that the importer, when importing this List<T>, stumbles
upon the T, it will try to import it according to a context, and will
try to get T in that context. In that case, as T is defined on the
method, it tries to get it from the current method in the context.

So, based on the code in your first mail, you should do:

public static void SetReturnType(this MethodDefinition method, Type
returnType)
       {
           var declaringType = method.DeclaringType;
           ModuleDefinition module = declaringType.Module;

           var returnTypeRef = module.Import(returnType, method); //
pass method as the context.

You would need to have a proper T defined in the GenericParameters
collection of the `method`.

And I have to implement the overload of import that would take a
MethodDefinition as a context. I'll have a look at that this
afternoon.

-- 
Jb Evain  <[email protected]>

--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---

Reply via email to