c#中的Type类

官方文档:链接地址

Type类,用来包含类型的特性。对于程序中的每一个类型,都会有一个包含这个类型的信息的Type类的对象,类型信息包含数据,属性和方法等信息。

1.生成Type对象
有两种方法可以生成Type类的对象:一种是Typeof(类名),一种是对象调用GetType()函数,如:

Type type = typeof(Person);
Person person = new Person();
Type type2 = person.GetType();
使用typeof运算符,如Type t = typeof(int);
使用GetType()方法,如int i;Type t = i.GetType();
使用Type类的静态方法GetType(),如Type t = Type.GetType("System.Double");

2.获取类的信息

Person person = new Person();
Type type = person.GetType();
//类的名称
string name = type.Name;
//类的命名空间
string space = type.Namespace;
//类的程序集
Assembly assembly = type.Assembly;
//类的共有字段
FieldInfo[] fieldInfos = type.GetFields();
//类的属性
PropertyInfo[] propertyInfos = type.GetProperties();
//类的方法
MethodInfo[] methodInfos = type.GetMethods();
GetMethod()    返回一个方法的信息
GetMethods()   返回所有方法的信息
//GetMember()和GetMembers()方法返回数据类型的任何成员或所有成员的详细信息,不管这些成员是构造函数、属性和方法等。

3.代码实例:

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

namespace TestConsole
{
    delegate void MyDel();
    class Program
    {

        static void Main(string[] args)
        {

            Type type = Type.GetType("ConsoleApp1.ReflectionClass");  //调用程序集下的类
            //获取构造函数
            ConstructorInfo constructor1 = type.GetConstructor(new Type[0]);//获取具有的零个参数的构造函数
            constructor1.Invoke(new object[0]);//调用无参构造函数
            ConstructorInfo constructor2 = type.GetConstructor(new Type[] { typeof(string) });
            constructor2.Invoke(new object[] { "蛋蛋" });//调用
            object obj = Activator.CreateInstance(type, new object[] { "建国" });//使用具有一个参数的构造函数创建一个实例
            //获取、设置字段值,
            FieldInfo fieldInfo = type.GetField("age", BindingFlags.NonPublic | BindingFlags.Instance);//获取私有字段的值,这句话的意思是在指定实例中搜索给public的字段
            fieldInfo.SetValue(obj, 1);//设置当对象的age字段的值
            object age = fieldInfo.GetValue(obj);//读取当前对象的age字段的值
            Console.WriteLine(age);
            //获取、设置属性值
            PropertyInfo propertyInfo = type.GetProperty("Name");
            propertyInfo.SetValue(obj, "测试属性");//设置当前对象的name属性值
            object name = propertyInfo.GetValue(obj);//获取当前对象的name属性值
            Console.WriteLine(name);
            //获取指定方法并执行
            MethodInfo[] methods = type.GetMethods();
            foreach (MethodInfo method in methods)
            {
                if (method.Name == "SayHello")
                {
                    method.Invoke(obj, new object[] { "测试人员" });
                }
                else if (method.IsStatic)
                {
                    method.Invoke(null, new object[] { "静态函数的测试人员" });//这里Invoke的第一个参数可以写成null或者一个实例对象
                }
                else
                {
                    Console.WriteLine("方法" + method.Name + "这不是我们将要执行的方法!");
                }
            }
            Console.ReadKey();
        }
    }
}

新建类:

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

namespace ConsoleApp1
{
    class BaseClass
    {
        public long Id { get; set; }
        public DateTime CreateDateTime { get; set; }
    }
    class ReflectionClass : BaseClass
    {
        public ReflectionClass()
        {
            Console.WriteLine("这是Reflection的无参构造函数");
        }
        public ReflectionClass(string name)
        {
            Console.WriteLine(name + "你好!");
        }
        public void SayHello(string name)
        {
            Console.WriteLine(name + ",这个是用于测试的SayHello方法");
        }
        public static void Welcome(string name)
        {
            Console.WriteLine(name + ",这个是用于测试的Welcome方法");
        }
        private int age;
        public int Age
        {
            get
            {
                return this.age;
            }
            set
            {
                this.age = value;
            }
        }
        public string Name { get; set; }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值