首页 > 编程语言 >C# 特性的创建与使用

C# 特性的创建与使用

时间:2023-08-18 14:12:45浏览次数:37  
标签:set get C# 创建 AttributeTargets 特性 class customAttributes public

1、先创建一些特性以及一个示例类

//应用的目标类型:类,属性,或者其他,是否对同一个目标进行多次应用
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    class DoseInterstingThingAttribute : Attribute
    {
        public int HowManyTimes { get; private set; }
        public string WhatDoseItDo { get; set; }

        public DoseInterstingThingAttribute(int howManyTimes)
        {
            HowManyTimes = howManyTimes;
        }
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    class DataBaseTypeAttribute : Attribute
    {
        public string DataTypeName { get; set; }

        public int MaxConnectiCount { get; set; }

        public DataBaseTypeAttribute(string dataTypeName)
        {
            DataTypeName = dataTypeName;
        }
    }

    [AttributeUsage(AttributeTargets.Property,AllowMultiple =false)]
    class DataAttribute : Attribute
    {
        public bool IsPrimaryKey { get; set; }
    }

    [DataBaseType("Mysql", MaxConnectiCount = 1000)]
    [DoseInterstingThing(1000)]
    public class DecoratedClass
    {
        [Data(IsPrimaryKey =false)]
        public int Code { get; set; }

        public string Name { get; set; }
    }

2、Main方法中输出

 static void Main(string[] args)
        {
            #region 特性使用
            Type classType = typeof(DecoratedClass);
            object[] customAttributes = classType.GetCustomAttributes(true);
            foreach (var customAttribute in customAttributes)
            {
                Console.WriteLine($"Attribute of type {customAttribute} found.");
                DoseInterstingThingAttribute interstingThingAttribute = customAttribute as DoseInterstingThingAttribute;
                if (interstingThingAttribute != null)
                {
                    Console.WriteLine($"This class does {interstingThingAttribute.WhatDoseItDo} x{interstingThingAttribute.HowManyTimes}");
                }
                DataBaseTypeAttribute dataBaseTypeAttribute = customAttribute as DataBaseTypeAttribute;
                if (dataBaseTypeAttribute != null)
                {
                    Console.WriteLine($"This class does {dataBaseTypeAttribute.DataTypeName}");
                }
            }

            PropertyInfo[] propertyInfo = classType.GetProperties();
            foreach (var property in propertyInfo)
            {
                object[] customAttributes_Propertys = classType.GetProperty(property.Name).GetCustomAttributes(true);
                foreach (var customAttributes_Property in customAttributes_Propertys)
                {
                    Console.WriteLine((DataAttribute)customAttributes_Property==null? $"{property.Name}不存在Data特性": ((DataAttribute)customAttributes_Property).IsPrimaryKey.ToString());
                }
            }
            #endregion
        }

 

标签:set,get,C#,创建,AttributeTargets,特性,class,customAttributes,public
From: https://www.cnblogs.com/icarec/p/17640304.html

相关文章

  • shell select命令语句 用户多选
    select命令语句,默认只能输入一个选择项。但有时候需要让用户输入多个选项,就需要加for循环处理多选项了。一、示例代码#!/usr/bin/envbashchoices=('one''two''three''four''five')#samplechoicesselectdummyin"${choices[@]}";do#present......
  • C++快速入门 第四讲:文件操作
    ifream与ofream分别为文件读取类和文件写入类实例1:文件读取(读取同一文件夹的test.txt文件内容)1#include<fstream>//涉及到了文件流操作2#include<iostream>34usingnamespacestd;56intmain()//in输入:读out输出:写7{8ifstreamin;//......
  • 1 CSS的引入方式
    1CSS的引入方式CSS样式有三种不同的使用方式,分别是行内样式,嵌入样式以及链接式。我们需要根据不同的场合不同的需求来使用不同的样式。行内样式行内样式,就是写在元素的style属性中的样式,这种样式仅限于元素内部起作用。当个别元素需要应用特殊样式时就可以使用内联样式。但不......
  • Script Lab
    $("#setup").click(()=>tryCatch(setup));$("#add-row").click(()=>tryCatch(addRow));$("#add-column").click(()=>tryCatch(addColumn));$("#add-calculated-column").click(()=>tryCatch(addCalculatedCo......
  • C++快速入门 第五讲:输入输出小结
    实例1:根据输入内容输出1#include<iostream>2usingnamespacestd;//名字空间3intmain()4{5charanswer;67cout<<"请问可以格式化您的硬盘吗?!【Y/N】"<<"\n";8cin>>answer;910switch(answer......
  • C#程序变量统一管理例子 - 开源研究系列文章
            今天讲讲关于C#应用程序中使用到的变量的统一管理的代码例子。        我们知道,在C#里使用变量,除了private私有变量外,程序中使用到的公共变量就需要进行统一的存放和管理。这里笔者使用到的公共变量管理库划分为:1)窗体;2)路径;3)对象;所以笔者对这几个库进行......
  • .net7 创建windows后台服务
    开发语言:C#运行时:.net7开发环境:visualstudio2022微软官方文档:https://learn.microsoft.com/zh-cn/dotnet/core/extensions/windows-service?pivots=dotnet-7-0最近公司需要使用后台调度服务定时拉取业务数据并推送至第三方,原本是直接使用aps.netcore应用程序部署在IIS上......
  • docker-compose是什么怎么使用
    docker-compose是一个用于定义和运行多个Docker容器的工具,它允许你使用一个单独的配置文件来定义多个容器、网络设置、卷挂载等,并可以一次性地启动、停止、重建整个应用程序。以下是使用docker-compose的基本步骤:创建docker-compose.yml文件:在你的项目目录下创建一个......
  • sol. ABC246Ex
    动态DP模板题[ABC246Ex]01?Queries题目大意:给定一个长度为\(N\)且只包含?,1,0的字符串\(a\)。\(Q\)次操作,每次操作会修改字符串中的一个字符,并求出此时整个字符串中本质不同的子序列个数。众所周知,动态DP依然是DP的一种。先考虑没有修改操作,那么本题变为一个普......
  • 容器化部署nacos 1.4.6报错caused: The specified key byte array is 0 bits which is
    nacos2.0+与nacos1.x区别nacos在2.0+版本开始使用grpc与客户端通信,并且通过非8848端口通信主要是有两个端口端口与主端口的偏移量描述98481000客户端gRPC请求服务端端口,用于客户端向服务端发起连接和请求98491001服务端gRPC请求服务端端口,用于服务间同步等......