C# 反射

C# 反射机制以及方法

 

 

目录:

一. 反射的主要特性

  1.反射中一个非常重要的类型就是 Type

    1)当没有对象的时候使用这种方式来获取某个类型的Type

    2)当已经获得对象后通过对象的GetType()方法来获取指定对象的类型的Type对象

  2.获取Person类中的所有的方法

  3.获取某个类型的所有属性

  4.获取类中的所有字段,私有字段无法获取

  5.获取所有成员,不包含私有成员

二. 反射动态加载程序集

  1.动态加载一个程序集

  2.获取刚刚加载的程序集中的所有的类型

    1)GetTypes()获取了所有的类型

    2)只获取那些public的类型

  3.获取程序集中某个类的Type

  4.动态调用类的方法

    1)调用无参数无返回值的方法

    2) 调用带参数,带返回值的方法

      1> 调用不带重载的方法

      2>调用带重载的方法

  5. 通过反射获取类的属性,并赋值

    1)获取Name属性

    2)为属性赋值

    3) 获取属性值

    4)获取方法并调用

  6.手动查找类型的构造函数,并且调用该构造函数来创建类型的对象

三. 其他的反射中的一些方法

  1. bool IsAssignableFrom(Type c) 判断当前的类型的变量是不是可以接受c类型变量的赋值

  2. bool IsInstanceOfType(object o):判断对象o是否是当前类的实例(当前类可以是o的类、父类、接口)

  3. bool IsSubclassOf(Type c):判断当前类是否是类c的子类

  4. IsAbstract 判断是否为抽象的,含接口

————————————————————————————————————————————————————————————————————————————

 

反射是什么?

反射:通过动态获取程序集,并获取其中的类型元数据,然后访问该类型的过程。

一. 反射的主要特性

在介绍反射的主要特性之前我们先建一个Person类(下面都是对Person类进行操作)

class Person
    {

        public int _height;

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

        public void Say()
        {
            Console.WriteLine("Hello everyone!");
        }


        public void SayMorning()
        {
            Console.WriteLine("Good morning everybody!");
        }

        //私有的
        void Do()
        {
            Console.WriteLine("Just do it!");
        }
    }

  

复制代码
    class Person
    {

        public int _height;

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

        public void Say()
        {
            Console.WriteLine("Hello everyone!");
        }


        public void SayMorning()
        {
            Console.WriteLine("Good morning everybody!");
        }

        //私有的
        void Do()
        {
            Console.WriteLine("Just do it!");
        }
    }
复制代码

1.反射中一个非常重要的类型就是 Type

获取Person类型的Type对象(Type对象中就是存放了一些关于某个类型的所有信息的内容。[某个类型的Type对象就是该类型“类型元数据”])

获取Type对象有两种方法:

1)当没有对象的时候使用这种方式来获取某个类型的Type
Type type = typeof(Person);

2)当已经获得对象后通过对象的GetType()方法来获取指定对象的类型的Type对象
Person p = new Person();
Type personType = p.GetType();


2.获取Person类中的所有的方法

(通过Type对象的GetMethods()可以获取指定类型的所有的方法其中包括编译器自动生成的方法以及从父类中继承来的方法,但是不包含private方法)
MethodInfo[] methods = personType.GetMethods();
for (int i = 0; i < methods.Length; i++)
{
Console.WriteLine(methods[i].Name);
}

 

3.获取某个类型的所有属性
PropertyInfo[] properties = personType.GetProperties();
for (int i = 0; i < properties.Length; i++)
{
Console.WriteLine(properties[i].Name);
}
Console.ReadKey();


4.获取类中的所有字段,私有字段无法获取
FieldInfo[] fields = personType.GetFields();
for (int i = 0; i < fields.Length; i++)
{
Console.WriteLine(fields[i].Name);
}
Console.ReadKey();


5.获取所有成员,不包含私有成员
MemberInfo[] members = personType.GetMembers();
for (int i = 0; i < members.Length; i++)
{
Console.WriteLine(members[i].Name);
}
Console.ReadKey();

二. 反射动态加载程序集

在接收发射动态加载程序集,先把程序级的代码贴出来(下面都是对程序集TestDll.dll进行操作)

namespace TestDll
{
    public class Class1
    {
    }

