ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

.net基础—反射

2022-02-10 01:04:09  阅读:173  来源: 互联网

标签:反射 Console Name GetType 基础 WriteLine net public 构造函数


反射

反射提供描述程序集、模块和类型的对象(Type 类型)。 可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型,然后调用其方法或访问器字段和属性。 如果代码中使用了特性,可以利用反射来访问它们。

反射的类位于System.Reflection命名空间中,他们是.net Reflection API的一部分,所以在使用的反射的程序中一般都要使用 System.Reflection的命名空间。
System. Type类包装了类型,因此是整个反射子系统的核心,这个类中包含了很多属性和方法,使用这些属性和方法可以在运行时得到类型的信息。

通过反射获取类型信息

 

internal class Program
    {
        static void Main(string[] args)
        {

            //获取类型信息
            Type t = typeof(int);
            Console.WriteLine($"命名空间名称:{t.Namespace}");
            Console.WriteLine($"基类:{t.BaseType}");
            Console.WriteLine($"全名:{t.FullName}");
            Console.WriteLine($"是否为抽象类型:{t.IsAbstract}");
            Console.WriteLine($"是否为密封类:{t.IsSealed}");


            //获取所以公有成员
            Type t2 = typeof(TestClass);
            var mi = t2.GetMembers();//获取所以公有成员
            foreach (var m in mi)
            {
                //Console.WriteLine($"【{m.MemberType.ToString()} : {m.Name}】");
            }

            //获取方法相关的信息
            MethodInfo[] methodList = t.GetMethods();
            foreach (var m in methodList)
            {
                Console.Write($"方法的返回类型:{m.ReturnType.Name}");
                Console.Write($"方法的名称:{m.Name} (");
                
                //获取方法参数列表并保持在ParameterInfo对象数组中
                ParameterInfo[] parameters = m.GetParameters();
                for (int i = 0; i < parameters.Length; i++)
                { 
                    Console.Write($"方法的参数类型名称{parameters[i].ParameterType.Name}");
                    Console.Write($"方法的参数名{parameters[i].Name}");
                    if (i + 1 < parameters.Length)
                    {
                        Console.Write($", ");
                    }
                }
                Console.Write($")");
                Console.WriteLine();
            }

            Console.ReadLine();
        }

    }

    class TestClass
    {

        public string TestName { get; set; }

        public int Sum(int x, int y)
        {
            return x + y;
        }

    }

  

通过反射调用构造函数和方法

   internal class Program
    {
        static void Main(string[] args)
        {
            #region 使用反射调用程序集中类型中的方法
            //加载dll 
            Assembly assembly = Assembly.Load("DemoLibrary");
            //获取类型信息
            Type type = assembly.GetType("DemoLibrary.DemoClass"); 
            object demoClass = Activator.CreateInstance(type); //创建对象
            DemoClass demo = (DemoClass)demoClass; //类型转换
            demo.Show(); //方法调用
            #endregion

            #region 调用不同参数的构造函数

            Type type2 = assembly.GetType("DemoLibrary.DemoClass2");
            object demoClass2 = Activator.CreateInstance(type2);
            object demoClass22 = Activator.CreateInstance(type2,new object[] { 123});
            object demoClass222 = Activator.CreateInstance(type2,new object[] { "abc"});

            #endregion

            #region 调用泛型
            // `占位符;2表示参数个数(一个的时候也要写)
            Type type3 = assembly.GetType("DemoLibrary.Demo3`2");
            //object demo3 = Activator.CreateInstance(type3); //这么写报错,因为没有指定类型
            //指定泛型的参数类型
            Type newType = type3.MakeGenericType(new Type[] { typeof(int), typeof(string) });
            object objNewType = Activator.CreateInstance(newType);

            #endregion


            Console.ReadKey();
        }
    }

  

程序集中的类:

DemoClass:

public class DemoClass
    {
        public DemoClass()
        {
            Console.WriteLine($"{this.GetType().Name} 被构造");
        }

        public void Show()
        {
            Console.WriteLine($"{this.GetType().Name}.Show");
        }
    }

DemoClass2:

public class DemoClass2
    {
        /// <summary>
        /// 无参数构造函数
        /// </summary>
        public DemoClass2()
        {
            Console.WriteLine($"这是无参数{this.GetType().Name} 构造函数");
        }
        /// <summary>
        /// 带参数的构造函数
        /// </summary>
        public DemoClass2(string strVal)
        {
            Console.WriteLine($"这有参数{this.GetType().Name} 构造函数,传入的值:{strVal}");
        }
        /// <summary>
        /// 带参数的构造函数
        /// </summary>
        public DemoClass2(int intVal)
        {
            Console.WriteLine($"这有参数{this.GetType().Name}  构造函数,传入的值:{intVal}");
        }

    }

Demo3:

 public class Demo3<T, X>
    {
        public void Show(T t, X x)
        {
            Console.WriteLine($"t.type={t.GetType()},x.type={x.GetType()}");
        }
    }

  

 

 

  

 

标签:反射,Console,Name,GetType,基础,WriteLine,net,public,构造函数
来源: https://www.cnblogs.com/tanyongjun/p/15861685.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有