如何调用CODEDOM生成的动态类

Lixin19821010 2008-03-18 08:16:27
大家好,有问题请教如下:
比如我用CODEDOM生成一个TEST.CS文件,在后来的代码中需要调用这个TEST.CS.
如何做?

生成的TEST.CS是存放在BIN.DEBUG文件下,在SOLUTION的文件夹下并没有自动添加.

十分感谢关注,如果有知道的请给予建设性指导意见.
...全文
173 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.CodeDom.Compiler;
using System.Collections;
using Microsoft.CSharp;

namespace Reporter.Windows
{
class ReporterCodeDom
{
public Assembly NewAssembly(ArrayList arraylist)
{
//创建编译器实例。
CSharpCodeProvider provider = new CSharpCodeProvider();
//设置编译参数。
CompilerParameters paras = new CompilerParameters();
paras.ReferencedAssemblies.Add("System.dll");
paras.GenerateExecutable = false;
paras.GenerateInMemory = true;

//创建动态代码。
StringBuilder classSource = new StringBuilder();

classSource.Append("using System.Collections.Generic;\n");
classSource.Append("using System;\n");
classSource.Append("using System.Text;\n");
classSource.Append("using System.ComponentModel;");

classSource.Append("namespace Reporter.Windows\n");

classSource.Append("{\n");

classSource.Append("public class DynamicClass \n");
classSource.Append("{\n");

//创建属性。
for (int i = 1; i < arraylist.Count; i++)
{
classSource.Append(propertyString(arraylist[i].ToString()));
}

classSource.Append("\n}");
classSource.Append("\n}");
System.Diagnostics.Debug.WriteLine(classSource.ToString());

//编译代码。
CompilerResults result = provider.CompileAssemblyFromSource(paras, classSource.ToString());

//获取编译后的程序集。
Assembly assembly = result.CompiledAssembly;

return assembly;
}
public void ReflectionSetProperty(object objClass, string propertyName, string value)
{
PropertyInfo[] infos = objClass.GetType().GetProperties();
foreach (PropertyInfo info in infos)
{
if (info.Name == propertyName && info.CanWrite)
{
info.SetValue(objClass, value, null);
}
}
}
public ArrayList ReflectionGetProperty(object objClass, string propertyName)
{
PropertyInfo[] infos = objClass.GetType().GetProperties();
ArrayList arrayValue = new ArrayList();
foreach (PropertyInfo info in infos)
{
if (info.Name == propertyName && info.CanRead)
{
//System.Console.WriteLine(info.GetValue(objClass, null));
arrayValue.Add(info.GetValue(objClass, null).ToString());
}
}
return arrayValue;
}
public string propertyString(string propertyName)
{
StringBuilder sbProperty = new StringBuilder();
sbProperty.Append(" \nprivate string _" + propertyName + " = null;\n");
sbProperty.Append("[Category(\"属性配置(装置物料代码配置)\")]\n");
sbProperty.Append(" public string " + "" + propertyName + "\n");
sbProperty.Append(" {\n");
sbProperty.Append(" get{ return _" + propertyName + ";} \n");
sbProperty.Append(" set{ _" + propertyName + " = value; }\n");
sbProperty.Append(" }");
return sbProperty.ToString();
}
public ArrayList GetClass1(object C,ArrayList newArrayValue)
{
ArrayList arraylist = new ArrayList();
for (int i = 1; i < newArrayValue.Count; i++)
{
arraylist.Add(C.GetType().GetProperty(newArrayValue[i].ToString()).GetValue(C, null).ToString());
}
return arraylist;
}
}
}
vwxyzh 2008-03-19
  • 打赏
  • 举报
回复
编译,加载,然后反射调用
如果要写的好一点,建议生成的类实现某个预先定义好的接口,然后用反射创建实例再用接口调用成员
Lixin19821010 2008-03-19
  • 打赏
  • 举报
回复
:)
Lixin19821010 2008-03-19
  • 打赏
  • 举报
回复
:)
Lixin19821010 2008-03-19
  • 打赏
  • 举报
