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

首頁 > 學院 > 開發設計 > 正文

C#反射之基礎應用

2019-11-17 02:51:16
字體:
來源:轉載
供稿:網友
C#反射之基礎應用

今天把反射的東西整理了一下,提供了最全面的東西,當然也是基礎的東西, 在學好了這一切的基礎上,大家可以學習反射的具體插件等應用首先我們建立一個類庫,將它生成為reflectPRj.dll,

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespacereflectPrj

{

///<summary>

///接口

///</summary>

publicinterfaceInterface1

{

intAdd(intnum);

intAdd();

}

///<summary>

///用來被測試的類

///</summary>

publicclassReflectTest:Interface1

{

publicstringwritea;

publicstringWriteA

{

get{returnwritea; }

set{ writea =value; }

}

publicstringWriteb;

publicstringWriteB

{

get{returnWriteb; }

set{ Writeb =value; }

}

publicReflectTest()

{

this.WriteA ="WriteA";

this.WriteB ="WriteB";

}

publicReflectTest(stringa,stringb)

{

this.WriteA = a;

this.WriteB = b;

}

publicintAdd()

{

return100;

}

publicintAdd(intnum)

{

returnnum;

}

publicstringWriteString(stringa,stringb)

{

return"歡迎你,"+ a +"------"+b;

}

publicstaticstringWriteName(strings)

{

return"歡迎光臨,"+ s;

}

publicstringWriteNoPara()

{

return"你使用的是無參數方法!";

}

privatestringWritePrivate()

{

return"私有類型的方法!";

}

}

}

后,建立再建立一個項目引入該reflectPrj.dll

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingreflectPrj;

usingSystem.Threading;

usingSystem.Reflection;

namespacereflectPrjTest

{

classMyReflectTest

{

//建立委托

delegatestringTestDelegate(stringa,stringb);

staticvoidMain(string[] args)

{

Assemblyassembly=Assembly.Load("reflectPrj");

foreach(Typevarinassembly.GetTypes())

{

Console.WriteLine(var.Name);//顯示dll下所有的類

}

//*******************************************************

Module[] modules = assembly.GetModules();

foreach(Modulemoduleinmodules)

{

Console.WriteLine("module(模塊/組件)名:"+module.Name);

}

//*******************************************************

//得到具體的類的類型

Typea =typeof(reflectPrj.ReflectTest);

Console.WriteLine(a.Name);

//*******************************************************

//A------創建類型的實例---->此處是由帶參的構造函數的來得到的實例

string[] paras ={"aaa","bbb"};

//創建該類的實例,后面的paras為有參構造函數的參數---->此obj即是類型a的一個實例

//此實例調用的是一個帶參的構造函數

objectobj =Activator.CreateInstance(a,paras);

//得到對象的屬性

Console.WriteLine("得到對象a的屬性:");

foreach(objectvarina.GetProperties())

{

Console.WriteLine(var.ToString());

}

MethodInfo[] miArr = a.GetMethods();

Console.WriteLine("顯示所有的共有方法:");

//顯示所有的共有方法

foreach(MethodInfomethodinmiArr)

{

Console.WriteLine(method.Name);

}

//************************************************************

//顯示具體的方法

Console.WriteLine("顯示具體的方法!");

//1.帶參的方法的使用

MethodInfomi = a.GetMethod("WriteString");

stringmiReturn =(string) mi.Invoke(obj,newobject[] {"使用的是帶參數的非靜態方法","2"});

Console.WriteLine("---"+mi.Name+"返回值:"+miReturn);

//2.不帶參數的方法調用

Console.WriteLine("不帶參數的方法的調用:");

MethodInfomiNopara = a.GetMethod("WriteNoPara");

stringmiNoparaReturn = (string)miNopara.Invoke(obj,null);

Console.WriteLine("---"+miNoparaReturn);

//3.私有類型方法的使用

Console.WriteLine("私有類型方法的使用:");

MethodInfomiprivate = a.GetMethod("WritePrivate",BindingFlags.Instance |BindingFlags.NonPublic);

stringmiPrivateReturn = (string)miPrivate.Invoke(obj,null);

Console.WriteLine("---"+miPrivateReturn);

Console.WriteLine("***********************屬性的使用**********************");

//4.得到對象的屬性

PropertyInfo[] propertys = a.GetProperties(BindingFlags.Instance |BindingFlags.NonPublic |BindingFlags.Public);

//5.顯示所有屬性名

Console.WriteLine("對象的所有屬性名如下:");

foreach(PropertyInfoproinpropertys)

{

//Console.WriteLine(pro.Name);

//獲取屬性最初的值

Console.WriteLine(pro.Name+":"+pro.GetValue(obj,null));

//給屬性重新賦值

pro.SetValue(obj,"張三豐",null);

Console.WriteLine(pro.Name +":"+ pro.GetValue(obj,null));

}

//6.獲取指定的屬性,并賦值

PropertyInfopropertyInfo=a.GetProperty("WriteA",BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);

propertyInfo.SetValue(obj,"郁金香",null);

Console.WriteLine(propertyInfo.Name+":"+propertyInfo.GetValue(obj,null));

Console.WriteLine("*********************FieldInfo---公開字段的使用***********************");

//7.字段的使用---->只能獲取公開字段

FieldInfof1 = a.GetField("writea",BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);

Console.WriteLine(f1.GetValue(obj));

try

{

reflectPrj.ReflectTesttest =newReflectTest("Marry","Jack");

TypemyReflect =typeof(ReflectTest);

<
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普兰县| 黄浦区| 商水县| 红河县| 潮安县| 灵石县| 武强县| 博野县| 云林县| 永宁县| 天峨县| 那曲县| 永平县| 淮滨县| 涡阳县| 辽阳县| 温宿县| 沂水县| 深水埗区| 文昌市| 滨海县| 银川市| 大名县| 宜良县| 黎城县| 庐江县| 洪泽县| 霞浦县| 新河县| 敖汉旗| 尼玛县| 南陵县| 连江县| 武胜县| 田东县| 义乌市| 天长市| 瑞昌市| 博湖县| 游戏| 石屏县|