从CodeDom实例对象,以及如何使用它

本文关键字:何使用 CodeDom 实例 对象 | 更新日期: 2023-09-27 18:04:03

我创建了一个类Test,其中Code, Name和Address作为_code, _name和_address的属性。CodeDom.

CSharpCodeProvider provider = new CSharpCodeProvider();
// Build the parameters for source compilation.
CompilerParameters cp = new CompilerParameters();
// Add an assembly reference.
cp.ReferencedAssemblies.Add("System.dll");
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
cp.OutputAssembly = "OutputAssembly";
//
//
CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace namespaces = new CodeNamespace("Models");
compileUnit.Namespaces.Add(namespaces);
// Define a class
CodeTypeDeclaration customClass = new CodeTypeDeclaration("Test");
customClass.IsClass = true;
customClass.TypeAttributes = System.Reflection.TypeAttributes.Public;
CodeMemberField field1 = new CodeMemberField("System.String", "_code");
field1.Attributes = MemberAttributes.Private;
customClass.Members.Add(field1);
CodeMemberProperty propertyField1 = new CodeMemberProperty();
propertyField1.Attributes = MemberAttributes.Public;
propertyField1.Name = "Code";
propertyField1.HasGet = true;
propertyField1.Type = new CodeTypeReference("System.String");
propertyField1.GetStatements.Add(new CodeMethodReturnStatement(
    new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "_code")));
customClass.Members.Add(propertyField1);
CodeMemberField field2 = new CodeMemberField("System.String", "_name");
field2.Attributes = MemberAttributes.Private;
customClass.Members.Add(field2);
CodeMemberProperty propertyField2 = new CodeMemberProperty();
propertyField2.Attributes = MemberAttributes.Public;
propertyField2.Name = "Name";
propertyField2.HasGet = true;
propertyField2.Type = new CodeTypeReference("System.String");
propertyField2.GetStatements.Add(new CodeMethodReturnStatement(
    new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "_name")));
customClass.Members.Add(propertyField2);
CodeMemberField field3 = new CodeMemberField("System.String", "_address");
field3.Attributes = MemberAttributes.Private;
customClass.Members.Add(field3);
CodeMemberProperty propertyField3 = new CodeMemberProperty();
propertyField3.Attributes = MemberAttributes.Public;
propertyField3.Name = "Address";
propertyField3.HasGet = true;
propertyField3.Type = new CodeTypeReference("System.String");
propertyField3.GetStatements.Add(new CodeMethodReturnStatement(
    new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "_address")));
customClass.Members.Add(propertyField3);
namespaces.Types.Add(customClass);
CompilerResults cr = provider.CompileAssemblyFromDom(cp, compileUnit);
if (cr.Errors.Count > 0)
{
    ApplicationException exception = new ApplicationException("Error building assembly");
    foreach (CompilerError ce in cr.Errors)
    {
        exception.Data.Add(ce.ToString(), ce);
    }
    throw exception;
}

类创建成功。但是当我创建类Test的实例时,它返回:{Name = "CultureInfo" FullName = "System.Globalization.CultureInfo"}

Assembly assembly = cr.CompiledAssembly;
object instance = assembly.CreateInstance("Test", true, BindingFlags.Default, null, null, Thread.CurrentThread.CurrentCulture, null);
Type instType = instance.GetType();

下面的代码是设置值为属性,但我从来没有达到它,导致上面的错误

instType.GetProperty("Code").SetValue(instance, "Code_value", null);
instType.GetProperty("Name").SetValue(instance, "Name_value", null);
instType.GetProperty("Address").SetValue(instance, "Address_value", null);
var listType = typeof(List<>);
var genericListType = listType.MakeGenericType(instType);
var instGenericListType = (IList)Activator.CreateInstance(genericListType);
instGenericListType.Add(instance);

我的问题:我在代码中错过了什么使它正确?

更新1:

因为我创建了一个命名空间模型。所以我需要通过模型。

类创建实例时,测试并设置当前区域性为空
object instance = assembly.CreateInstance("Models.Test", true, BindingFlags.Default, null, null, null, null);

实例创建成功。但是我仍然不能设置属性的值,当使用SetValue

从CodeDom实例对象,以及如何使用它

时出现错误

实际上,我在生成属性时错过了set action,在代码中,我只是设置了get action。这是第一个集合的代码:

propertyField2.SetStatements.Add(new CodeAssignStatement(
            new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_name"), new CodePropertySetValueReferenceExpression()));