它仅适用于附加到托管方法的委托。
如果试图使用Mike的文章对带有GetDelegateForFunctionPointer的非托管dll函数附加的委托进行处理,那么CreateDelegate技术将返回空的附件,因此在调用时崩溃。
public abstract class IInvokable
{
public abstract T Call0<T>();
public abstract T Call1<T, T2>(T2 arg);
public abstract T Call2<T, T2, T3>(T2 arg1, T3 arg2);
public abstract void SetDelegate(Delegate thedel);
public abstract Type GetDelegateType();
}
例如:
class Invokable : IInvokable
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int SomeDelegateTypeReturningIntTakingVoid();
public override Type GetDelegateType()
{
return typeof(SomeDelegateTypeReturningIntTakingVoid);
}
public override void SetDelegate(Delegate thedel)
{
mydelegate = (SomeDelegateTypeReturningIntTakingVoid)thedel;
}
public SomeDelegateTypeReturningIntTakingVoidmydelegate;
public override T Call0<T>()
{
return (T)(Object)mydelegate();
}
public override T Call1<T, T2>(T2 arg)
{
throw new ArgumentException("this delegate is a Call0<int>");
}
public override T Call2<T, T2, T3>(T2 arg1, T3 arg2)
{
throw new ArgumentException("this delegate has a Call0<int>");
}
}
在这一点上,类型必须是完全“硬编码”的,这意味着不能使用Func,因为它会阻止使用GetDelegateForFunctionPointer,因为该函数有一个愚蠢的限制(不能使用泛型,因为MS团队基本上是不称职的,c.f.msdn论坛提供了该函数的源代码)。
我的解决方案是:
Type GenerateDynamicType(string sourceCode, string typenameToGet)
{
var cp = new System.CodeDom.Compiler.CompilerParameters
{
GenerateInMemory = true, // you will get a System.Reflection.Assembly back
GenerateExecutable = false, // Dll
IncludeDebugInformation = false,
CompilerOptions = ""
};
var csharp = new Microsoft.CSharp.CSharpCodeProvider();
// this actually runs csc.exe:
System.CodeDom.Compiler.CompilerResults cr =
csharp.CompileAssemblyFromSource(cp, sourceCode);
// cr.Output contains the output from the command
if (cr.Errors.Count != 0)
{
// handle errors
throw new InvalidOperationException("error at dynamic expression compilation");
}
System.Reflection.Assembly a = cr.CompiledAssembly;
// party on the type here, either via reflection...
Type t = a.GetType(typenameToGet);
return t;
}
并为各种可调用的动态对象生成代码。创建实例时使用:
IInvokable inv = (IInvokable)Activator.CreateInstance(GenerateDynamicType(...));
最后是一个非常复杂的系统。谢谢你这么懒,真的。