C# basic syntax of reflection



1. Reflection definition

The behavior of a running program to view its own elements or other types of metadata is called reflection

MVC, IOC, ORM, AOP all use reflection.



2. Simple example

 class BaseClass
    {
    
    
        public int BaseField = 0;
    }
 class DerivedClass:BaseClass
    {
    
    
        public int DerviedField = 0;
    }
 static void Main(string[] args)
        {
    
    
            BaseClass bc = new BaseClass();
            var dc = new DerivedClass();
            BaseClass[] bca = new BaseClass[] {
    
     bc, dc };
            foreach (var v in bca)
            {
    
    
                Type t = v.GetType();
                Console.WriteLine($"Object type:{t.Name}");


                FieldInfo[] fi = t.GetFields();
                foreach (var f in fi)
                {
    
    
                    Console.WriteLine($"File :{f.Name}");
                }
                Console.WriteLine();
            }
        }

Output result:

Object type:BaseClass
File :BaseField

Object type:DerivedClass
File :DerviedField
File :BaseField




3. Commonly used Type members

member Member type description
Name Attributes Return type name
Namespace Attributes Return type namespace
Assembly Attributes Returns the data set of the declared type
GetFields method Return type of field list
GetProperties method Returns the list of attributes of the type
GetMethods method Return type method list



4. Use reflection on dll files

4.1 Reading of dll files

通过程序集名称返回Assembly对象
        Assembly ass = Assembly.Load("ClassLibrary831");
        
通过DLL文件名称返回Assembly对象
        Assembly ass = Assembly.LoadFrom("ClassLibrary831.dll");
        
通过Assembly获取程序集中类 
        Type t = ass.GetType("ClassLibrary831.NewClass");   //参数必须是类的全名
        
通过Assembly获取程序集中所有的类
        Type[] t = ass.GetTypes();


4.2 Specific examples

1. Directory structure

Insert picture description here
2. Code

 static void Main(string[] args)
        {
    
    
            // 加载 DLL 文件
            // 这个是直接动态加载
            Assembly assembly = Assembly.Load("LibA");
            // 和上面的区别是这个得用 dll 文件
            Assembly assembly1 = Assembly.LoadFrom("LibA.dll");
            // 用绝对路径引入
            Assembly assembly2 = Assembly.LoadFile("D:/Desktop/test01/ConsoleApp3/LibA/bin/Debug/netstandard2.0/LibA.dll");

            var Tpye = assembly2;
            Console.WriteLine(Tpye.FullName);

            // 获取类型(这一步要在获得 Dll 文件的基础上进行)
            
            Type type = assembly.GetType("LibA.Class1");
            object o = Activator.CreateInstance(type);// 必须是个type类型,简单点说,必须是个类。
            Console.WriteLine(type.Name);

            // 用接口做强制规范,然他能 “点“ 出来
            IAClasscs rel = type as IAClasscs;
            rel.GetName();

            //object mm = Activator.CreateInstance(type);


references

[1] https://www.cnblogs.com/Stephenchao/p/4481995.html
[2] https://www.bilibili.com/video/BV1hk4y1r7PZ

Guess you like

Origin blog.csdn.net/zhaozhao236/article/details/111656721