Csharp/C Sharp by API/System/Type

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

Type.BaseType

<source lang="csharp"> using System; class MainClass {

 static void Main(string[] args)
 {
   Object cls1 = new Object();
   System.String cls2 = "Test String" ;
   Type type1 = cls1.GetType();
   Type type2 = cls2.GetType();
   // Object class output
   Console.WriteLine(type1.BaseType);
   Console.WriteLine(type1.Name);
   Console.WriteLine(type1.FullName);
   Console.WriteLine(type1.Namespace);
   // string output
   Console.WriteLine(type2.BaseType);
   Console.WriteLine(type2.Name);
   Console.WriteLine(type2.FullName);
   Console.WriteLine(type2.Namespace);
 } 

}



 </source>


Type.ContainsGenericParameters

<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Reflection; using System.Reflection.Emit; public class MainClass {

   public static void Main()
   {
       Type listType = typeof(List<>);
       Console.WriteLine("List<>: {0}, {1}",listType.IsGenericType, listType.ContainsGenericParameters);
       Type listIntType = typeof(List<int>);
       Console.WriteLine("List<int>: {0}, {1}",listIntType.IsGenericType, listIntType.ContainsGenericParameters);
   }

}



 </source>


Type.FullName

<source lang="csharp"> using System; class MainClass {

 static void Main(string[] args)
 {
   Object cls1 = new Object();
   System.String cls2 = "Test String" ;
   Type type1 = cls1.GetType();
   Type type2 = cls2.GetType();
   // Object class output
   Console.WriteLine(type1.BaseType);
   Console.WriteLine(type1.Name);
   Console.WriteLine(type1.FullName);
   Console.WriteLine(type1.Namespace);
   // string output
   Console.WriteLine(type2.BaseType);
   Console.WriteLine(type2.Name);
   Console.WriteLine(type2.FullName);
   Console.WriteLine(type2.Namespace);
 } 

}



 </source>


Type.GetConstructor

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

   public static void Main ()
   {
       Type type = typeof(StringBuilder);
       Type[] argTypes = new Type[] { typeof(System.String), typeof(System.Int32) };
       ConstructorInfo cInfo = type.GetConstructor(argTypes);
       object[] argVals = new object[] { "Some string", 30 };
       // Create the object and cast it to StringBuilder.
       StringBuilder sb = (StringBuilder)cInfo.Invoke(argVals);
       Console.WriteLine(sb);
   }

}



 </source>


Type.GetCustomAttributes

<source lang="csharp"> using System; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] public class AuthorAttribute : System.Attribute {

   private string company; 
   private string name;
   public AuthorAttribute(string name)
   {
       this.name = name;
       company = "";
   }
   public string Company
   {
       get { return company; }
       set { company = value; }
   }
   public string Name
   {
       get { return name; }
   }

}

[assembly: Author("Tom", Company = "Ltd.")] [Author("Tom", Company = "Abc Ltd.")] class SomeClass { } [Author("Lena")] public class SomeOtherClass { }

[Author("FirstName")] [Author("Jack", Company = "Ltd.")] class MainClass {

   public static void Main()
   {
       Type type = typeof(MainClass);
       object[] attrs = type.GetCustomAttributes(typeof(AuthorAttribute), true);
       foreach (AuthorAttribute a in attrs)
       {
           Console.WriteLine(a.Name + ", " + a.rupany);
       }
   }

}



 </source>


Type.GetEvents()

<source lang="csharp">

using System; using System.Reflection; class MainClass {

   public static void ShowEvents(Type t) {
       EventInfo[] events = t.GetEvents();
       Console.WriteLine("Implemented Events");
       foreach (EventInfo e in events) {
           Console.WriteLine("Event name: {0}", e.Name);
           Console.WriteLine("Multicast: {0}", e.IsMulticast ? "Yes" : "No");
           Console.WriteLine("Member Type {0}", e.MemberType.ToString());
       }
   }
   public static void ShowTypes(string name, Assembly assembly) {
       Type[] typeArray = assembly.GetTypes();
       Console.WriteLine("Assembly Name: {0}", name);
       foreach (Type type in typeArray) {
           if (type.IsClass) {
               ShowEvents(type);
           } 
       }
   }
   public static void Main(string[] args) {
       for (int i = 0; i < args.Length; ++i) {
           // Get the assemble object (from System.Reflection)
           Assembly assembly = Assembly.LoadFrom(args[0]);
           ShowTypes(args[0], assembly);
       }
   }

}


 </source>


