ASP编程 PHP编程 JSP编程 NET编程 CGI编程 XML编程 
Google adsense申请技巧本站核心代理域名注册主机业务 快速发布你的买卖域名买卖网站信息 1元注册 cn域名
站长每日新闻导读 √ ·推荐万网空间¥120元 150m 站长网:站长必上的网站网站联盟大全本站代理万网域名55空间120元
 2006-12-10 10:31:17

C#动态方法调用

来源: 字体:[ ]
Dynamic Method Invocation

One very useful feature related to Reflection is the ability to create objects dynamically and call methods on them.
note : Class1.cs has methods which will be dynamically invoked at runtime from the DynaInvoke.cs

Class1.cs

using System;
class Class1{
       public static String method1()
       {
           return "I am Static method (method1) in class1";
       }
       public String method2()
       {
           return "I am a Instance Method (method2) in Class1";
       }
       public String method3(String s)
       {
          return "Hello " + s;
       }
}
save this file as Class1.cs and Compile c:/>csc /t:library Class1.cs

DynaInvoke.cs

using System;
using System.Reflection;
class DynamicInvoke
{
public static void Main(String [] args)
{

String path = "Class1.dll"
Assembly a = Assembly.Load(path);

//Invoking a static method
Type mm = a.GetType("Class1");
String i = (String) mm.InvokeMember ("method1",BindingFlags.Default | BindingFlags.InvokeMethod,null,null,new object [] {}); Console.WriteLine(i);

//Invoking a non-static method
object o = Activator.CreateInstance(mm);
i = (String) mm.InvokeMember("method2",BindingFlags.Default | BindingFlags.InvokeMethod,null,o,new object [] {});
Console.WriteLine(i);

//Invoking a non-static method with parameters
object [] par = new object[] {"kunal"};
i = (String) mm.InvokeMember("method3",BindingFlags.Default | BindingFlags.InvokeMethod,null,o,par);
Console.WriteLine(i);
}
}

save this file as DynaInvoke.cs and Compile c:/>csc DynaInvoke.cs and run C:\>DynaIn
网站地图 - 域名注册续费虚拟主机代理 - 交易论坛 - 网站投稿 - 广告服务 - 帮助中心 - 联系我们
Copyright ©2003-2007 www.Admin5.com All Rights Reserved