使用反射查詢類的方法和字段
using System;
using System.Reflection;
namespace Refrect
{
/// <summary>
/// Class1 的摘要說明。
/// </summary>
class cleQueryMyClass
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Type t = typeof(Demo);
MethodInfo[] Methods = t.GetMethods();
System.Console.Write("Methods");
foreach(MethodInfo Method in Methods)
{
System.Console.Write("{0}/n",Method.Name);
}
System.Console.Write("Members");
MemberInfo[] Members = t.GetMembers();
foreach(MemberInfo Member in Members)
{
System.Console.Write("{0}/n",Member.Name);
}
System.Console.Write("Fields");
FieldInfo[] Fields = t.GetFields();
foreach(FieldInfo Field in Fields)
{
System.Console.Write("{0}/n",Field.Name);
}
}
}
class Demo
{
public int MemberOne = 1 ;
public int MemberTwo = 2;
public int MemberThree = 3 ;
private int MemberFour = 4 ;
private int MebmerFive = 5;
public int MethodOne()
{
return MemberOne + MemberTwo;
}
public int MethodTwo()
{
return MemberThree + MemberFour;
}
public int MethodThree()
{
return MebmerFive;
}
}
}
利用反射動態調用方法
using System;
using System.Reflection;
namespace Refrect
{
/// <summary>
/// Class1 的摘要說明。
/// </summary>
class UsingInvoke
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
object myObject = null;
// typeof創建一個string類的type類型對象myType
Type myType = typeof(string);
//調用其GetConstructor方法 取得一個包含所有String類定義的構造函數版本 myConsts包含了所有的構造函數版本
ConstructorInfo[] myConsts = myType.GetConstructors();
foreach (ConstructorInfo myConst in myConsts)
{
//取得版本構造函數需要的參數
ParameterInfo[] myPI = myConst.GetParameters();
if (myPI.Length == 2 && myPI[0].ParameterType.Name == "Char")
{
object[] objPara ={ 'S', 5 };
// 條件成立 調用 ConstructorInfo的Invoke方法
myObject = myConst.Invoke(objPara);
Console.WriteLine(myObject.ToString());
}
}
// 取得包含所有方法的數組對象myMIS
MethodInfo[] myMIS = myType.GetMethods();
foreach(MethodInfo myMI in myMIS)
{
ParameterInfo[] myPara = myMI.GetParameters();
if(myMI.Name=="ToLower"&&myPara.Length==0)
{
Console.WriteLine(myMI.Invoke(myObject,null));
}
}
Console.ReadLine();
}
}
}
使用IS運算符號
using System;
namespace UserIs
{
/// <summary>
/// clsIsOperator 的摘要說明。
/// </summary>
class clsIsOperator
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
clsTonePhone Cellular = new clsTonePhone("555-1212");
clsRotaryPhone GrandmasPhone = new clsRotaryPhone("555-5555");
Cellular.Dial("800-555-1212");
GrandmasPhone.Dial("800-555-1212");
System.Console.Write("/n/n About to Create a Polymorphic phone/n");
clsPhone Phone = Cellular;
Phone.Dial("800-555-1212");
if (Phone is clsTonePhone)
{
System.Console.Write("The Object is a tone phone/n");
}
Phone = GrandmasPhone;
Phone.Dial("800-555-1212");
if ( Phone is clsRotaryPhone)
{
System.Console.Write("The Object is a rotary phone/n");
}
}
}
class clsPhone
{
protected string Number;
public clsPhone(string number)
{
Number = number;
}
public virtual void Dial(string NumberToCall)
{
System.Console.Write("Beep,Beep: Tone Dial");
System.Console.Write("Calling{0}/n",NumberToCall);
}
}
class clsRotaryPhone : clsPhone
{
public clsRotaryPhone(string number) : base(number){}
public override void Dial(string NumberToCall)
{
System.Console.Write("In Rotary Dial,Calling {0}",NumberToCall);
}
}
class clsTonePhone : clsPhone
{
public clsTonePhone(string number) : base(number){}
}
}
類轉換和AS運算符
using System;
namespace UserIs
{
/// <summary>
/// clsIsOperator 的摘要說明。
/// </summary>
class clsIsOperator
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
clsPhone BasePhone = new clsPhone("555-1212");
clsRotaryPhone DerivedPhone = new clsRotaryPhone("555-5555");
DerivedPhone = BasePhone as clsRotaryPhone;
if ( DerivedPhone == null )
{
System.Console.Write("DerivedPhone is Null/n");
}
else
{
System.Console.Write("BasePhone is downcast/n");
}
try
{
DerivedPhone = (clsRotaryPhone)BasePhone;
}
catch(System.Exception ex)
{
System.Console.Write("Invalid cast exception caught/n");
}
BasePhone = DerivedPhone as clsPhone;
if ( BasePhone == null )
{
System.Console.Write("BasePhone is Null/n");
}
else
{
System.Console.Write("Base Phone has been upcate/n");
}
try
{
BasePhone = (clsPhone)DerivedPhone;
System.Console.Write("DerivedPhone again upcast/n");
}
catch(System.Exception ex)
{
System.Console.Write("Invalid cast exception caught/n");
}
}
}
class clsPhone
{
protected string Number;
public clsPhone(string number)
{
Number = number;
}
public virtual void Dial(string NumberToCall)
{
System.Console.Write("Beep,Beep: Tone Dial");
System.Console.Write("Calling{0}/n",NumberToCall);
}
}
class clsRotaryPhone : clsPhone
{
public clsRotaryPhone(string number) : base(number){}
public override void Dial(string NumberToCall)
{
System.Console.Write("In Rotary Dial,Calling {0}",NumberToCall);
}
}
}