Casting generic type instances created using Reflection

后端 未结 1 453
执笔经年
执笔经年 2020-12-31 20:16

I\'m creating instances of a generic type using reflection:

public interface IModelBuilder
{
    TModel BuildModel();
}

public class MyModel
{         


        
相关标签:
1条回答
  • 2020-12-31 21:03

    Since modelType is just a Type instance, you can't do that automatically, since there is no non-generic API available. Various options:

    1: use reflection, for example (untested)

    object builder = Activator.CreateInstance(...);
    var model=builder.GetType().GetMethod("BuildModel").Invoke(builder,null);
    

    2: cheat with dynamic:

    dynamic builder = Activator.CreateInstance(...);
    var model = builder.BuildModel();
    

    3: make a non-generic version of IModelBuilder, and use that

    Note that 1 & 2 rely on a public implementation of the interface, and will fail for a (perfectly legal) explicit interface implementation. For "1", you can fix this via:

    var model = typeof(IModelBuilder<>).MakeGenericType(modelType)
           .GetMethod("BuildModel").Invoke(builder);
    

    A final sneaky option is to flip from a non-generic method into a generic method, so inside the generic method you can use all the members directly. There's a lazy way to do that via dynamic:

    interface ISneaky<T>
    {
        T Foo { get; }
    }
    class Sneaky<T> : ISneaky<T>
    {
        T ISneaky<T>.Foo { get { return default(T); } }
    }
    class Program
    {
        static void Main()
        {
            Execute(typeof(int));
        }
        static void Execute(Type t)
        {
            dynamic obj = Activator.CreateInstance(
                typeof(Sneaky<>).MakeGenericType(t));
            // crafy hack to flip from non-generic code into generic code:
            Evil(obj);
        }
        static void Evil<T>(ISneaky<T> sneaky)
        {   // in here, life is simple; no more reflection
            Console.WriteLine("{0}: {1}", typeof(T).Name, sneaky.Foo);
        }
    }
    
    0 讨论(0)
提交回复
热议问题