Csharp/C Sharp/Reflection/MemberInfo — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:38, 26 мая 2010

Deeper Reflection:Finding Members

<source lang="csharp"> /* A Programmer"s Introduction to C# (Second Edition) by Eric Gunnerson Publisher: Apress L.P. ISBN: 1-893115-62-3

  • /

// 36 - Deeper into C#\Deeper Reflection\Finding Members // copyright 2000 Eric Gunnerson using System; using System.Reflection; class MyClass {

   MyClass() {}
   static void Process() {}
   public int DoThatThing(int i, Decimal d, string[] args)
   {
       return 55;
   }
   public int        value = 0;
   public float        log = 1.0f;
   public static int    value2 = 44;

} public class DeeperReflectionFindingMembers {

   public static void Main(String[] args)
   {
       // iterate through the fields of the class
       Console.WriteLine("Fields of MyClass");
       Type t = typeof (MyClass);
       foreach (MemberInfo m in t.GetFields())
       {
           Console.WriteLine("{0}", m);
       }
       
       // and iterate through the methods of the class
       Console.WriteLine("Methods of MyClass");
       foreach (MethodInfo m in t.GetMethods())
       {
           Console.WriteLine("{0}", m);
           foreach (ParameterInfo p in m.GetParameters())
           {
               Console.WriteLine("  Param: {0} {1}",
               p.ParameterType, p.Name);
           }
       }
   }

}

      </source>


Get MethodInfo and MemberInfo

<source lang="csharp"> using System; using System.Reflection; public class Apple {

   public int nSeeds;
   public void Ripen() {
   }

} public class TypeOfApp {

   static void Main(string[] args) {
       Type t = typeof(Apple);
       string className = t.ToString();
       Console.WriteLine(className);
       Console.WriteLine(className);
       MethodInfo[] methods = t.GetMethods();
       foreach (MethodInfo method in methods) {
           Console.WriteLine(method.ToString());
       }
       Console.WriteLine(className);
       MemberInfo[] allMembers = t.GetMembers();
       foreach (MemberInfo member in allMembers) {
           Console.WriteLine(member.ToString());
       }
   }

}

</source>


Get variable base type and public numbers

<source lang="csharp"> using System; using System.Text; using System.Windows.Forms; using System.Reflection; class MainClass {

   static void Main() {
       Type t = typeof(double);
       Console.WriteLine("Type Name:  " + t.Name);
       Console.WriteLine("Full Name:  " + t.FullName);
       Console.WriteLine("Namespace:  " + t.Namespace);
       Type tBase = t.BaseType;
       if (tBase != null)
           Console.WriteLine("Base Type:" + tBase.Name);
       Type tUnderlyingSystem = t.UnderlyingSystemType;
       if (tUnderlyingSystem != null)
           Console.WriteLine("UnderlyingSystem Type:" + tUnderlyingSystem.Name);
       Console.WriteLine("\nPUBLIC MEMBERS:");
       MemberInfo[] Members = t.GetMembers();
       foreach (MemberInfo NextMember in Members) {
           Console.WriteLine(NextMember.DeclaringType + " " + NextMember.MemberType + " " + NextMember.Name);
       }
   }

}

</source>