2014-05-19 70 views
0

我創建了一個類Code,Code,Name和Address作爲_code,_name和_address的屬性。 CodeDom使用下面的代碼。CodeDom的Instace對象以及如何使用它

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類的實例,它返回: {名稱=「CultureInfo的」全名=「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並設置當前區域性爲空時創建類的實例:創建成功現在

object instance = assembly.CreateInstance("Models.Test", true, BindingFlags.Default, null, null, null, null); 

實例。但我仍然無法設置屬性的值,我使用SetValue時出現錯誤

回答

0

實際上,我錯過了在生成屬性時設置動作,在代碼中,我只是設置了get動作。這是一組代碼:

propertyField2.SetStatements.Add(new CodeAssignStatement(
      new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_name"), new CodePropertySetValueReferenceExpression())); 
相關問題