C# 反射和特性练习代码

反射

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
using System.Runtime;

class Test
{
    private int b;
    public int a = 1;
    public string str = "123";
    public Test() { b = 5; }
    public Test(int b)
    {
        this.b = b;
    }
    public Test(int a, string str)
    {
        this.a = a;
        this.str = str;
    }
    public void Speak(int a)
    {
        Console.WriteLine("{0}说话了",a);
    }
}



class Program
{



    public static void Main()
    {
        Type type = typeof(Test);

        Console.WriteLine(type.Assembly);
        //获取类中的所有公共成员
        MemberInfo[] info = type.GetMembers();
        for (int i = 0; i < info.Length; i++)
        {
            Console.WriteLine(info[i]);
        }
        Console.WriteLine("00000000000000000");

        //获取所有公共构造函数
        ConstructorInfo[] ctor = type.GetConstructors();
        for (int i = 0; i < ctor.Length; i++)
        {
            Console.WriteLine(ctor[i]);
        }
        Console.WriteLine("111111111111111111");

        //得到无参构造并执行
        ConstructorInfo ctorinfo1 =type.GetConstructor(new Type[0]);
        Test a = ctorinfo1.Invoke(null)as Test;
        Console.WriteLine(a.str);
        Console.WriteLine("2222222222222222222");

        //得到有参构造并执行
        ConstructorInfo ctorinfo2 =type.GetConstructor(new Type[] {typeof(int), typeof(string)});
        Test b = ctorinfo2.Invoke(new object[] {666,"999"})as Test;
        Console.WriteLine(b.str);
        Console.WriteLine("33333333333333333333");

        //得到所有公共成员变量
        FieldInfo[] fieldInfos = type.GetFields();
        for (int i = 0; i < fieldInfos.Length; i++)
        {
            Console.WriteLine(fieldInfos[i]);
        }
        Console.WriteLine("444444444444444444444");

        //得到指定名称成员变量
        FieldInfo info1 = type.GetField("str");
        Console.WriteLine(info1);
        Console.WriteLine("555555555555555555555");

        //通过反射获取和修改对象的值
        Test test = new Test(12,"693");
        Console.WriteLine(info1.GetValue(test));
        Console.WriteLine("6666666666666666666666");
        //设置
        info1.SetValue(test, "100");
        Console.WriteLine(info1.GetValue(test));
        Console.WriteLine("7777777777777777777777");
        //获取类的公共成员方法
        MethodInfo[] MInfos = type.GetMethods();
        for (int i = 0; i < MInfos.Length; i++)
        {
            Console.WriteLine(MInfos[i]);
        }
        // 调用类中的方法 (让他说话真tm难)
        //MethodInfo 的Invoke 方法 第一个参数是要调用方法的类,第二个参数是方法要用的参数们
        MethodInfo speak = type.GetMethod("Speak",
            new Type[] {typeof(int)});
        ConstructorInfo constructorInfo =type.GetConstructor(new Type[0] );

        speak.Invoke((constructorInfo.Invoke(null) as Test),new object[]{ 911});


        //Activator


        
    }
}

反射特性

类库

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{

    class PlayerAttribute : Attribute
    {

        public PlayerAttribute() 
        {
           
        }
       
    }

    internal class Player
    {
       [Player()] public  string Name;
        public int Atk;
        public int Hp ;
        public int Def ;
        public int posX;
        public int posY;
        public Player() { }
    }
}

特性

cs 复制代码
using System.Reflection;

class Program
{
    
    public static void Main()
    {
        Assembly a = Assembly.LoadFrom(@"E:\应用  。。。\Visual Studio 2022\WWWWorks\Advanced\ClassLibrary1\bin\Debug\ClassLibrary1");
        Type[] Type = a.GetTypes();
        for (int i = 0; i < Type.Length; i++)
        {
            Console.WriteLine(Type[i]);
        }
        Type player = a.GetType("ClassLibrary1.Player");

        MemberInfo[] Minfo= player.GetMembers();
        foreach (MemberInfo mi in Minfo)
        {
            Console.WriteLine(mi);
        }

        FieldInfo field = player.GetField("Name");
        
        object obj = Activator.CreateInstance(player);


        //得到PlayerAttribute的Type
        Type attribute = a.GetType("ClassLibrary1.PlayerAttribute");

        if (field.GetCustomAttribute(attribute) != null)
        {
            //通过得到的特性Type来判断field成员有没有该特性
            //有的话
         
                Console.WriteLine("不能修改该成员");
            

        }
        else
        {   //没有就可以设置名字
            field.SetValue(obj, "好名字");
            Console.WriteLine(field.GetValue(obj));
        }
    }
}
相关推荐
liu****10 分钟前
4.基础开发工具(一)
linux·开发语言·1024程序员节
文火冰糖的硅基工坊10 分钟前
[人工智能-大模型-72]:模型层技术 - 模型训练六大步:①数据预处理 - 基本功能与对应的基本组成函数
开发语言·人工智能·python
小龙报14 分钟前
《C语言疑难点 --- 字符函数和字符串函数专题(上)》
c语言·开发语言·c++·算法·学习方法·业界资讯·visual studio
凭君语未可19 分钟前
深度解析Java的多态特性
java·开发语言
csbysj202030 分钟前
DTD 元素:XML 与 SGML 文档结构解析指南
开发语言
傻童:CPU43 分钟前
C语言练习题
c语言·开发语言
极地星光1 小时前
协程:实战与系统集成(高级篇)
开发语言
0和1的舞者1 小时前
《Git:从入门到精通(八)——企业级git开发相关内容》
大数据·开发语言·git·搜索引擎·全文检索·软件工程·初学者
liulilittle1 小时前
LwIP协议栈MPA多进程架构
服务器·开发语言·网络·c++·架构·lwip·通信
水淹萌龙2 小时前
玩转 Go 表达式引擎:expr 实战指南
开发语言·后端·golang