型のメソッドやメンバーを調べる
class CastTest
{
class Person
{
public int ID { get; set; }
public string Name { get; set; }
public void TestMethod() { }
public void TestMethod2() { }
}
public CastTest()
{
int x = 15;
Person p = new Person();
//.NET Framework のメソッド GetType により型を得る
Console.WriteLine(x.GetType()); //System.Int32
Console.WriteLine(p.GetType()); //CSharpLearningClass.CastTest+Person
//typeof 演算子によって型を得る。受け取り側にはSystem.Typeオブジェクトを使用する
Type t = typeof(Person); //CSharpLearningClass.CastTest+Person
Console.WriteLine(t);
//型に含まれるメソッドを得る
System.Reflection.MethodInfo[] methodInfo = t.GetMethods();
foreach (var item in methodInfo){ Console.WriteLine(item); }
//Int32 get_ID()
//Void set_ID(Int32)
//System.String get_Name()
//Void set_Name(System.String)
//Void TestMethod()
//Void TestMethod2()
//System.String ToString()
//Boolean Equals(System.Object)
//Int32 GetHashCode()
//System.Type GetType()
//型に含まれるメンバーを得る
System.Reflection.MemberInfo[] memberInfo = t.GetMembers();
foreach (var item in memberInfo) { Console.WriteLine(item); }
//Void .ctor()
//Int32 ID
//System.String Name
}
}
最終更新:2012年05月30日 01:31