When importing the field type, did you provide its generic context?

Am 13.11.2010 um 04:39 schrieb Timwi <[email protected]>:

> Hi,
>
> I tried to create a minimalist testcase for this, here goes:
>
> In a class library, I have a type like this:
>
>    public class BaseClass<TKey, TValue> where TKey : struct where
> TValue : struct
>    {
>        protected Dictionary<TKey, TValue> staticField = new
> Dictionary<TKey, TValue> { { default(TKey), default(TValue) } };
>    }
>
> And now, in a separate assembly (console application), I have the
> following code:
>
>    public class DerivedClass : BaseClass<int, int>
>    {
>        public Dictionary<int, int> GetDic() { return staticField; }
>    }
>    static class Program
>    {
>        static void Main()
>        {
>            var asm =
> AssemblyDefinition.ReadAssembly(Assembly.GetExecutingAssembly().Location);
>            var type = asm.MainModule.Types.Single(t => t.Name ==
> "DerivedClass");
>            var method = type.Methods.Single(m => m.Name == "GetDic");
>            var fieldResolved = ((FieldReference)
> method.Body.Instructions.Single(i => i.Operand is
> FieldReference).Operand).Resolve();
>            var imported =
> asm.MainModule.Import(fieldResolved.FieldType);
>        }
>    }
>
> The last line fails with an InvalidOperationException.
>
> However, if I import the whole field:
>
>    var imported = asm.MainModule.Import(fieldResolved);
>
> and then look at its FieldType, it looks correct — it is the
> GenericInstanceType that I expect, with the GenericArguments set to
> the GenericParameters of the BaseClass type, all correctly imported to
> the other module. Why doesn’t Import() work with the original
> FieldType in the same way?
>
> --
> --
> mono-cecil

-- 
--
mono-cecil

Reply via email to