我需要动态调用dll函数。
我正在使用标准 Windows API 来加载 dll 并获取 proc 地址。
在检索 proc 的 IntPtr 后,我尝试将其转换为委托:
Func<int> inv = (Func<int>)Marshal.GetDelegateForFunctionPointer(proc, typeof(Func<int>));
Run Code Online (Sandbox Code Playgroud)
但它失败了,因为 typeof(Func) 返回泛型类型。
有没有什么干净的方法来完成我想要做的事情,而无需声明成员委托并将其用作类型?
我的意思是我正在努力避免以下冗余:
//I need this member only for typeof operator
private delegate int RunDll();
RunDll inv = (RunDll)Marshal.GetDelegateForFunctionPointer(proc, typeof(RunDll));
Run Code Online (Sandbox Code Playgroud)
如果委托类型是动态的并且您不知道参数,情况会更糟。然后您可以使用.NET 方法来生成它。
\n\npublic static class DelegateCreator\n{\n private static readonly Func<Type[],Type> MakeNewCustomDelegate = (Func<Type[],Type>)Delegate.CreateDelegate(typeof(Func<Type[],Type>), typeof(Expression).Assembly.GetType("System.Linq.Expressions.Compiler.DelegateHelpers").GetMethod("MakeNewCustomDelegate", BindingFlags.NonPublic | BindingFlags.Static));\n\n public static Type NewDelegateType(Type ret, params Type[] parameters)\n {\n Type[] args = new Type[parameters.Length];\n parameters.CopyTo(args, 0);\n args[args.Length-1] = ret;\n return MakeNewCustomDelegate(args);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\xc2\xa0
\n\nDelegateCreator.NewDelegateType(typeof(int)) //returns non-generic variant of Func<int>\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
6936 次 |
| 最近记录: |