    class MyClass
    {
        public void English()
        {
            Console.WriteLine("Hi,English");
        }
    }

    public abstract class MyAbstractClass
    {

    }

    public static class MyStaticClass
    {
    }


    public class Person
    {
        public Person()
        {

        }

        public Person(string name, int age, string email)
        {
            this.Name = name;
            this.Age = age;
            this.Email = email;
        }

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

        public void GetNameValue()
        {
            Console.WriteLine(this.Name + "--" + this.Age + "--" + this.Email);
        }


        public void English()
        {
            Console.WriteLine("Hi,English");
        }

        public void China()
        {
            Console.WriteLine("你好,中国");
        }

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

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

    public class Student : Person, IFlyable
    {
        public string StudentNo { get; set; }

        #region IFlyable 成员

        public void Fly()
        {
            Console.WriteLine("I can Fly!");
        }

        #endregion
    }

    class Teacher : Person
    {

    }

    public delegate void MyDelegate();

    delegate void MyDelegate1();

    public enum GoodMan
    {
        高,
        富,
        帅
    }

    public interface IFlyable
    {
        void Fly();
    }
}

  

复制代码
namespace TestDll
{
    public class Class1
    {
    }

    class MyClass
    {
        public void English()
        {
            Console.WriteLine("Hi,English");
        }
    }

    public abstract class MyAbstractClass
    {

    }

    public static class MyStaticClass
    {
    }


    public class Person
    {
        public Person()
        {

        }

        public Person(string name, int age, string email)
        {
            this.Name = name;
            this.Age = age;
            this.Email = email;
        }

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

        public void GetNameValue()
        {
            Console.WriteLine(this.Name + "--" + this.Age + "--" + this.Email);
        }


        public void English()
        {
            Console.WriteLine("Hi,English");
        }

        public void China()
        {
            Console.WriteLine("你好,中国");
        }

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

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

    public class Student : Person, IFlyable
    {
        public string StudentNo { get; set; }

        #region IFlyable 成员

        public void Fly()
        {
            Console.WriteLine("I can Fly!");
        }

        #endregion
    }

    class Teacher : Person
    {

    }

    public delegate void MyDelegate();

    delegate void MyDelegate1();

    public enum GoodMan
    {
        高,
        富,
        帅
    }

    public interface IFlyable
    {
        void Fly();
    }
}
复制代码

1.动态加载一个程序集
Assembly assembly = Assembly.LoadFile(@"D:\TestDll\bin\Debug\TestDll.dll"); 

注意:这个地址是程序及所在的绝对地址 


2.获取刚刚加载的程序集中的所有的类型
assembly.GetType() 等价于 typeof(Assembly)
1)GetTypes()获取了所有的类型
Type[] types = assembly.GetTypes();

2)只获取那些public的类型
Type[] types = assembly.GetExportedTypes();
for (int i = 0; i < types.Length; i++)
{
Console.WriteLine(types[i].Name);
}

3.获取程序集中某个类的Type
如:只获取Person类的Type
GetType()方法有重载,选择第二个重载,参数表示是要获取的类型的“完全限定名称”,即:命名空间.类名
这里拿到了Type,其实就等价于typeof(Person)或者是:p.GetType();
Type personType = assembly.GetType("_02TestDll.Person");

获取所有的方法:personType.GetMethods();

4.动态调用类的方法

(借用上面获取的Person类的方法)

获取某个特定的方法(根据方法名):personType.GetMethod(); 

1)调用无参数无返回值的方法

MethodInfo method = personType.GetMethod("SayHi");
Console.WriteLine(method.Name);

//通过反射来创建一个Person类型的对象{其实就是通过Person的Type来创建一个Person对象}

object objPerson = Activator.CreateInstance(personType);

//调用这个方法
method.Invoke(objPerson, null);

2) 调用带参数,带返回值的方法

1> 调用不带重载的方法
//找到对应的方法
MethodInfo method = personType.GetMethod("Add");
object obj = Activator.CreateInstance(personType);
//调用
object result = method.Invoke(obj, new object[] { 102, 203 });
Console.WriteLine("调用Add方法的返回值结果是:{0}", result);
#endregion


2>调用带重载的方法

