Sure!
In the Cecil generics issue sample I sent you earlier, paste the
following:

            var assembly =
AssemblyDefinition.ReadAssembly(@"MyAssembly.dll", new
ReaderParameters() { ReadSymbols = true });
            var type = assembly.MainModule.Types.FirstOrDefault(t =>
"A`1".Equals(t.Name));
            var originalPropertyType =
typeof(MyAssembly.A<>).GetProperty("L").PropertyType;
            // works all right with additional context param
 
type.Module.Import(typeof(List<>).MakeGenericType(originalPropertyType),
type);
            // fails with no context param
 
type.Module.Import(typeof(List<>).MakeGenericType(originalPropertyType).GetConstructor(new
Type[0]));

The code in "MyAssembly" may be simplified as follows:

namespace MyAssembly
{
    public class A<T>
    {
        public T L { get { return default(T); } }
    }
}

The second Import statement in the above snippet fails trying to
import the MethodBase / ConstructorInfo
(my mistake, I wrote "MethodReference" in my original post)

Cheers,
Gabriel

On 1 juin, 14:23, Jb Evain <[email protected]> wrote:
> Hey Gabriel,
>
> On Tue, Jun 1, 2010 at 12:33 PM, Gabriel Kevorkian
>
> <[email protected]> wrote:
> > Sorry for bothering you again but I think it would be great if the
> > support for specifying a generic context were actually extended to the
> > overload of ModuleDefinition.Import() taking in a MethodReference as
> > argument.
>
> When importing a MethodReference, its DeclaringType becomes the
> context, so am not exactly sure.
>
> > using .net reflection on a generic type e.g.
>
> > public class A<TParam>
> > {
> >   public TParam L { get { return null; } }
> > }
>
> > to:
> > - retrieve a type defined in terms of a generic parameter,
> > - use the thus retrieved type to instantiate another generic type, say
> > List<> ,
> > - get the constructor so as to be able to perform a new of
> > List<TParam> within A<TParam>
>
> Could you post code for those usages, it would be much easier to understand.
>
> Thanks!
>
> --
> Jb Evain  <[email protected]>

-- 
--
mono-cecil

Reply via email to