C# 在AppDomain中创建委托

C# 在AppDomain中创建委托,c#,reflection,delegates,c++-cli,appdomain,C#,Reflection,Delegates,C++ Cli,Appdomain,如何使用反射在应用程序域中创建委托的实例 我有一个C++/CLI DLL,可以从中动态加载C#DLL。由于C#DLL位于网络共享上,因此我将其加载到单独的AppDomain中(其中PermissionSet是PermissionState.Unrestricted) 这在创建类时效果很好: AppDomain^ appDomain = AppDomain::CreateDomain( ... ); Object^ obj = appDomain->CreateInstanceAndUnwr

如何使用反射在应用程序域中创建委托的实例

我有一个C++/CLI DLL,可以从中动态加载C#DLL。由于C#DLL位于网络共享上,因此我将其加载到单独的AppDomain中(其中PermissionSet是PermissionState.Unrestricted)

这在创建类时效果很好:

AppDomain^ appDomain = AppDomain::CreateDomain( ... );
Object^ obj = appDomain->CreateInstanceAndUnwrap(
    assemblyName,
    "MyNamespace.MyClass",
    false, // ignoreCase
    BindingFlags::CreateInstance | BindingFlags::Public | BindingFlags::Instance,
    nullptr, // binder
    args, // constructor arguments
    nullptr, // culture
    nullptr); // activationAttributes
但是,以相同方式创建委托时:

array<Object^>^ args = gcnew array<Object^>(1);
args[0] = MyFunctionThatIWantTheDelegateToWrap;
Object^ obj = appDomain->CreateInstanceAndUnwrap(
    assemblyName,
    "MyNamespace.MyDelegate",
    false, // ignoreCase
    BindingFlags::CreateInstance | BindingFlags::Public | BindingFlags::Instance,
    nullptr, // binder
    args, // constructor arguments
    nullptr, // culture
    nullptr); // activationAttributes
看看这个方法。注意实际方法中使用的类型。您可能需要手动加载其他程序集,或者将其他AppDomain的和设置为与主程序集相同

namespace MyNamespace
{
    public delegate string MyDelegate(int fieldId, int size);
}