回复
public static Assembly NewAssembly()
{
//创建编译器实例。
CSharpCodeProvider provider = new CSharpCodeProvider();
//设置编译参数。
CompilerParameters paras = new CompilerParameters();
paras.GenerateExecutable = false;
paras.GenerateInMemory = true;

//创建动态代码。
StringBuilder classSource = new StringBuilder();
classSource.Append("public class DynamicClass \n");
classSource.Append("{\n");

//创建属性。
ArrayList arraylist = new ArrayList();
arraylist.Add("unitId");
arraylist.Add("unitIdd");
for (int i = 0; i < arraylist.Count; i++)
{
classSource.Append(propertyString(arraylist[i].ToString()));
}
//classSource.Append(propertyString("aaa"));
//classSource.Append(propertyString("bbb"));
//classSource.Append(propertyString("ccc"));

classSource.Append("\n}");

System.Diagnostics.Debug.WriteLine(classSource.ToString());

//编译代码。
CompilerResults result = provider.CompileAssemblyFromSource(paras, classSource.ToString());

//获取编译后的程序集。
Assembly assembly = result.CompiledAssembly;

return assembly;
}
private static void ReflectionSetProperty(object objClass, string propertyName, string value)
{
PropertyInfo[] infos = objClass.GetType().GetProperties();
foreach (PropertyInfo info in infos)
{
if (info.Name == propertyName && info.CanWrite)
{
info.SetValue(objClass, value, null);
}
}
}
private static void ReflectionGetProperty(object objClass, string propertyName)
{
PropertyInfo[] infos = objClass.GetType().GetProperties();
foreach (PropertyInfo info in infos)
{
if (info.Name == propertyName && info.CanRead)
{
System.Console.WriteLine(info.GetValue(objClass, null));
}
}
}
private static string propertyString(string propertyName)
{
StringBuilder sbProperty = new StringBuilder();
sbProperty.Append(" \nprivate string _" + propertyName + " = null;\n");
sbProperty.Append(" public string " + "" + propertyName + "\n");
sbProperty.Append(" {\n");
sbProperty.Append(" get{ return _" + propertyName + ";} \n");
sbProperty.Append(" set{ _" + propertyName + " = value; }\n");
sbProperty.Append(" }");
return sbProperty.ToString();
}
Lixin19821010 2008-03-19
  • 打赏
  • 举报
回复
ArrayList arraylist = new ArrayList();
arraylist.Add("unitId");
arraylist.Add("unitIdd");
Assembly assembly = NewAssembly();
object Class1 = assembly.CreateInstance("DynamicClass");
for (int i = 0; i < arraylist.Count; i++)
{
ReflectionSetProperty(Class1, arraylist[i].ToString(), "SEYU" + i);
ReflectionGetProperty(Class1, arraylist[i].ToString());
}
this.propertyGrid1.SelectedObject = Class1;
动态调用WebService的方法,可以不用在VS本地引用,直接动态调用即可。 样例如下: /// /// 实例化WebServices /// /// WebServices地址 /// 调用的方法 /// 把webservices里需要的参数按顺序放到这个object[]里 public static object InvokeWebService(string url, string methodname, object[] args) { //这里的namespace是需引用的webservices的命名空间,我没有改过,也可以使用。也可以加一个参数从外面传进来。 //string @namespace = "Nq.Application.WebService"; string @namespace = "client"; try { //获取WSDL WebClient wc = new WebClient(); //Stream stream = wc.OpenRead(url); Stream stream = wc.OpenRead(url + "?WSDL"); ServiceDescription sd = ServiceDescription.Read(stream); string classname = sd.Services[0].Name; ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, "", ""); CodeNamespace cn = new CodeNamespace(@namespace); //生成客户端代理代码 CodeCompileUnit ccu = new CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); CSharpCodeProvider csc = new CSharpCodeProvider(); //ICodeCompiler icc = csc.CreateCompiler(); //设定编译参数 CompilerParameters cplist = new CompilerParameters(); cplist.GenerateExecutable = false;//动态编译后的程序集不生成可执行文件 cplist.GenerateInMemory = true;//动态编译后的程序集只存在于内存中,不在硬盘的文件上 cplist.ReferencedAssemblies.Add("System.dll"); cplist.ReferencedAssemblies.Add("System.XML.dll"); cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); cplist.ReferencedAssemblies.Add("System.Data.dll"); //编译代理 CompilerResults cr =

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