C# 五十六、反射

概念

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

在.NET中,反射可以实现从对象的外部来了解对象或程序集内部结构的功能,哪怕不知道这个对象或程序集是什么。

反射可以动态创建出对象并执行其中的方法。

反射是.NET中的重要机制,通过反射,可以在运行时获得程序或程序集中每一个类型(包括类、结构、委托、接口、枚举等)的成员和成员的信息。

有了反射,即可对每一个类型了如指掌。还可以直接创建对象,即使这个对象的类型在编译时还不知道。

新建类库 

解决方案--->添加--->新建项目--->类库(.NET Framework) 

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

namespace ClassLibrary1
{
    public class Class1
    {
        public Class1()
        {
            Console.WriteLine("构造函数");
        }
        public void Method()
        {
            Console.WriteLine("我的方法");
        }
    }
}

使用类库(不使用反射机制)

using ClassLibrary1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 class1 = new Class1();

            class1.Method();

            Console.ReadKey();
        }
    }
}
--->
构造函数
我的方法

使用反射

添加命名空间:using System.Reflection;

访问类库

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

namespace TestCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.Load("ClassLibrary1");
            Module[] modules = assembly.GetModules();
            foreach (Module module in modules)
            {
                Console.WriteLine(module.Name);
            }

            Console.ReadKey();
        }
    }
}
--->
ClassLibrary1.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace TestCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Assembly assembly = Assembly.Load("ClassLibrary1");
            Assembly assembly = Assembly.LoadFile(@"C:\Users\86515\Desktop\TestCSharp\ClassLibrary1\bin\Debug\ClassLibrary1.dll");
            Module[] modules = assembly.GetModules();
            foreach (Module module in modules)
            {
                Console.WriteLine(module.Name);
            }

            Console.ReadKey();
        }
    }
}

--->
ClassLibrary1.dll

获得类型

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

namespace TestCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.Load("ClassLibrary1");
            Type[] types = assembly.GetTypes();
            foreach (Type type in types)
            {
                Console.WriteLine(type.Name);
            }

            Type mtype = assembly.GetType("ClassLibrary1.Class1");

            Console.WriteLine(mtype.Name);
            
            Console.ReadKey();
        }
    }
}

--->
Class1
Class1

System.Type类对于反射起着核心的作用。但它是一个抽象的基类,Type有与每种数据类型对应的派生类,使用不同派生类的对象的方法、字段、属性等可以来查找有关该类型的所有信息。

获取一个给定类型的Type的三种方式:

Type t = typeof(string);
Console.WriteLine(t);
--->
System.String
string s = "aaa";
Type t = s.GetType();
Console.WriteLine(t);
--->
System.String
Type t = Type.GetType("System.String");
Console.WriteLine(t);

创建实例

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

namespace TestCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.Load("ClassLibrary1");
            Type mtype = assembly.GetType("ClassLibrary1.Class1");

            Object obj = Activator.CreateInstance(mtype);
            Console.WriteLine(obj);
            
            Console.ReadKey();
        }
    }
}

--->
构造函数
ClassLibrary1.Class1

获取方法并调用

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

namespace TestCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.Load("ClassLibrary1");
            Type mtype = assembly.GetType("ClassLibrary1.Class1");

            object obj = Activator.CreateInstance(mtype);

            MethodInfo[] methods = mtype.GetMethods();

            foreach (var item in methods)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("---------------------------------");

            MethodInfo method = mtype.GetMethod("Method");
            method.Invoke(obj, null);

            Console.ReadKey();
        }
    }
}

--->
构造函数
Void Method()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
---------------------------------
我的方法

使用接口完成方法调用

新建类库并创建一个接口

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

namespace ClassLibrary2
{
    public interface Interface1
    {
        void Method();
    }
}

添加引用

  

实现继承

using ClassLibrary2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class Class1: Interface1
    {
        public Class1()
        {
            Console.WriteLine("构造函数");
        }
        public void Method()
        {
            Console.WriteLine("我的方法");
        }
    }
}

将下面文件复制替换TestCSharp文件夹中Debug文件夹的同名文件

完成调用

using ClassLibrary2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace TestCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.Load("ClassLibrary1");
            Type mtype = assembly.GetType("ClassLibrary1.Class1");

            Object obj = Activator.CreateInstance(mtype);

            Console.WriteLine(obj);

            Interface1 interface1 = (Interface1)obj;

            interface1.Method();

            Console.ReadKey();
        }
    }
}

--->
构造函数
ClassLibrary1.Class1
我的方法

猜你喜欢

转载自blog.csdn.net/NCZ9_/article/details/88814548