Type.GetFields()

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

  public int Field1;
  public int Field2;
  public void Method1() { }
  public int  Method2() { return 1; }

} class MainClass {

  static void Main()
  {
     Type t = typeof(MyClass);
     FieldInfo[] fi = t.GetFields();
     foreach (FieldInfo f in fi)
        Console.WriteLine("Field : {0}", f.Name);
  }

}



 </source>


Type.GetGenericArguments()

<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Reflection; using System.Reflection.Emit; public class MainClass {

   public static void Main()
   {
       PrintTypeParams(typeof(List<>));
       PrintTypeParams(typeof(List<int>));
       PrintTypeParams(typeof(Nullable<>));
   }
   private static void PrintTypeParams(Type t)
   {
       Console.WriteLine(t.FullName);
       foreach (Type ty in t.GetGenericArguments())
       {
           Console.WriteLine(ty.FullName);
           Console.WriteLine(ty.IsGenericParameter);
           if (ty.IsGenericParameter)
           {
               Type[] constraints = ty.GetGenericParameterConstraints();
               foreach (Type c in constraints)
                   Console.WriteLine(c.FullName);
           }
       }
   }

}


 </source>


Type.GetGenericTypeDefinition()

<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Reflection; using System.Reflection.Emit; public class MainClass {

   public static void Main()
   {
       Console.WriteLine(typeof(List<>).Equals(typeof(List<int>).GetGenericTypeDefinition()));
   }

}



 </source>


Type.GetInterfaces()

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

   public static void ShowInterfaces(Type t) {
       Type[] interfaces = t.GetInterfaces();
       Console.WriteLine("Implemented Interfaces");
       foreach (Type type in interfaces) {
           Console.WriteLine("Interface : {0}", type.FullName);
           if (type.IsPublic)
               Console.WriteLine("Scope: Public");
           else
               Console.WriteLine("Scope: Private");
       }
   }
   public static void ShowTypes(string name, Assembly assembly) {
       Type[] typeArray = assembly.GetTypes();
       Console.WriteLine("Assembly Name: {0}", name);
       foreach (Type type in typeArray) {
           if (type.IsClass) {
               ShowInterfaces(type);
           } 
       }
   }
   public static void Main(string[] args) {
       for (int i = 0; i < args.Length; ++i) {
           // Get the assemble object (from System.Reflection)
           Assembly assembly = Assembly.LoadFrom(args[0]);
           ShowTypes(args[0], assembly);
       }
   }

}


 </source>


Type.GetMethods()

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

  public int Field1;
  public int Field2;
  public void Method1() { }
  public int  Method2() { return 1; }

} class MainClass {

  static void Main()
  {
     Type t = typeof(MyClass);
     MethodInfo[] mi = t.GetMethods();
     foreach (MethodInfo m in mi)
        Console.WriteLine("Method: {0}", m.Name);
  }

}



 </source>


Type.GetMethod(String methodName);

<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Reflection; using System.Reflection.Emit; public class MainClass {

   public static void Main()
   {
       Type typeInfo = typeof(object);
       MethodInfo methInfo = typeInfo.GetMethod("ToString");
       Console.WriteLine("Info: {0}", methInfo);
   }

}



 </source>


Type.GetProperties()

<source lang="csharp"> using System; using System.Reflection;

public class Test {

   public static void Main(string[] args)
   {
   TheType.MyClass aClass = new TheType.MyClass();
   Type t = aClass.GetType();
   PropertyInfo[] pi = t.GetProperties();
   foreach(PropertyInfo prop in pi)
     Console.WriteLine("Prop: {0}",  prop.Name);
   }

}

namespace TheType {

   public interface IFaceOne {
     void MethodA();
   }
   
   public interface IFaceTwo {
     void MethodB();
   }
   
   public class MyClass: IFaceOne, IFaceTwo {
     public int myIntField;
     public string myStringField;
       private double myDoubleField = 0;
       
       
       public double getMyDouble(){
         return myDoubleField;
       }
       
     public void myMethod(int p1, string p2)
     {
     }
   
     public int MyProp
     {
       get { return myIntField; }
       set { myIntField = value; }
     }
   
     public void MethodA() {}
     public void MethodB() {}
   }

}


 </source>


Type.GetType(String typeName)

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

   public static void Main()
   {
       Type t2 = Type.GetType("System.String");
   }

}



 </source>


Type.GetType(String typeName, true)

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

   public static void Main()
   {
       Type t3 = Type.GetType("System.String", true);
   }

}



 </source>


Type.GetType(String typeName, true, true);

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

   public static void Main()
   {
       Type t4 = Type.GetType("system.string", true, true);
   }

}



 </source>


Type.IsAbstract

<source lang="csharp"> using System; using System.Reflection; public interface IFaceOne {

 void MethodA();

} public interface IFaceTwo {

 void MethodB();

} public class MyClass: IFaceOne, IFaceTwo {

 public enum MyNestedEnum{}
 
 public int myIntField;
 public string myStringField;
 public void myMethod(int p1, string p2)
 {
 }
 public int MyProp
 {
   get { return myIntField; }
   set { myIntField = value; }
 }
 void IFaceOne.MethodA(){}
 void IFaceTwo.MethodB(){}

} public class MainClass {

 public static void Main(string[] args)
 {
   MyClass f = new MyClass();
   Type t = f.GetType();
   Console.WriteLine("Full name is: {0}", t.FullName);
   Console.WriteLine("Base is: {0}", t.BaseType);
   Console.WriteLine("Is it abstract? {0}", t.IsAbstract);
   Console.WriteLine("Is it a COM object? {0}", t.IsCOMObject);
   Console.WriteLine("Is it sealed? {0}", t.IsSealed);
   Console.WriteLine("Is it a class? {0}", t.IsClass);
   
 }

}



 </source>


Type.IsClass

<source lang="csharp"> using System; using System.Reflection; public interface IFaceOne {

 void MethodA();

} public interface IFaceTwo {

 void MethodB();

} public class MyClass: IFaceOne, IFaceTwo {

 public enum MyNestedEnum{}
 
 public int myIntField;
 public string myStringField;
 public void myMethod(int p1, string p2)
 {
 }
 public int MyProp
 {
   get { return myIntField; }
   set { myIntField = value; }
 }
 void IFaceOne.MethodA(){}
 void IFaceTwo.MethodB(){}

} public class MainClass {

 public static void Main(string[] args)
 {
   MyClass f = new MyClass();
   Type t = f.GetType();
   Console.WriteLine("Full name is: {0}", t.FullName);
   Console.WriteLine("Base is: {0}", t.BaseType);
   Console.WriteLine("Is it abstract? {0}", t.IsAbstract);
   Console.WriteLine("Is it a COM object? {0}", t.IsCOMObject);
   Console.WriteLine("Is it sealed? {0}", t.IsSealed);
   Console.WriteLine("Is it a class? {0}", t.IsClass);
   
 }

}



 </source>


Type.IsCOMObject

<source lang="csharp"> using System; using System.Reflection; public interface IFaceOne {

 void MethodA();

} public interface IFaceTwo {

 void MethodB();

} public class MyClass: IFaceOne, IFaceTwo {

 public enum MyNestedEnum{}
 
 public int myIntField;
 public string myStringField;
 public void myMethod(int p1, string p2)
 {
 }
 public int MyProp
 {
   get { return myIntField; }
   set { myIntField = value; }
 }
 void IFaceOne.MethodA(){}
 void IFaceTwo.MethodB(){}

} public class MainClass {

 public static void Main(string[] args)
 {
   MyClass f = new MyClass();
   Type t = f.GetType();
   Console.WriteLine("Full name is: {0}", t.FullName);
   Console.WriteLine("Base is: {0}", t.BaseType);
   Console.WriteLine("Is it abstract? {0}", t.IsAbstract);
   Console.WriteLine("Is it a COM object? {0}", t.IsCOMObject);
   Console.WriteLine("Is it sealed? {0}", t.IsSealed);
   Console.WriteLine("Is it a class? {0}", t.IsClass);
   
 }

}



 </source>


Type.IsEnum

<source lang="csharp">

using System; using System.Collections; using System.Reflection; public class MainClass{

   public static void Main(){
       Assembly LoadedAsm = Assembly.LoadFrom("yourName");
       Console.WriteLine(LoadedAsm.FullName);
       Type[] LoadedTypes = LoadedAsm.GetTypes();
       if (LoadedTypes.Length == 0) {
           Console.WriteLine("No Types!");
       } else {
           foreach (Type t in LoadedTypes) {
               if (t.IsPublic) {
                   Console.WriteLine("Public ");
               } else if (t.IsNestedPublic) {
                   Console.WriteLine("Public Nested ");
               } else {
                   Console.WriteLine("Not Public ");
               }
               if (t.IsEnum) {
                   Console.WriteLine("Enum: ");
               } else if (t.IsValueType) {
                   Console.WriteLine("Struct: ");
               } else {
                   Console.WriteLine("Class: ");
               }
               Console.WriteLine("{0}", t.FullName);
           }
       }
   }

}


 </source>


Type.IsGenericTypeDefinition

<source lang="csharp"> using System; using System.Reflection; public class MyClass<T, V> {

   public T membera;

} public class XClass {

   public void MethodA<T>() {
   }

} class Starter {

   static void Main() {
       Type[] types = { typeof(MyClass<,>), typeof(MyClass<int, int>) };
       bool[,] bresp = { {types[0].IsGenericType,
                              types[0].IsGenericTypeDefinition},
                             {types[1].IsGenericType,
                              types[1].IsGenericTypeDefinition}};
       Console.WriteLine("Is MyClass<,> a generic type? " + bresp[0, 0]);
       Console.WriteLine("Is MyClass<,> open? " + bresp[0, 1]);
       Console.WriteLine("Is MyClass<int,int> a generic type? " + bresp[1, 0]);
       Console.WriteLine("Is MyClass<int,int> open? " + bresp[1, 1]);
       Type tObj = typeof(XClass);
       MethodInfo method = tObj.GetMethod("MethodA");
       bool[] bMethod ={method.IsGenericMethod, method.IsGenericMethodDefinition};
       Console.WriteLine("Is XClass.MethodA<T> a generic method? " + bMethod[0]);
       Console.WriteLine("Is XClass.MethodA<T> open? " + bMethod[1]);
   }

}


 </source>


Type.IsSealed

<source lang="csharp"> using System; using System.Reflection; public interface IFaceOne {

 void MethodA();

} public interface IFaceTwo {

 void MethodB();

} public class MyClass: IFaceOne, IFaceTwo {

 public enum MyNestedEnum{}
 
 public int myIntField;
 public string myStringField;
 public void myMethod(int p1, string p2)
 {
 }
 public int MyProp
 {
   get { return myIntField; }
   set { myIntField = value; }
 }
 void IFaceOne.MethodA(){}
 void IFaceTwo.MethodB(){}

} public class MainClass {

 public static void Main(string[] args)
 {
   MyClass f = new MyClass();
   Type t = f.GetType();
   Console.WriteLine("Full name is: {0}", t.FullName);
   Console.WriteLine("Base is: {0}", t.BaseType);
   Console.WriteLine("Is it abstract? {0}", t.IsAbstract);
   Console.WriteLine("Is it a COM object? {0}", t.IsCOMObject);
   Console.WriteLine("Is it sealed? {0}", t.IsSealed);
   Console.WriteLine("Is it a class? {0}", t.IsClass);
   
 }

}



 </source>


Type.MakeGenericType

<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Reflection; using System.Reflection.Emit; public class MainClass {

   public static void Main()
   {
       Type listType = typeof(List<>);
       Type listOfIntType = listType.MakeGenericType(typeof(int));
       Console.WriteLine(listOfIntType.FullName);
   }

}



 </source>


Type.Name

<source lang="csharp"> using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text;

public class MainClass {

   public static void Main()
   {
       string s = "A string";
       Type t = s.GetType();
       Console.WriteLine(t.Name);              
       Console.WriteLine(t.Namespace);         
       Console.WriteLine(t.IsPublic);          
       Console.WriteLine(t == typeof(string)); 
   }

}



 </source>


Type.ReflectionOnlyGetType

<source lang="csharp">

using System; using System.Reflection; class ReflectOnlyType {

   static void Main() {
       Type zType = Type.ReflectionOnlyGetType("ReflectOnlyType", false, false);
       Console.WriteLine(zType.Name);
   }

}


 </source>


Type.UnderlyingSystemType

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

 public static void Main()
 {
   String myVar = "P";
   Type t = myVar.GetType();
   Console.WriteLine("Name : {0}",t.Name);
   Console.WriteLine("Underlying System Type : {0}",t.UnderlyingSystemType);
   Console.WriteLine("Is Class : {0}",t.IsClass);
 }

}


 </source>