反射

1  最基础的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace _05反射
{
class Program
{
static void Main(string[] args)
{
#region 通过反射获取成员

////反射中的重要类:Type
//Person p = new Person();

////这里可以理解为是,通过调用p对象的GetType()方法就能获取p对象所属的类型(Person),的类型元数据信息。
////Type type = p.GetType();

////不通过对象,也能获取类型的”类型元数据“
////Type type1 = typeof(Person);
////string s;
////s.GetType().BaseType.BaseType.BaseType.BaseType;
////type.GetEvents

////===============获取Person类型的所有方法=======
//Type typePerson = p.GetType();
//////获取了Person类型中的所有方法(没有获取私有方法)
////MethodInfo[] methods = typePerson.GetMethods();
////foreach (var item in methods)
////{
//// Console.WriteLine(item.Name);
////}

//////获取所有属性
////PropertyInfo[] properties = typePerson.GetProperties();
////foreach (PropertyInfo item in properties)
////{
//// Console.WriteLine(item.Name);
////}

//////获取所有的字段
////FieldInfo[] fields = typePerson.GetFields();
////foreach (FieldInfo item in fields)
////{
//// Console.WriteLine(item.Name);
////}

//////获取所有成员
////MemberInfo[] members = typePerson.GetMembers();
////foreach (MemberInfo item in members)
////{
//// Console.WriteLine(item.Name);
////}
//Console.ReadKey();

#endregion

#region 通过反射调用方法

////1.获取Person类的Type
//Type typePerson = typeof(Person);
////2.获取SayHi方法
//MethodInfo methodSayHi = typePerson.GetMethod("SayHi");

////2.1创建一个Person类型的对象
////通过反射动态创建一个Person类型的对象
//object obj = Activator.CreateInstance(typePerson);

////3.调用该方法
////3.1第一个参数表示SayHi方法调用时需要的一个对象,由于SayHi方法是属于Person类型的一个实例方法,所以需要创建一个Person类型的对象,传递进来。如果SayHi方法是一个静态方法,则直接传递一个null就好了。
//methodSayHi.Invoke(obj, null);
//Console.ReadKey();

//========================调重载方法Say(msg)=========================
////1.获取Person类的Type
//Type typePerson = typeof(Person);
////2.获取SayHi方法
//// MethodInfo methodSayHi = typePerson.GetMethod("Say", System.Type.EmptyTypes);
////通过指定不同的参数的Type来确定调用哪个重载
//MethodInfo methodSayHi = typePerson.GetMethod("Say", new Type[] { typeof(string) });

////2.1创建一个Person类型的对象
////通过反射动态创建一个Person类型的对象
//object obj = Activator.CreateInstance(typePerson);

////3.调用该方法
//methodSayHi.Invoke(obj, new object[] { "能调到我吗?" });
//Console.ReadKey();

//==============调一个带返回值的方法=================================
////1.获取Person类的Type
//Type typePerson = typeof(Person);

////2.1创建一个Person类型的对象
//object obj = Activator.CreateInstance(typePerson);

////2.2获取指定的方法
//MethodInfo method = typePerson.GetMethod("Add");

////3.调用该方法
//object objReturn = method.Invoke(obj, new object[] { 10, 20 });
//Console.WriteLine(objReturn);
//Console.ReadKey();

#endregion
}
}

public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Email
{
get;
set;
}

public bool _sex;
public string _address;

public void SayHi()
{
Console.WriteLine("Hi^_~");
}


public void Say()
{
Console.WriteLine("hello");
}
public void Say(string msg)
{
Console.WriteLine(msg);
}

public int Add(int n1, int n2)
{
return n1 + n2;
}
}
}

2  动态获取程序集内容

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _06MyClassLib1
{
public class Class1
{
}

class MyClass
{

}
public class MyClass1
{

}
public interface IInterface
{
void Show();
}

public enum MyEnum
{
男,

}
public struct MyStruct
{
public void Show()
{
Console.WriteLine("gogoggo");
}
}

public delegate void ShowDelegate();

public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Email
{
get;
set;
}
}

public class Chinese : Person
{

}

public class Japanese : IInterface
{

#region IInterface 成员

public void Show()
{
Console.WriteLine("shit!");
}

#endregion
}

public abstract class MyClass3 : IInterface
{

#region IInterface 成员

public void Show()
{
Console.WriteLine("抽象类的show方法");
}

#endregion
}


