美文网首页
c# Attribute 特性

c# Attribute 特性

作者: 独步江雪 | 来源:发表于2021-01-19 11:54 被阅读0次

https://www.cnblogs.com/zhaoyl9/p/12027938.html

using System;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

namespace ConsoleApp
{
    class ValidatorAttribute : Attribute
    {
        public bool use = false;
    }

    class Test
    {
        [ValidatorAttribute(use = true)] public string a = "aa";
        [ValidatorAttribute(use = false)] public string b = "bb";
        public string c = "cc";
    }

    class Program
    {
        static void Main(string[] args)
        {
            var t = new Test();

            foreach (var e in t.GetType().GetFields())
            {
                var attrs = e.GetCustomAttributes().Where(e1 => (e1 as ValidatorAttribute) != null).ToList();
                if (attrs.Any())
                {
                    Console.WriteLine($"{e.Name} use valid :{((ValidatorAttribute) attrs[0]).use}");
                }
                else
                {
                    Console.WriteLine($"{e.Name} use valid :null");
                }
            }
        }
    }
}

相关文章

  • C#特性(Attribute)-现学现用

    前言 想要灵性的使用C#反射机制,特性(Attribute)的使用是必不可少的。 C# 特性(Attribute)...

  • C# 特性(Attribute)

    特性是用于在运行时传递程序中各种元素(比如类、方法、结构、枚举、组件等)的行为信息的声明性标签。可以通过使用特性向...

  • c# Attribute 特性

    https://www.cnblogs.com/zhaoyl9/p/12027938.html[https://w...

  • [Hack] C# Attribute使用约束

    Attribute是C#中一个广泛使用的特性,通过使用Attribute对程序集中的类型进行标记,并通过Refle...

  • c#特性

    c#特性: 特性(Attribute)是用于在运行时传递程序中的各种元素(比如类,方法,结构,枚举,组件等)的行为...

  • 知识点|C#高级知识:Attribute特性

    C#特性[Attribute]是一类特殊的语法结构,我们掌握了特性的相关用法,就能在运行时,通过反射,得到一些额外...

  • 编辑器扩展基础1——常用的Attribute

    Attribute Attribute是C#的功能,在Unity中可以使用Attribute来给变量和方法增加新的...

  • 关于C# 中的Attribute 特性

    https://kb.cnblogs.com/page/87531/

  • 特性(Attribute)

    前言:本来打算将特性(Attribute)和反射(Reflection)写在一章里,但感觉反射(Reflectio...

  • Unity Attribute(特性)

    1、DisallowMultipleComponent控制同一类型的组件在一个对象上只能挂载一个,一般给一个父类加...

网友评论

      本文标题:c# Attribute 特性

      本文链接:https://www.haomeiwen.com/subject/iruqzktx.html