C# 指示泛型返回动态类型的对象

C# 指示泛型返回动态类型的对象,c#,generics,reflection,.net-3.5,C#,Generics,Reflection,.net 3.5,这个问题是我最初问题的后续问题 假设我有以下泛型类(简化!^ ^): class CasterClass其中T:class { 公共类(){/*无*/} 公共T型铸造(对象obj) { 返回(obj作为T); } } 具有将对象强制转换为指定类型的功能 不幸的是,在编译时,我无法准确地知道我必须使用哪些类型,因此我必须通过反射实例化这个类,如下所示: Type t = typeof(castedObject); // creating the reflected Caster object

这个问题是我最初问题的后续问题

假设我有以下泛型类(简化!^ ^):

class CasterClass其中T:class
{
公共类(){/*无*/}
公共T型铸造(对象obj)
{
返回(obj作为T);
}
}
具有将对象强制转换为指定类型的功能

不幸的是,在编译时,我无法准确地知道我必须使用哪些类型,因此我必须通过反射实例化这个类,如下所示:

Type t = typeof(castedObject);

// creating the reflected Caster object
object CasterObj = Activator.CreateInstance(
    typeof(CasterClass<>).MakeGenericType(t)
);

// creating a reflection of the CasterClass' Cast method
MethodInfo mi = typeof(CasterClass<>).GetMethod("Cast");
Type t=typeof(castedObject);
//创建反射的Caster对象
对象CasterObj=Activator.CreateInstance(
typeof(CasterClass).MakeGenericType(t)
);
//创建CasterClass的Cast方法的反射
MethodInfo mi=typeof(CasterClass).GetMethod(“Cast”);
问题是,一旦我使用mi.Invoke()调用该方法,它将返回一个对象类型的输出,而不是特定类型的T实例(因为反射)

有没有办法让通过反射调用的方法返回动态类型,如上图所示?我非常确定.NET3.5没有将其转换为动态类型的功能(或者更确切地说,这是非常不切实际的)


非常感谢

如果您可以控制要使用的类,请让它们都实例化一个包含要调用的方法的接口,并在实例化后转换到接口


我也用同样的想法回答了您之前的问题。

只要将任何类型传递给ObjectCreateMethod,您就会得到一个动态方法处理程序,您可以稍后使用该处理程序调用CreateInstance将泛型对象强制转换为特定类型

ObjectCreateMethod _MyDynamicMethod = new ObjectCreateMethod(info.PropertyType);
object _MyNewEntity = _MyDynamicMethod.CreateInstance();
将此类称为:

using System.Reflection;
using System.Reflection.Emit;

public class ObjectCreateMethod
{
    delegate object MethodInvoker();
    MethodInvoker methodHandler = null;

    public ObjectCreateMethod(Type type)
    {
        CreateMethod(type.GetConstructor(Type.EmptyTypes));
    }

    public ObjectCreateMethod(ConstructorInfo target)
    {
        CreateMethod(target);
    }

    void CreateMethod(ConstructorInfo target)
    {
        DynamicMethod dynamic = new DynamicMethod(string.Empty,typeof(object),new Type[0], target.DeclaringType);
        ILGenerator il = dynamic.GetILGenerator();
        il.DeclareLocal(target.DeclaringType);
        il.Emit(OpCodes.Newobj, target);
        il.Emit(OpCodes.Stloc_0);
        il.Emit(OpCodes.Ldloc_0);
        il.Emit(OpCodes.Ret);

        methodHandler = (MethodInvoker)dynamic.CreateDelegate(typeof(MethodInvoker));
    }

    public object CreateInstance()
    {
        return methodHandler();
    }
}

谢谢马克,也许就是这样。不过,我还得再仔细研究一下,不过这很有道理。
using System.Reflection;
using System.Reflection.Emit;

public class ObjectCreateMethod
{
    delegate object MethodInvoker();
    MethodInvoker methodHandler = null;

    public ObjectCreateMethod(Type type)
    {
        CreateMethod(type.GetConstructor(Type.EmptyTypes));
    }

    public ObjectCreateMethod(ConstructorInfo target)
    {
        CreateMethod(target);
    }

    void CreateMethod(ConstructorInfo target)
    {
        DynamicMethod dynamic = new DynamicMethod(string.Empty,typeof(object),new Type[0], target.DeclaringType);
        ILGenerator il = dynamic.GetILGenerator();
        il.DeclareLocal(target.DeclaringType);
        il.Emit(OpCodes.Newobj, target);
        il.Emit(OpCodes.Stloc_0);
        il.Emit(OpCodes.Ldloc_0);
        il.Emit(OpCodes.Ret);

        methodHandler = (MethodInvoker)dynamic.CreateDelegate(typeof(MethodInvoker));
    }

    public object CreateInstance()
    {
        return methodHandler();
    }
}