public static class MyClassStatic
{

}
}

--------------------------

在另一个类中调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace _07动态加载程序集并获取成员信息
{
class Program
{
static void Main(string[] args)
{
//1.动态加载程序集
Assembly asm = Assembly.LoadFile(@"C:Documents and SettingssteveMy DocumentsVisual Studio 2008ProjectsSln201301236MyClassLib1inDebug6MyClassLib1.dll");

//2.获取程序集中的所有的”类型“的Type
//Type[] types = asm.GetTypes();

////获取所有的public类型的Type
//Type[] types = asm.GetExportedTypes();
//foreach (Type item in types)
//{
// Console.WriteLine(item.Name);
//}

//3.根据类型名,获取指定的类型
Type typePerson = asm.GetType("_06MyClassLib1.Person");
Type typeChinese = asm.GetType("_06MyClassLib1.Chinese");
Type typeJapanese = asm.GetType("_06MyClassLib1.Japanese");
Type typeInterface = asm.GetType("_06MyClassLib1.IInterface");
Type typeMyClass3 = asm.GetType("_06MyClassLib1.MyClass3");
Type tpyeMyClassStatic = asm.GetType("_06MyClassLib1.MyClassStatic");

#region bool IsAssignableFrom(Type c):

//Console.WriteLine(typePerson.IsAssignableFrom(typeChinese));
//Console.WriteLine(typePerson.IsAssignableFrom(typeJapanese));
//Console.WriteLine(typeInterface.IsAssignableFrom(typeChinese));
//Console.WriteLine(typeInterface.IsAssignableFrom(typeJapanese));
//Console.ReadKey();

#endregion

#region bool IsInstanceOfType(object o)
//object person = Activator.CreateInstance(typePerson);
//object ch = Activator.CreateInstance(typeChinese);
//object jp = Activator.CreateInstance(typeJapanese);

//Console.WriteLine(typePerson.IsInstanceOfType(person));//true
//Console.WriteLine(typePerson.IsInstanceOfType(ch));//true
//Console.WriteLine(typePerson.IsInstanceOfType(jp));

#endregion

#region bool IsSubclassOf(Type c)

//Console.WriteLine(typeChinese.IsSubclassOf(typePerson));
//Console.WriteLine(typeJapanese.IsSubclassOf(typePerson));
////这个不考虑接口,只考虑是否是子类与父类的关系。
//Console.WriteLine(typeJapanese.IsSubclassOf(typeInterface));

#endregion

#region IsAbstract,判断是否为抽象的,含接口.只要不能被实例化就算

//Console.WriteLine(typePerson.IsAbstract);
//Console.WriteLine(typeChinese.IsAbstract);
//Console.WriteLine(typeJapanese.IsAbstract);
//Console.WriteLine(typeInterface.IsAbstract);
//Console.WriteLine(typeMyClass3.IsAbstract);
//Console.WriteLine(tpyeMyClassStatic.IsAbstract);
#endregion


//Console.WriteLine(typePerson.Name);
Console.ReadKey();
}
}
}

------------------------------------

了解
c#代码 编译 MSIL(中间语言)
MSIL --- 编译(JIT) cpu可执行的代码

程序集

反射
Assembly 程序集
GetExportedTypes() 获得程序集中的公共类型
GetTypes() 获得程序集中的所有类型
GetType(name) 根据类的全名称字符串 获得程序集中的类型
LoadFile(path) 动态从文件加载程序集
Type 数据类型的类型
获取Type
Type t = typeof(Person)
Person p = new Person();
Type t = p.GetType();
t.FullName 获取类型的全名称
t.IsAssignableFrom(t1) 判断t1类型的对象是否可以赋给t的对象
typyof(IPlugin).IsAssignableFrom(typeof(ShutDown))
t.IsInstanceOfType(object o) 判断o对象是否是t类型
t.IsSubclassOf(t1) 判断t是否是t1的子类
t.GetProperties() 获取t类型中的所有属性
t.GetMethods() 获取t类型中的所有方法
PropertyInfo
MethodInfo
Invoke(object o,object[] param) 动态调用方法
IsPublic
IsAbstract

//动态创建对象
object o = Activator.CreateInstance(typeof(Person));

原文地址:https://www.cnblogs.com/cdaq/p/3577352.html