起初:方法DelegateCreator.NewDelegateType的實現是不正確的!
public static Type NewDelegateType(Type ret, params Type[] parameters) {
/*
Type[] args = new Type[parameters.Length]; // Create "args" array of same length as "parameters" (must be length + 1)
parameters.CopyTo(args, 0); // Copy all values of parameters to "args" array
args[args.Length - 1] = ret; // Put "ret" value to last item of "args" array
return MakeNewCustomDelegate(args);
*/
var offset = parameters.Length;
Array.Resize(ref parameters, offset + 1);
parameters[offset] = ret;
return MakeNewCustomDelegate(parameters);
}
什麼是助手的利潤,如果我們不能得到一個類型的委託來調用?見下文試驗例。
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
namespace GenericDelegates {
static class DelegateCreator {
public 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
)
);
public static Type NewDelegateType(Type ret, params Type[] parameters) {
var offset = parameters.Length;
Array.Resize(ref parameters, offset + 1);
parameters[offset] = ret;
return MakeNewCustomDelegate(parameters);
}
}
static class Kernel {
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetModuleHandle(string name);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr module, string name);
}
static class InvokeHelper {
public static Delegate MakeDelegate(this IntPtr address, Type ret, params Type[] parameters) {
return Marshal.GetDelegateForFunctionPointer(address, DelegateCreator.NewDelegateType(ret, parameters));
}
}
class Program {
static void Main(string[] args) {
var kernel = Kernel.GetModuleHandle("kernel32.dll");
var address = Kernel.GetProcAddress(kernel, "GetModuleHandleW");
Console.WriteLine(@"
Module base: 0x{0:X8}
Entry point: 0x{1:X8}
", (int) kernel, (int) address);
var invoke = address.MakeDelegate(typeof(IntPtr), typeof(string));
Console.WriteLine(@"
Untyped delegate: {0}
Cast to Invoke: {1}
", invoke, invoke as Func<string, IntPtr> == null ? "Error" : "Valid"); // invoke as Func<string, IntPtr> = NULL
Console.ReadKey();
}
}
}
這完全是我所需要的,非泛型的委託類型創建者,所以我可以調用Marshal.GetFunctionPointerForDelegate。 :) – Ani 2015-04-24 00:38:51