C# 如何将类转换为动态父接口?

C# 如何将类转换为动态父接口?,c#,reflection,C#,Reflection,我有一个通用接口和一些相应的代码(以下所有代码都在ClassLibrary1名称空间中): 所以我有几个重载版本的process方法 我编写了一个dispatcher类: public partial class Dispatcher { private const string NS_BASE = "ClassLibrary1"; private static Processor processor = new Processor(); static partial v

我有一个通用接口和一些相应的代码(以下所有代码都在
ClassLibrary1
名称空间中):

所以我有几个重载版本的
process
方法

我编写了一个
dispatcher
类:

public partial class Dispatcher
{
    private const string NS_BASE = "ClassLibrary1";
    private static Processor processor = new Processor();
    static partial void Dispatch(MessageType msgType, Object message);
    static partial void Dispatch_cached(MessageType msgType, Object message);
    // basic declaration above, implemntation follows
    // message parameter corresponds with msgType.The method signature is compulsory and can't be modified()
}
我首先使用了直接反射:

public partial class Dispatcher
{    
    static partial void Dispatch(MessageType msgType,Object message)
    {
        Type messageType = Type.GetType(NS_BASE + "." + msgType.ToString());
        Type processorType = typeof(Processor);
        Type[] typeArr = new Type[] { messageType };
        Object[] msgArr = new Object[] { message };
        MethodInfo processMethod = processorType.GetMethod("process", typeArr);
        processMethod.Invoke(processor, msgArr);
    }
}
并使用缓存提高性能:

public partial class Dispatcher
{    
    private static Dictionary<String, MethodInfo> typeDictionary = initDict();

    private static Dictionary<String, MethodInfo> initDict()
    {
        string[] names = Enum.GetNames(typeof(MessageType));
        return names.ToDictionary(msgTypeStr => msgTypeStr, msgTypeStr =>
        {
            Type messageType = Type.GetType(NS_BASE + "." + msgTypeStr.ToString());
            Type processorType = typeof(Processor);
            Type[] typeArr = new Type[] { messageType };
            return processorType.GetMethod("process", typeArr);
        });
    }

    static partial void Dispatch_cached(MessageType msgType, Object message)
    {
        if (typeDictionary.TryGetValue(msgType.ToString(), out MethodInfo processorMethod))
        {
            Object[] msgArr = new Object[] { message };
            processorMethod.Invoke(processor, msgArr);
        }
    }
}
公共部分类调度程序
{    
私有静态字典typeDictionary=initDict();
私有静态字典initDict()
{
string[]name=Enum.GetNames(typeof(MessageType));
返回names.ToDictionary(msgTypeStr=>msgTypeStr,msgTypeStr=>
{
Type messageType=Type.GetType(NS_BASE+“+msgTypeStr.ToString());
类型处理器类型=类型(处理器);
类型[]typeArr=新类型[]{messageType};
返回processorType.GetMethod(“process”,typeArr);
});
}
静态部分无效调度缓存(MessageType msgType,对象消息)
{
if(typeDictionary.TryGetValue(msgType.ToString(),out MethodInfo processorMethod))
{
Object[]msgArr=新对象[]{message};
调用(processor,msgArr);
}
}
}
根据我的测试,性能实际上提高了5~6倍,但我也看到了可能的反射选项,如表达式、emit或DLR那么这些替代解决方案是否适合现场,它们是否会提高性能?

这些是我实际面临的问题。“如何将类转换为动态父接口?”只是我在尝试使用DLR解决它时提出的一个问题

static void Dispatch_dynamic(MessageType msgType, dynamic message)
{
    processor.process(message);// I think this line will raise a runtime error
    //can I convert processor to IFoo<First/Second> type and call the processor_converted.process(message)?
}
static void Dispatch\u dynamic(MessageType msgType,dynamic message)
{
processor.process(message);//我认为此行将引发运行时错误
//我可以将处理器转换为IFoo类型并调用处理器\u converted.process(消息)吗?
}

你想要达到的目标还不清楚。您不会更改对象本身的类型-通常您强制转换为使用您在编译时知道的类型的特定成员集。如果你能提供更多关于你想做什么的信息——理想情况下是作为一名教师——我们将能够为你提供更多帮助updated@JonSkeet问题更新我可以想出两种方法来实现这一点,但我不想自己去测试它们,当你觉得这样做更合适的时候。现在的问题不是伪代码(
do
是一个关键字,无论如何都不是惯用的方法名),而是一个完整的示例。根据我的经验,要快速调用动态方法,必须知道签名。执行此操作最慢的方法之一是
Delegate.DynamicInvoke
,这是因为签名未知。最快的方法之一是使用
Delegate.CreateDelegate
返回强类型委托。这些速度非常非常快。但是,如果这不是性能关键的代码,这可能并不重要
MethodInfo.Invoke
,这是您正在使用的,可能已经足够快了。现在还不清楚您想要实现什么。您不会更改对象本身的类型-通常您强制转换为使用您在编译时知道的类型的特定成员集。如果你能提供更多关于你想做什么的信息——理想情况下是作为一名教师——我们将能够为你提供更多帮助updated@JonSkeet问题更新我可以想出两种方法来实现这一点,但我不想自己去测试它们,当你觉得这样做更合适的时候。现在的问题不是伪代码(
do
是一个关键字,无论如何都不是惯用的方法名),而是一个完整的示例。根据我的经验,要快速调用动态方法,必须知道签名。执行此操作最慢的方法之一是
Delegate.DynamicInvoke
,这是因为签名未知。最快的方法之一是使用
Delegate.CreateDelegate
返回强类型委托。这些速度非常非常快。但是,如果这不是性能关键的代码,这可能并不重要
MethodInfo.Invoke
,这是您正在使用的,可能已经足够快了。
public partial class Dispatcher
{    
    private static Dictionary<String, MethodInfo> typeDictionary = initDict();

    private static Dictionary<String, MethodInfo> initDict()
    {
        string[] names = Enum.GetNames(typeof(MessageType));
        return names.ToDictionary(msgTypeStr => msgTypeStr, msgTypeStr =>
        {
            Type messageType = Type.GetType(NS_BASE + "." + msgTypeStr.ToString());
            Type processorType = typeof(Processor);
            Type[] typeArr = new Type[] { messageType };
            return processorType.GetMethod("process", typeArr);
        });
    }

    static partial void Dispatch_cached(MessageType msgType, Object message)
    {
        if (typeDictionary.TryGetValue(msgType.ToString(), out MethodInfo processorMethod))
        {
            Object[] msgArr = new Object[] { message };
            processorMethod.Invoke(processor, msgArr);
        }
    }
}
static void Dispatch_dynamic(MessageType msgType, dynamic message)
{
    processor.process(message);// I think this line will raise a runtime error
    //can I convert processor to IFoo<First/Second> type and call the processor_converted.process(message)?
}