C# 反射映射的简单使用

通过反射获取到目标类中的字段属性以及方法
一般可用于获取别人提供的dll 查看dll中有哪些字段属性方法

给自己记录一下反射的使用,说不定哪天项目就用上了

namespace MyInfo
{
    internal class MyClass
    {
        public int a { get; set; }
        private string b;
        public int c;
        public void info()
        {

        }
    }
}
using System;
using System.Reflection;

namespace MyInfo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Type
            //方法一
            //Type t = typeof(MyClass);

            //方法二
            MyClass myClass = new MyClass();
            Type t = myClass.GetType();

            Console.WriteLine(t.Name);
            Console.WriteLine(t.Assembly);
            Console.WriteLine(t.FullName);

            //获取到字段
            FieldInfo[] fieldInfo = t.GetFields();
            foreach (FieldInfo fil in fieldInfo)
            {
                Console.WriteLine(fil);
            }

            //获取到属性
            PropertyInfo[] propertyInfos= t.GetProperties();
            foreach (PropertyInfo item in propertyInfos)
            {
                Console.WriteLine(item.Name);
            }

            //获取方法
            MethodInfo[] methodInfos = t.GetMethods();
            foreach (MethodInfo method in methodInfos)
            {
                Console.WriteLine(method.Name);
            }

            Console.WriteLine("Hello World!");
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值