国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#動態方法調用

2024-07-21 02:24:59
字體:
來源:轉載
供稿:網友
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:/>dynainvoke

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天全县| 德州市| 舒城县| 蒙阴县| 莱阳市| 武清区| 舞阳县| 措美县| 江城| 南城县| 新蔡县| 杭州市| 敖汉旗| 丁青县| 莱芜市| 扎兰屯市| 白城市| 娱乐| 巴南区| 霍林郭勒市| 昌吉市| 新营市| 安庆市| 鹤峰县| 汉源县| 汝城县| 德庆县| 同仁县| 红安县| 湖口县| 垫江县| 新乡市| 北宁市| 木兰县| 尉犁县| 恭城| 息烽县| 赤壁市| 新乡市| 清远市| 信阳市|