简体   繁体   English

如何定义列表<foo>在 do.net Core 7 中使用 AssemblyBuilder 和 TypeBuilder 作为 Type 的属性?</foo>

[英]how to define List<Foo> as a property of Type by using AssemblyBuilder and TypeBuilder in dotnet Core 7?

I'm trying to crate Dymamic Dll by using do.net 7 Reflection.Emit.我正在尝试使用 do.net 7 Reflection.Emit 来创建 Dymamic Dll。

I would like to create an Assembly which has a list of string (or Class Type) propery like below;我想创建一个程序集,其中包含如下所示的字符串(或 Class 类型)属性列表;

 private List<string> Items;
// OR 
 private List<Foo> Items;

And This is the code I'm using to add any property to a Type.这是我用来向类型添加任何属性的代码。

 AssemblyGeneratorHelper.AddProperty(dynamicType, "Items", typeof(List<string>));

This is the my my Helper method.这是我的助手方法。

public static void AddProperty(TypeBuilder typeBuilder, string propertyName, Type propertyType)
    {
        var fieldBuilder = typeBuilder.DefineField("_" + propertyName, propertyType, FieldAttributes.Private);
        var propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);

        var getMethod = typeBuilder.DefineMethod("get_" + propertyName,
            MethodAttributes.Public |
            MethodAttributes.SpecialName |
            MethodAttributes.HideBySig, propertyType, Type.EmptyTypes);
        var getMethodIL = getMethod.GetILGenerator();
        getMethodIL.Emit(OpCodes.Ldarg_0);
        getMethodIL.Emit(OpCodes.Ldfld, fieldBuilder);
        getMethodIL.Emit(OpCodes.Ret);

        var setMethod = typeBuilder.DefineMethod("set_" + propertyName,
              MethodAttributes.Public |
              MethodAttributes.SpecialName |
              MethodAttributes.HideBySig,
              null, new[] { propertyType });
        var setMethodIL = setMethod.GetILGenerator();
        Label modifyProperty = setMethodIL.DefineLabel();
        Label exitSet = setMethodIL.DefineLabel();

        setMethodIL.MarkLabel(modifyProperty);
        setMethodIL.Emit(OpCodes.Ldarg_0);
        setMethodIL.Emit(OpCodes.Ldarg_1);
        setMethodIL.Emit(OpCodes.Stfld, fieldBuilder);
        setMethodIL.Emit(OpCodes.Nop);
        setMethodIL.MarkLabel(exitSet);
        setMethodIL.Emit(OpCodes.Ret);

        propertyBuilder.SetGetMethod(getMethod);
        propertyBuilder.SetSetMethod(setMethod);
    }

This Code is running successfully.此代码运行成功。 But when I try to use generated Assembly I'm getting error shown like below.但是当我尝试使用生成的程序集时,出现如下所示的错误。 在此处输入图像描述

Actually problem is I don't know how to add List property to a complex type.实际上问题是我不知道如何将 List 属性添加到复杂类型。 I think there is an other way to use instead of我认为还有另一种方法可以代替

AssemblyGeneratorHelper.AddProperty(dynamicType, "Items",typeof(List<string>));

This is how I solved the problem.这就是我解决问题的方法。 It works as I expected.它按我的预期工作。

    Type t1= typeof(Foo).MakeArrayType();
    // OR 
    Type t2 = typeof(string).MakeArrayType();

AssemblyGeneratorHelper.AddProperty(dynamicType, "Items", t1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用AssemblyBuilder构建复杂类型 - Using AssemblyBuilder to build a complex type 如何在 fluent ef core 中定义一个映射到多个一对多共享超类型的列表属性? - How to define one list property that maps to multiple one to manys of shared super type in fluent ef core? 如果我使用TypeBuilder构建一个带有属性的类型,我是否需要使用propertyBuilder? - If I build a type with a property using TypeBuilder do I need to use a propertyBuilder? 使用不带Lambda表达式的LinQ在List类型属性上定义条件 - using LinQ without Lambda expressions to define condition on a List type property 如何将类型从预定义的程序集复制到动态AssemblyBuilder - How to copy a type from a predefined assembly to a dynamic AssemblyBuilder 检索类型Foo <Bar> 从TypeBuilder创建Bar时从名称开始 - Retrieve Type Foo<Bar> from name when Bar is created from TypeBuilder 从Type转换为TypeBuilder - Convert from Type to TypeBuilder 如何使用VisualStudio构建配置为dotnet Core项目定义DEBUG? - How to use VisualStudio build configuration to define DEBUG for dotnet Core project? 使用TypeBuilder通过构造函数从基本类型为字符串分配值 - Assigning value to string from base type through constructor using TypeBuilder TypeBuilder设置类型大小 - TypeBuilder set type size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM