Csharp/C Sharp by API/System/Type
Содержание
- 1 Type.BaseType
- 2 Type.ContainsGenericParameters
- 3 Type.FullName
- 4 Type.GetConstructor
- 5 Type.GetCustomAttributes
- 6 Type.GetEvents()
- 7 Type.GetFields()
- 8 Type.GetGenericArguments()
- 9 Type.GetGenericTypeDefinition()
- 10 Type.GetInterfaces()
- 11 Type.GetMethods()
- 12 Type.GetMethod(String methodName);
- 13 Type.GetProperties()
- 14 Type.GetType(String typeName)
- 15 Type.GetType(String typeName, true)
- 16 Type.GetType(String typeName, true, true);
- 17 Type.IsAbstract
- 18 Type.IsClass
- 19 Type.IsCOMObject
- 20 Type.IsEnum
- 21 Type.IsGenericTypeDefinition
- 22 Type.IsSealed
- 23 Type.MakeGenericType
- 24 Type.Name
- 25 Type.ReflectionOnlyGetType
- 26 Type.UnderlyingSystemType
Type.BaseType
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);
}
}
Type.ContainsGenericParameters
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);
}
}
Type.FullName
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);
}
}
Type.GetConstructor
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);
}
}
Type.GetCustomAttributes
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);
}
}
}
Type.GetEvents()
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);
}
}
}
Type.GetFields()
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);
}
}
Type.GetGenericArguments()
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);
}
}
}
}
Type.GetGenericTypeDefinition()
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()));
}
}
Type.GetInterfaces()
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);
}
}
}
Type.GetMethods()
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);
}
}
Type.GetMethod(String methodName);
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);
}
}
Type.GetProperties()
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() {}
}
}
Type.GetType(String typeName)
using System;
using System.Text;
class MainClass
{
public static void Main()
{
Type t2 = Type.GetType("System.String");
}
}
Type.GetType(String typeName, true)
using System;
using System.Text;
class MainClass
{
public static void Main()
{
Type t3 = Type.GetType("System.String", true);
}
}
Type.GetType(String typeName, true, true);
using System;
using System.Text;
class MainClass
{
public static void Main()
{
Type t4 = Type.GetType("system.string", true, true);
}
}
Type.IsAbstract
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);
}
}
Type.IsClass
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);
}
}
Type.IsCOMObject
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);
}
}
Type.IsEnum
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);
}
}
}
}
Type.IsGenericTypeDefinition
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]);
}
}
Type.IsSealed
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);
}
}
Type.MakeGenericType
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);
}
}
Type.Name
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));
}
}
Type.ReflectionOnlyGetType
using System;
using System.Reflection;
class ReflectOnlyType {
static void Main() {
Type zType = Type.ReflectionOnlyGetType("ReflectOnlyType", false, false);
Console.WriteLine(zType.Name);
}
}
Type.UnderlyingSystemType
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);
}
}