I haven't run the unit tests (I don't have nunit set up on my box)
All I know is this breaks in my specific case with the following
example:

public class A<T>
{
}

var assembly = AssemblyDefinition.ReadAssembly
(
    @"MyAssembly.dll",
    new ReaderParameters() { ReadSymbols = true }
);
var type = assembly.MainModule.Types.FirstOrDefault(t =>
"A`1".Equals(t.Name));
var list_of_a_Add = type.Module.Import
(
    typeof(List<>)
        .MakeGenericType(typeof(MyAssembly.A<>))
        .GetMethod("Add"),
    type
);
Console.WriteLine(list_of_a_Add.FullName);

Basically I'm attempting to get hold of method Add() on type
List<A<T>>
With the original implementation, I get

    System.Void System.Collections.Generic.List`1::Add(T)

as output where I would like to have:

    System.Void
System.Collections.Generic.List`1<MyAssembly.A`1<T>>::Add(T)

instead (which is what is returned if I reuse declaring_type when
assigning DeclaringType and have generic instance support in
ImportTypeSpecification patched as follows:

TypeReference ImportTypeSpecification (Type type, IGenericContext
context)
{
        if (type.IsByRef)
                return new ByReferenceType (ImportType (type.GetElementType (),
context));

        if (type.IsPointer)
                return new PointerType (ImportType (type.GetElementType (),
context));

        if (type.IsArray) {
                var array_type = new ArrayType (ImportType (type.GetElementType 
(),
context));
                for (int i = 1; i < type.GetArrayRank (); i++)
                        array_type.Dimensions.Add (new ArrayDimension ());

                return array_type;
        }

        if (IsGenericInstance (type)) {
                return ImportGenericInstance(type, context);
        }

        if (type.IsGenericParameter) {
                if (context == null)
                        throw new InvalidOperationException ();

                var owner = type.DeclaringMethod != null
                        ? context.Method
                        : context.Type;

                if (owner == null)
                        throw new InvalidOperationException ();

                return owner.GenericParameters [type.GenericParameterPosition];
        }

        throw new NotSupportedException (type.FullName);
}

private TypeReference ImportGenericInstance(Type type, IGenericContext
context)
{
    var element_type = ImportType(type.GetGenericTypeDefinition(),
context);
    var instance = new GenericInstanceType(element_type);
    var arguments = type.GetGenericArguments();
    for (int i = 0; i < arguments.Length; i++)
    {
        TypeReference argument_type;
        if (arguments[i].IsGenericTypeDefinition)
            argument_type = ImportGenericInstance(arguments[i],
context);
        else
            argument_type = ImportType(arguments[i], context);

        instance.GenericArguments.Add(argument_type);
    }
    return instance;
}

I may be doing something wrong but I cannot seem to find any other
suitable workaround to get my code to work.

Your help is greatly appreciated

Gabriel


On 4 juin, 14:34, Jb Evain <[email protected]> wrote:
> On Fri, Jun 4, 2010 at 12:47 PM, Gabriel Kevorkian
>
> <[email protected]> wrote:
> > is not correct.
>
> It is. You can run the unit test to verify it.
>
> --
> Jb Evain  <[email protected]>

-- 
--
mono-cecil

Reply via email to