动态创建、编译类的代码如下:
public static Type CreateDynamicType(string typeName) { //创建编译器实例。 CSharpCodeProvider provider = new CSharpCodeProvider(); //设置编译参数。 CompilerParameters paras = new CompilerParameters(); paras.GenerateExecutable = false; paras.GenerateInMemory = true; paras.IncludeDebugInformation = true; var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.IsDynamic).Select(x => x.Location); paras.ReferencedAssemblies.AddRange(assemblies.ToArray()); //创建动态代码。 StringBuilder classSource = new StringBuilder(); bool containsNamespace = false; string newTypeName = typeName; if (typeName.Contains(".")) { int index = typeName.LastIndexOf("."); newTypeName = typeName.Substring(index+1); classSource.Append("namespace " + typeName.Substring(0, index) + " {\n"); containsNamespace = true; } classSource.Append(@"public class " + newTypeName + " \n"); classSource.Append("{\n"); //创建属性。 classSource.Append(CreatePropertyString("Prop1", "string")); classSource.Append(CreatePropertyString("Prop2", "int")); classSource.Append(CreatePropertyString("Prop3","string")); classSource.Append("}\n"); if (containsNamespace) { classSource.Append("}\n"); } //System.Diagnostics.Debug.WriteLine(classSource.ToString()); //编译代码。 CompilerResults result = provider.CompileAssemblyFromSource(paras, classSource.ToString()); if (result.Errors.HasErrors) { foreach (CompilerError error in result.Errors) { Console.WriteLine(error.Line + ":" + error.ErrorText); } return null; } //获取编译后的程序集。 Assembly assembly = result.CompiledAssembly; return assembly.GetType(typeName); } private static string CreatePropertyString(string propertyName, string typeName) { StringBuilder sbProperty = new StringBuilder(); sbProperty.Append(string.Format(" private {0} _{1} ;\n", typeName, propertyName)); sbProperty.Append(string.Format(" public {0} {1} \n", typeName, propertyName)); sbProperty.Append(" {\n"); sbProperty.Append(" get{ return _"+propertyName+";}\n"); sbProperty.Append(" set{_"+propertyName+" = value; }\n"); sbProperty.Append(" }\n"); return sbProperty.ToString(); } private static void ReflectionSetProperty(object objClass, string propertyName, object value) { PropertyInfo[] infos = objClass.GetType().GetProperties(); foreach (PropertyInfo info in infos) { if (info.Name == propertyName && info.CanWrite) { info.SetValue(objClass, value, null); } } } private static object ReflectionGetProperty(object objClass, string propertyName) { PropertyInfo[] infos = objClass.GetType().GetProperties(); foreach (PropertyInfo info in infos) { if (info.Name == propertyName && info.CanRead) { object obj = info.GetValue(objClass, null); return obj; } } return null; }