C#特性+反射(通过反射遍历类和方法的特性)+属性

特性:C#特性(Attribute)_寒冷的晚风的博客-CSDN博客

反射:动态的创建封装的程序集、模块和类型的对象实例,将类型绑定到现有对象,从现有对象中获取类型,然后调用类型的方法或访问其字段和属性

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

namespace AtrributeTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            HelpAttribute help;
            foreach(HelpAttribute attr in typeof(MyClass).GetCustomAttributes(true))//遍历类的属性
            {
                help = attr;
                if(help != null)
                {
                    Console.WriteLine("Description:{0}",help.Description);
                    Console.WriteLine("Name:"+ help.Name);
                    Console.WriteLine("Age:{0}",help.Age);
                }
            }
            foreach (System.Reflection.MethodInfo methods in typeof(MyClass).GetMethods())//遍历类下面的所有方法
            {
                foreach(Attribute methodAttr in methods.GetCustomAttributes(true))//遍历方法的所有属性
                {
                    HelpAttribute temp = methodAttr as HelpAttribute;
                    if (temp != null)
                    {
                        Console.WriteLine("Description:{0}", temp.Description);
                        Console.WriteLine("Name:" + temp.Name);
                        Console.WriteLine("Age:{0}", temp.Age);
                    }
                }
            }
            Console.ReadKey();
        }
    }
    //自定义Attribute标签
    //AllowMultiple在这里表示同一个类上是否只能放一个Attribute
    //Inherited表示Class被继承的时候,是否继承Attribute
    [AttributeUsage
    #region
        (AttributeTargets.Class |
        AttributeTargets.Method,
    #endregion
        AllowMultiple = true,Inherited =false)]
    public class HelpAttribute : System.Attribute
    {
        private string name;
        private int age;
        public string description;
        public HelpAttribute(string name,int age)
        {
            this.name = name;
            this.age = age;
        }
        public string Name
        {
            get { return this.name; }
        }
        public int Age
        {
            get { return this.age; }
        }
        public string Description
        {
            set { this.description = value; }
            get { return this.description; }
        }
    }
    //给MyClass加上了自定义的Attribute标签信息
   [HelpAttribute("Peter",18,Description = "this is a class")]
    public class MyClass
    {
        [HelpAttribute("ZhangSan", 19, Description = "set course and score")]
        public void setClass()
        {
        }
        [HelpAttribute("LiSi", 20, Description = "display course and score")]
        public void getClass()
        {
        }
    }
}

更多C#反射机制和方法:C# 反射机制以及方法_c#反射获取方法_Phil Arist的博客-CSDN博客

属性:使用属性的访问器来获取或设置类或结构中的私有属性

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

namespace ProPertyTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student();
            student.Name = "张三";
            student.Age = 12;
            Console.WriteLine($"Student Info:{student}");
        }
    }
    class Student
    {
        private string id="20230405";
        private string name;
        private int age;
        //只读的访问器
        public string Id
        {
            get { return id; }
        }
        //可读可写的访问器
        public string Name
        {
            set { name = value; }
            get { return name; }
        }
        //只写的访问器
        public int Age
        {
            set { age = value; }
        }
        public override string ToString()
        {
            return $"id:{Id},name:{Name}"; //无法只读Age的值
        }
    }
}

结语:合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。

猜你喜欢

转载自blog.csdn.net/falsedewuxin/article/details/129969821