我正在尝试从MethodReference生成委托类型,但PEVerify一直说我的类型无效。
这是我的代码:
TypeDefinition CreateDelegates(TypeDefinition type, MethodReference source) {
var desiredname = source.Name.ToLowerInvariant() + "__DELEGATE";
var module = type.Module;
if (type.HasNestedTypes) {
foreach (var deleg in type.NestedTypes) {
if (deleg.Name == desiredname) return deleg;
}
}
var multiDelegateType = module.Import(typeof(MulticastDelegate));
var voidType = module.Import(typeof(void));
var objectType = module.Import(typeof(object));
var nativeIntType = module.Import(typeof(IntPtr));
var asyncCallbackType = module.Import(typeof(AsyncCallback));
var asyncResultType = module.Import(typeof(IAsyncResult));
// Create new delegate type
var dlgtype = new TypeDefinition("", desiredname, TypeAttributes.Sealed | TypeAttributes.NestedAssembly, multiDelegateType);
type.NestedTypes.Add(dlgtype);
var constructor = new MethodDefinition(".ctor", MethodAttributes.Public | MethodAttributes.CompilerControlled | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName | MethodAttributes.HideBySig, voidType);
constructor.IsRuntime = true;
constructor.HasThis = true;
constructor.IsHideBySig = true;
//constructor.IsVirtual = true;
constructor.IsPublic = true;
constructor.Parameters.Add(new ParameterDefinition("'object'", ParameterAttributes.None, objectType));
constructor.Parameters.Add(new ParameterDefinition("'method'", ParameterAttributes.None, nativeIntType));
dlgtype.Methods.Add(constructor);
var begininvoke = new MethodDefinition("BeginInvoke", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual, asyncResultType);
begininvoke.IsRuntime = true;
begininvoke.HasThis = true;
begininvoke.IsHideBySig = true;
begininvoke.IsRuntimeSpecialName = true;
begininvoke.IsSpecialName = true;
begininvoke.IsPublic = true;
begininvoke.IsVirtual = true;
foreach (var para in source.Parameters) {
begininvoke.Parameters.Add(new ParameterDefinition(para.Name, para.Attributes, para.ParameterType));
}
begininvoke.Parameters.Add(new ParameterDefinition("callback", ParameterAttributes.None, asyncCallbackType));
begininvoke.Parameters.Add(new ParameterDefinition("object", ParameterAttributes.None, objectType));
dlgtype.Methods.Add(begininvoke);
var endinvoke = new MethodDefinition("EndInvoke", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual, voidType);
endinvoke.Parameters.Add(new ParameterDefinition("result", ParameterAttributes.None, asyncResultType));
endinvoke.IsRuntime = true;
endinvoke.HasThis = true;
endinvoke.IsHideBySig = true;
endinvoke.IsRuntimeSpecialName = true;
endinvoke.IsSpecialName = true;
endinvoke.IsPublic = true;
dlgtype.Methods.Add(endinvoke);
var invoke = new MethodDefinition("Invoke", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual, voidType);
foreach (var para in source.Parameters) {
invoke.Parameters.Add(new ParameterDefinition(para.Name, para.Attributes, para.ParameterType));
}
invoke.IsRuntime = true;
invoke.HasThis = true;
invoke.IsHideBySig = true;
invoke.IsVirtual = true;
invoke.IsPublic = true;
dlgtype.Methods.Add(invoke);
return dlgtype;
}
在peverify中我得到:
[token 0x02000008] Type load failed.
[token 0x02000009] Type load failed.
[token 0x0200000A] Type load failed.
[token 0x0200000B] Type load failed.
哪位代表。
答案 0 :(得分:1)
我很确定我太迟了,但这里什么都没有。我打赌你得到“类型加载失败”错误消息,因为构造函数参数周围有单引号。如果您有兴趣,这里是一个完全实现的Delegate发射器类:
我希望我帮忙!