我创建了一个带有Code,Name和Address的类Test作为_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: 因为我创建了一个命名空间Models。所以我需要传递Models.Test并在创建类的实例时将当前文化设置为null:
object instance = assembly.CreateInstance("Models.Test", true, BindingFlags.Default, null, null, null, null);
现在已成功创建实例。但是我仍然无法设置属性值,使用SetValue
时出错答案 0 :(得分:0)
实际上,我在生成属性时会错过set动作,在代码中,我只是设置了get动作。这是第一集的代码:
propertyField2.SetStatements.Add(new CodeAssignStatement(
new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_name"), new CodePropertySetValueReferenceExpression()));