.net 如何为具有嵌套类的类生成il代码,并且派生类在方法中使用嵌套类

slsn1g29  于 2023-02-14  发布在  .NET
关注(0)|答案(1)|浏览(112)

我正在通过TypeBuilder创建一个类(DerivedClass)。我必须在DerivedClass中创建另一个嵌套类(NestedClass),所以我使用了DefineNestedType。但是当我在DerivedClass的方法中声明具有类型NestedClass的局部变量时,它会抛出异常“Unhandled exception. System.TypeLoadException:未能加载类型“DerivedClass”“。
我想得到的:

class DerivedClass
{
    class NestedClass
    {
         //some code
    }

    void Foo()
    {
        var nested = new NestedClass();
        //some code with nested
    }
}

当我生成“Foo”方法时,在var composite = lout.DeclareLocal(internalClass);行抛出异常。lout = builder.GetILGenerator(),builder = MethodBuilder
完整异常:未处理的异常。系统类型LoadException:

Could not load type 'CompositeFooable' from assembly 'CompositeAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.RuntimeTypeHandle.GetDeclaringType(RuntimeType type)
at System.RuntimeType.RuntimeTypeCache.GetEnclosingType()
at System.RuntimeType.get_DeclaringType()
at System.Reflection.Emit.ModuleBuilder.GetTypeRefNested(Type type, Module refedModule, String strRefedModuleFileName)
at System.Reflection.Emit.ModuleBuilder.GetTypeTokenWorkerNoLock(Type type, Boolean getGenericDefinition)
at System.Reflection.Emit.ModuleBuilder.GetTypeTokenInternal(Type type, Boolean getGenericDefinition)
at System.Reflection.Emit.SignatureHelper.AddOneArgTypeHelperWorker(Type clsArgument, Boolean lastWasGenericInst)
at System.Reflection.Emit.SignatureHelper.AddArgument(Type argument, Boolean pinned)
at System.Reflection.Emit.ILGenerator.DeclareLocal(Type localType, Boolean pinned)
at System.Reflection.Emit.ILGenerator.DeclareLocal(Type localType)
at InterfaceBuilder.CompositeCreator.CreateInterfaceMethodWithParams(TypeBuilder typeBuilder, MethodInfo method, Type generic, KeyValuePair`2[] args, Type internalClass) in C:\Us
ers\Vladimir\RiderProjects\InterfaceBuilder\InterfaceBuilder\CompositeGeneration\CompositeCreator.cs:line 291
at InterfaceBuilder.CompositeCreator.Create[T]() in C:\Users\Vladimir\RiderProjects\InterfaceBuilder\InterfaceBuilder\CompositeGeneration\CompositeCreator.cs:line 76
at InterfaceBuilder.Program.Main(String[] args) in C:\Users\Vladimir\RiderProjects\InterfaceBuilder\InterfaceBuilder\Program.cs:line 20

复合可查找-派生类复合程序集-生成的程序集
我尝试在DefineNestedType中添加不同的属性,并在DeclareLocal中设置TypeBuilder。此外,我创建了非嵌套类,它工作正常,但我需要完全嵌套的类。此外,我尝试声明对象,但稍后在方法中,我使用了自构造函数、方法和字段,然后它在这几行中抛出异常

aiazj4mn

aiazj4mn1#

您遇到的异常指示无法加载DerivedClass类型。这可能是由于DerivedClass的定义与声明局部变量时使用的类型不匹配。
在使用TypeBuilder时,必须注意创建和定义类型的顺序。在这种情况下,很可能您试图在NestedClass完全定义之前使用它。
若要解决此问题,请尝试确保在开始定义DerivedClass的方法(包括Foo)之前已完成NestedClass的定义。此外,请确保在声明局部变量时将用于NestedClass的TypeBuilder作为参数传递给DeclareLocal。

TypeBuilder derivedClassBuilder = ...;
TypeBuilder nestedClassBuilder = ...;

// Define NestedClass type first
nestedClassBuilder.CreateType();

// Define methods for DerivedClass
MethodBuilder fooBuilder = derivedClassBuilder.DefineMethod("Foo", ...);
ILGenerator il = fooBuilder.GetILGenerator();

// Get the Type object for NestedClass
Type nestedClassType = nestedClassBuilder.CreateType();

// Declare local variable of type NestedClass
LocalBuilder nested = il.DeclareLocal(nestedClassType);

// ... rest of the method implementation ...

// Create DerivedClass type
Type derivedClassType = derivedClassBuilder.CreateType();

相关问题