C#反射机制
ï?? Â?3/4??Ã?Ã?Â?ï??à Â?à Â?1Â??×
pdf??Î?Î??Ã??Ã??úWAP???ˉà à Ã???Ã?2"1/4??ï??1/2Â?Ã?Ã??úÃ???Ã?????TXTï???"Ã??????â????1/4tÎ?1/2Â?3/4"ú2Ã??â???ï??
C#Â?â??Ã??"ú?? ×?1/2Ã?×D???Â??Â?Ã??Ã?"??
C#Î??Â?â??Ã??"ú??ï????ï??Ã-??Ã?oÃ-â??Ã?1/4Ã?*??Ã-?ï??
Ã?Ã?Î?1/2*â??Ã???Ã???â??Ã?1/4Ã??1/42"?Â?Ã?úï???*â??Ã??
Ã?Ã?1(c)Ã??*a×Â?3Ã?DÃ?1/4ˉ?ï? ?ï???Ã?oÃ-à à DÃ-Î?????Ã?ï??Â? Type
à à DÃ-ï??(c)?ï???Ã?Ã??ê1Ã??*â??Ã???ˉÃ??â??â??1/2Â?à à DÃ-Î??êÎ?à yï???
1/2"Ã Ã DÃ-Â?Ã??Â?Î?1/2??Ã?D???Ã?ï???"Ã?â??Ã???Ã?D???Ã?"?Ã??Ã Ã DÃ-2ï? Î?Ã?Ã????
*1/2Â?Â?"Ã?*??ê??×???oÃ-ê?D?,?Ã??ú?Ã??? ??1/4? using System.Reflection?ï?? *â??Ã??Î??×Ã?Ã??ï??o
1.ê1Ã??*â??Ã???Ã?Ã???ˉÃ??â??â??1/2Â?à à DÃ-Î??êÎ?à yï???Ã?"oÃ?1/2"êÎ?à y?Ã?Â
???Â?Î?1/2??Ã?DÎ?????Ã?"Ã?â??Ã???Ã?D???Ã??D"?Ã??Ã Ã DÃ-?ï??
2.Ã?|Ã??3Ã?DÃ?DÃ?Ã?a?ú??DDêÂ?â??Ã??3??Ã???Â?Î??3Ã?DÃ?1/4ˉ?D??Ã??Ã???Â?Î??à Ã
DÃ-ï???Ã??â??â??1/2Â?Ã???Â?à à DÃ-Î??êÎ?à y?ï??
3. ?yê?Ã?Ã??a*â??Ã??Î??Ã??D?ï??Â??ˉÃ??â??â??1/2Â?êÎ?à yï??(c)?Ã?Ã???ú?à ???ï
???Ã??(r)1/4?1/4?Ã?Ã?â??ú??Î??1/2??1/4o?*1/2??ï???à ?Ã??
*â??Ã???Ã?Ã???eÎ?1/2Ã??oÃ?â??Ã?Î????Ã???ï??Î?"ê?*â??Ã??Ã?2Ã?D?Ã?Î??Â?×??ï???3/
4Ã-ê?Ã?aÃ???tÃ?Ã?D??Ã??aâ??ú1/4?ï??????Ã?Ã?DD(c)D? ?ï? à ?Ã??Â?â??Ã??ê??TÂ?Â?
Î??Î?1/2Î???ï???úê1Ã??êÂ?Ã?a?Ã?3/4Y?Ã???1/2?DD?????ï???Ã???Ã-Â?1y??Ã-1/4Î??2?â????ï??DÃ-â??Ã?1/4Ã??Ã??Ã? "Ã??Ã??Ã?Â?×?Ã?Î??"ú???ï??
Ã??Ã??1?Ã?ú*â??Ã???aê?Ã??1/2a?(r)oÃ?ï????úÃ-Â?1yâ??ú??â??Ã?1/4Ã??Ã??Ã?"Ã??Ã?o?Î??à Ã-1/2a*â??Ã??ï??o ê×?Ã??ú Vs2008 ?Dâ??â??1/2Â?Ã?"??à à DÃ-ï????Ã????a
ReflectDemo à à ?a?ï??2ï? Ã?Ã-1/4Ã?Ã?"?????a ReflectTest Î??à Ã
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SKY.ReflectDemo { public class ReflectTest : IPerson { private string _name; private int _age; public string Name { get { return this._name; } set { this._name = value; } } public int Age { get { return this._age; } set { this._age = value; } } public ReflectTest(string name, int age) { this._name = name; this._age = age; } public string WelcomeInfo(string name) { return "welcome come here " + name; } public static string WriteStaticInfo(string name) { return "welcome come here static" + name; } public static string WriteStaticNoParam() { return "Static and No parma";
} public void Show(string info) { Console.WriteLine(info); } public ReflectTest() { } public string WriteNoPara() { return "??ê1Ã??Î??ê??T2?Î??*1/2*Â?"; } private string WritePrivate() { return "?1/2Ã?Dà à DÃ-Î??*1/2*Â?"; } #region IPerson 3Ã??Â? public int Add() { return Age; } #endregion } public interface IPerson { /// /// Ã?Ã-1/4Ã????Ã? /// /// int Add();
} } Ã?"oÃ??Ã?â??â??1/2Â?Ã?"???Ã-"Â???3Ã?DÃ?ï??oà yÃ??
clientTestDemo,Ã?Ã-1/4Ã?????D?1/2Â?Î??3Ã?DÃ?1/4ˉÃ?yÃ??ï???Ã?yÃ???Ã??? ??1/4? using System.Reflection class Program { delegate string TestDelegate(string name); static void Main(string[] args)
{ //*********************Ã?yÎ?1/43Ã?DÃ?1/4ˉ************************// //Assembly ass = Assembly.LoadFrom(("SKY.ReflectDemo.dll")); //Assembly
ass=Assembly.GetAssembly(typeof(SKY.ReflectDemo.ReflectTest); Assembly ass = Assembly.Load("SKY.ReflectDemo");//1|?Ã?Ã-?Ã?? Console.Write(ass.FullName+"\n"); //*********************??ê3/4?? dll ???Ã?Ã?DÎ??à à ***************// foreach (Type type in ass.GetTypes()) { Console.Write(type.Name+"\n"); } //*********************??ê3/4?? dll ?????Â?à à **************// Type itype = ass.GetType("SKY.ReflectDemo.ReflectTest"); Console.Write(itype.Name); Type itype1 = typeof(SKY.ReflectDemo.ReflectTest);
//********************?Ã?Ã?D?ï???Ã?***********************// foreach (Module temp in ass.GetModules()) {
Console.WriteLine(temp.Assembly.FullName); }
//********************â??â??1/2Â???à à Î??êÎ?à y,oÃ???Î??
param ?aÃ?D2?11?Ã?oˉêyÎ??2? êy******************// object[] param = { "test",
30 };//11?�oˉêy2?êy SKY.ReflectDemo.ReflectTest temp1 =
(SKY.ReflectDemo.ReflectTest)Activator.CreateInstance(itype1, param); // SKY.ReflectDemo.ReflectTest temp1 =
(SKY.ReflectDemo.ReflectTest)ass.CreateInstance("SKY.ReflectDemo.ReflectTest "); Console.WriteLine(temp1.WriteNoPara());
//******************************??ê3/4?Ã?Ã?DÎ??12Ã?D*1/2Â?Â?
************************************// MethodInfo[] methods = itype.GetMethods(); Console.WriteLine("??ê3/4?Ã?Ã?DÎ??12Ã?D*1/2*Â?"); foreach (MethodInfo temp in methods) { Console.WriteLine(temp.Name); }
//******************************??ê3/4�??�*1/2*�
************************************//
Console.WriteLine("??ê3/4Ã???Â?*1/2Â?Â?"); MethodInfo method1 = itype.GetMethod("WriteStaticInfo"); //â???2?Î??3/42Ã?? *1/2*Â?
Console.WriteLine(method1.Name); object[] param1 =
{ "ê1Ã??Î??ê?3/42Ã??*1/2Â?Â?" };
string s1 = (string)method1.Invoke(null, param1);
Console.WriteLine("?â??DDoÃ?:"+s1+"\n"); MethodInfo method2 =
itype.GetMethod("WriteStaticNoParam");//?T2?3/42 Ã??*1/2*Â?
Console.WriteLine(method2.Name); string s2 = method2.Invoke(null, null) as string; Console.WriteLine("?â??DDoÃ?:" + s2 + "\n"); MethodInfo method3 =
itype.GetMethod("WelcomeInfo");//â???2?Î??*?3/42Ã?? *1/2Â?Â?
Console.WriteLine(method3.Name); object[] param2 =
{ "ê1Ã??Î??ê?â???Ã?D2?êyÎ??*?3/42Ã??*1/2*Â?" }; string s3 =
(string)method3.Invoke(temp1, param2); Console.WriteLine("?â??DDoÃ?:" + s3 + "\n");
MethodInfo method4 = itype.GetMethod("WriteNoPara");//?T2?*?3/42Ã??*1/2Â?Â? Console.WriteLine(method4.Name); string s4 = method4.Invoke(temp1, null) as string; Console.WriteLine("?â??DDoÃ?:" + s4 + "\n"); MethodInfo method5 =
itype.GetMethod("WritePrivate", BindingFlags.NonPublic |
BindingFlags.Instance);//?T2??1/2Ã?D*?3/42Ã??*1/2Â?Â?
Console.WriteLine(method5.Name); string s5 = method5.Invoke(temp1, null) as string;
Console.WriteLine("?â??DDoÃ?:" + s5 + "\n"); MethodInfo method6 =
itype.GetMethod("Show");//Â?Î?"?Ã Ã DÃ-?a void Î??*1/2Â?Â?
Console.WriteLine(method6.Name); object[]param3={"returnType is void"}; string s6 = method6.Invoke(temp1, param3) as string; Console.WriteLine(s6);
//***********************************??ê3/4?��Dê?D?
*********************************88// Console.WriteLine("??ê3/4?��Dê?D?"); PropertyInfo[] pros = itype.GetProperties(BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance); foreach (PropertyInfo temp in pros) { Console.WriteLine(temp.Name); }
//***********************************??ê3/4�??�ê?D?
*********************************88// Console.WriteLine("??ê3/4�??�ê?D?"); PropertyInfo proName = itype.GetProperty("Name"); proName.SetValue(temp1, "testName",null); Console.WriteLine(proName.GetValue(temp1, null)); //***********************************??ê3/411?�oˉêy
*********************************88// Console.WriteLine("??ê3/411?�oˉêy"); ConstructorInfo[] cons = itype.GetConstructors(); foreach (ConstructorInfo t in cons) { Console.WriteLine(t.ToString()); }
//***********************************??ê3/4�??�11?�oˉêyD?ê1/2 *********************************88//
Console.WriteLine("??ê3/4Ã???Â?11?Ã?oˉêyD?ê1/2"); ConstructorInfo con1= itype.GetConstructor(new Type[] { typeof(string), typeof(int) }); Console.WriteLine(con1); ConstructorInfo con2 = itype.GetConstructor(new Type[] { }); Console.WriteLine(con2); //**************************************?ˉÃ-DÎ??ê1Ã?? *********************************************//
Console.WriteLine("?ˉÃ-DÎ??ê1Ã??"); TestDelegate td =
(TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), temp1, "WelcomeInfo");//*?3/42Ã??*1/2*Â?
Console.WriteLine(td("ê1Ã??Î??ê?â???Ã?D2?êyÎ??*?3/42Ã??*1/2Â?Â?")); TestDelegate td2 = Delegate.CreateDelegate(typeof(TestDelegate), method1) as TestDelegate;//3/42Ã??*1/2*Â? Console.WriteLine(td2("ê1Ã??3/42Ã??*1/2*Â?")); // TestDelegate td1 = new TestDelegate(SKY.ReflectDemo.ReflectTest.WriteStaticInfo); // Console.WriteLine(td1("ê1Ã??Î??ê?â???Ã?D2?êyÎ??*?3/42Ã??*1/2Â?Â? 222")); Ã-?Ã?? ??DD1/2Ã?1?Ã????ï??o
Ã?Dê2?â??2"??ï?????â??Ã?1/4Ã??ú?à ??Ã?Â??ï??
本文档为【C#反射机制】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。