//找到对应的方法
MethodInfo method = personType.GetMethod("Add", new Type[] { typeof(int), typeof(int), typeof(int) });
object obj = Activator.CreateInstance(personType);

//调用
int r = (int)method.Invoke(obj, new object[] { 1, 2, 3 });
Console.WriteLine(r);

5. 通过反射获取类的属性,并赋值

(借用上面获取的Person类的方法)

1)获取Name属性
PropertyInfo property = personType.GetProperty("Name");
object obj = Activator.CreateInstance(personType);
2)为属性赋值
property.SetValue(obj, "张三", null);

3) 获取属性值
string name = property.GetValue(obj, null).ToString();
Console.WriteLine(name);

 

4)获取方法并调用

MethodInfo method = personType.GetMethod("GetNameValue");
method.Invoke(obj, null);
Console.ReadKey();


6.手动查找类型的构造函数,并且调用该构造函数来创建类型的对象

查找到了对应的构造函数,但是还没有调用
ConstructorInfo ctor = personType.GetConstructor(new Type[] { typeof(string), typeof(int), typeof(string) });

开始调用构造函数
object obj = ctor.Invoke(new object[] { "hpp", 16, "hpp@yahoo.com" });
Console.WriteLine(obj.ToString());

MethodInfo method = personType.GetMethod("GetNameValue");
method.Invoke(obj, null);
Console.ReadKey();

三. 其他的反射中的一些方法

//动态加载一个程序集
Assembly assembly = Assembly.LoadFile(@"D:\TestDll\bin\Debug\TestDll.dll");

//获取类的Type 
Type typePerson = assembly.GetType("TestDll.Person");

Type typeStudent = assembly.GetType("TestDll.Student");

Type typeIFlyable = assembly.GetType("TestDll.IFlyable");

1. bool IsAssignableFrom(Type c) 判断当前的类型的变量是不是可以接受c类型变量的赋值


//表示可以将Student类型赋值给Person类型,因为Student类型继承自Person类
bool b = typePerson.IsAssignableFrom(typeStudent); //true

//表示可以将Student类型赋值给IFlyable类型,因为Student类型继承自IFlyable接口

bool b = typeIFlyable.IsAssignableFrom(typeStudent);//true

//表示不可以将Person类型赋值给IFlyable类型,因为Person类型没有继承IFlyable接口

bool b = typeIFlyable.IsAssignableFrom(typePerson);//false

2. bool IsInstanceOfType(object o):判断对象o是否是当前类的实例(当前类可以是o的类、父类、接口)

//Person
object objPerson = Activator.CreateInstance(typePerson);
//Student
object objStudent = Activator.CreateInstance(typeStudent);

//当前类就是Person类

bool b = typePerson.IsInstanceOfType(objPerson);//true

//Suntent类是Person类的子类

bool b = typePerson.IsInstanceOfType(objStudent);//true

//person类不是Student的子类

bool b = typeStudent.IsInstanceOfType(objPerson);//false

3. bool IsSubclassOf(Type c):判断当前类是否是类c的子类

//Person
object objPerson = Activator.CreateInstance(typePerson);
//Student
object objStudent = Activator.CreateInstance(typeStudent);

//Suntent类是Person类的子类

bool b = typeStudent.IsSubclassOf(typePerson);//true

//person类不是Student的子类

 bool b = typePerson.IsSubclassOf(typeStudent);//false

//这个返回是false,只验证类与类之间的父子类关系,接口不包含。
bool b = typeStudent.IsSubclassOf(typeIFlyable);

4. IsAbstract 判断是否为抽象的,含接口

Type typeMyAbsClass = assembly.GetType("TestDll.MyAbstractClass");
Type typeMyStaticClass = assembly.GetType("TestDll.MyStaticClass");

Console.WriteLine(typePerson.IsAbstract);//false;
Console.WriteLine(typeStudent.IsAbstract);//false
Console.WriteLine(typeIFlyable.IsAbstract);//true
Console.WriteLine(typeMyAbsClass.IsAbstract);//true
Console.WriteLine(typeMyStaticClass.IsAbstract); //true
Console.ReadKey();

 

以上就是反射的一些介绍以及一些放大的基本用法······

posted @ 2021-02-01 19:07  左正  阅读(1598)  评论(0编辑  收藏  举报