2

我正在尝试从 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.

哪些是代表。

4

1 回答 1

1

我很确定我来晚了,但这里什么都没有。我敢打赌,由于构造函数参数周围的单引号,您会收到“类型加载失败”错误消息。如果你有兴趣,这里有一个完全实现的 Delegate 发射器类:

https://github.com/SusirinkeLaborams/LaborasLang-aka-ForScience-/blob/master/LaborasLangCompiler/Codegen/Types/DelegateEmitter.cs

我希望我有所帮助!

于 2014-04-17T18:18:11.483 回答