请问用CodeDom 怎么实现创建对象?
如下形式:DateTime dt = new DateTime();
谢谢! --------------------编程问答-------------------- 给你个例子:
--------------------编程问答-------------------- 谢谢老大,我有点菜,我只想知道那句是怎么实现的? --------------------编程问答-------------------- ding
/// <summary>
/// 根据指定的信息,调用远程WebService方法
/// </summary>
/// <param name="url">WebService的http形式的地址</param>
/// <param name="namespace">欲调用的WebService的命名空间</param>
/// <param name="className">欲调用的WebService的类名(不包括命名空间前缀)</param>
/// <param name="methodName">欲调用的WebService的方法名</param>
/// <param name="args">参数列表</param>
/// <returns>WebService的执行结果</returns>
/// <remarks>
/// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。
/// 异常信息可能会发生在两个地方:
/// 1、动态构造WebService的时候,CompileAssembly失败。
/// 2、WebService本身执行失败。
/// </remarks>
/// <example>
/// <code>
/// string url = "http://localhost/ws/ws/WebService.asmx";
/// string @namespace = "Test.WS";
/// string className = "WebService";
/// string methodName = "HelloWorld";
/// object[] args = null;
/// object objReturn = ws.InvokeWebservice(url, @namespace, className, methodName, args);
/// Response.Write(objReturn.ToString());
/// </code>
/// </example>
public static object InvokeWebservice(string url, string @namespace, string className, string methodName, object[] args)
{
try
{
//获取WSDL
System.Net.WebClient wc = new System.Net.WebClient();
System.IO.Stream stream = wc.OpenRead(url + "?WSDL");
System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);
//生成客户端代理类代码
System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();
//设定编译参数
System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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");
//编译代理类
System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
if (true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
//生成代理实例,并调用方法
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@namespace + "." + className, true, true);
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo mi = t.GetMethod(methodName);
return mi.Invoke(obj, args);
}
catch (Exception ex)
{
throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
}
}
补充:.NET技术 , C#