国产探花免费观看_亚洲丰满少妇自慰呻吟_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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黑龙江省| 疏附县| 绍兴市| 宁化县| 台安县| 康平县| 灯塔市| 新津县| 中超| 灯塔市| 无棣县| 皋兰县| 南郑县| 庄浪县| 梅州市| 泸定县| 田林县| 淮安市| 翁牛特旗| 乌拉特前旗| 张家界市| 兴业县| 北宁市| 辉南县| 青岛市| 呼伦贝尔市| 马鞍山市| 宜都市| 仁布县| 铜川市| 金塔县| 全州县| 衡东县| 绩溪县| 宕昌县| 黎城县| 江山市| 通城县| 浦城县| 安陆市| 临沭县|