当前位置: 首页 > news >正文

C#自定义特性-SQL

语法

原则

        自定义特性必须继承自System.Attribute类;
        AttributeUsage属性来指定特性的使用范围和是否允许重复等;
        在特性类中定义属性,这些属性将用于存储特性值。

示例

using System;// 定义一个自定义特性类
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class CustomAttribute : Attribute
{// 特性属性public string Description { get; set; }public int Version { get; set; }// 特性构造函数public CustomAttribute(string description, int version){Description = description;Version = version;}
}// 使用自定义特性
[Custom("This is a sample class", 1)]
public class SampleClass
{[Custom("This is a sample method", 1)]public void SampleMethod(){// 方法实现}
}class Program
{static void Main(){// 获取SampleClass类的特性信息var classAttributes = typeof(SampleClass).GetCustomAttributes(typeof(CustomAttribute), false);foreach (CustomAttribute attr in classAttributes){Console.WriteLine($"Class Description: {attr.Description}, Version: {attr.Version}");}// 获取SampleMethod方法的特性信息var methodAttributes = typeof(SampleClass).GetMethod("SampleMethod").GetCustomAttributes(typeof(CustomAttribute), false);foreach (CustomAttribute attr in methodAttributes){Console.WriteLine($"Method Description: {attr.Description}, Version: {attr.Version}");}}
}

AttributeUsage中的AllowMultiple属性默认值为false,它决定同种特性类型的实例能否在同一个目标上多次使用,比如在类中的同一个属性上。 

特性声明代码

using System;
using System.Reflection;namespace Model.Common
{/// <summary>/// 用于生成SQLServer数据库查询的like条件/// </summary>[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]public class SqlLikeAttribute : Attribute{/// <summary>/// 数据库字段名/// </summary>public string FieldName { get; set; }public SqlLikeAttribute(string fidleName){FieldName = fidleName;}}/// <summary>/// 用于生成SQLServer数据库查询的>、>=、<、<==、=条件/// </summary>[AttributeUsage(AttributeTargets.Property,AllowMultiple = false)]public class SqlRangeAttribute: Attribute{/// <summary>/// 数据库字段名/// </summary>public string FieldName { get; set; }/// <summary>/// 取值范围:>、>=、<、<==、=/// </summary>public string Range { get; set; }public SqlRangeAttribute(string fidleName, string range){FieldName = fidleName;Range= range;}}/// <summary>/// 用于生成SQLServer数据库查询的between条件/// </summary>[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]public class SqlBetweenAttribute : Attribute{/// <summary>/// 数据库字段名/// </summary>public string FieldName { get; set; }/// <summary>/// 查询条件实体中的另一个字段名/// </summary>public string AnotherFieldName { get; set; }/// <summary>/// 是否是开始/// </summary>public bool IsStart { get; set; }public Object Value { get; set; }public SqlBetweenAttribute(string fidleName, string anotherFieldName, bool start = true){FieldName = fidleName;AnotherFieldName = anotherFieldName;IsStart = start;}}/// <summary>/// 用于生成SQLServer数据库查询的条件,有SqlIgnoreAttribute修饰的属性直接忽略掉/// </summary>[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]public class SqlIgnoreAttribute : Attribute{}/// <summary>/// 测试/// </summary>public class AttributeTest{public static void Test(){TestModel testModel = new TestModel(){ID = 1,Name = "test",Begin = DateTime.Now,End = DateTime.Now.AddDays(1),};Type type = testModel.GetType();PropertyInfo[] infos = type.GetProperties();foreach (PropertyInfo info in infos){SqlBetweenAttribute attr = (SqlBetweenAttribute)info.GetCustomAttribute(typeof(SqlBetweenAttribute), false);if (attr != null){string field = attr.FieldName;}}}}public class TestModel{public int ID { get; set; }public string Name { get; set; }[SqlBetween("field", "End")]public DateTime Begin { get; set; }[SqlBetween("field", "Begin", false)]public DateTime End { get; set; }}
}

使用特性

/// <summary>/// 从实体中获取属性值不为空的属性和值,用于数据库Select的Where条件/// 暂时只支持,int、string、double、DateTime/// 要求数值类型默认为-1/// </summary>/// <param name="data"></param>/// <param name="prefix">前缀</param>/// <returns></returns>public static List<string> GetPropertyValueNotNullForSelectWhere(this object data, string prefix){string item = "";List<string> items = new List<string>();Dictionary<string, SqlBetweenAttribute> dic = new Dictionary<string, SqlBetweenAttribute>();DateTime dateTime = new DateTime(1, 1, 1, 0, 0, 0);PropertyInfo[] propertyInfos = data.GetType().GetProperties();foreach (PropertyInfo propertyInfo in propertyInfos){if (propertyInfo.Name.ToUpper() == "ID")continue;item = "";object obj = propertyInfo.GetValue(data, null);switch (propertyInfo.PropertyType.FullName){case "System.Int32":if (Convert.ToInt32(obj) == -1)continue;item = $" and {prefix}{propertyInfo.Name}={Convert.ToInt32(obj)}";break;case "System.String":if (Convert.ToString(obj) == "")continue;item = $" and {prefix}{propertyInfo.Name}='{Convert.ToString(obj)}'";break;case "System.Double":if (Convert.ToDouble(obj) == -1)continue;item = $" and {prefix}{propertyInfo.Name}={Convert.ToDouble(obj)}";break;case "System.Decimal":if (Convert.ToDecimal(obj) == -1)continue;item = $" and {prefix}{propertyInfo.Name}={Convert.ToDecimal(obj)}";break;case "System.DateTime":obj = propertyInfo.GetValue(data, null);if (Convert.ToDateTime(obj) == dateTime)continue;item = $" and {prefix}{propertyInfo.Name}='{Convert.ToDateTime(obj)}'";break;}//if(!CheckAttrSqlBetween(propertyInfo, obj, ref dic, ref item))//    continue;CheckAttrSqlRange(propertyInfo, obj, ref item, prefix);CheckAttrSqlLike(propertyInfo, obj, ref item, prefix);if (!CheckAttrSqlIgnore(propertyInfo, obj, ref item))continue;items.Add(item);}return items;}/// <summary>/// 检查属性是否被SqlBetween特性修饰,并处理/// </summary>/// <param name="propertyInfo">实体的属性对象</param>/// <param name="obj">属性的值</param>/// <param name="dic">暂存特性的字典</param>/// <param name="item"></param>/// <returns>true 表示需要把item加到List<string>中</returns>static bool CheckAttrSqlBetween(PropertyInfo propertyInfo, object obj, ref Dictionary<string, SqlBetweenAttribute> dic, ref string item){SqlBetweenAttribute attr = (SqlBetweenAttribute)propertyInfo.GetCustomAttribute(typeof(SqlBetweenAttribute), false);if (attr == null)return true;attr.Value = obj;if (!dic.ContainsKey(attr.AnotherFieldName)){   //缺少另外一个,先缓存dic.Add(attr.AnotherFieldName, attr);return false;}else{SqlBetweenAttribute _attr = dic[attr.AnotherFieldName];dic.Remove(attr.AnotherFieldName);SqlBetweenAttribute attrb = attr.IsStart ? attr : _attr;SqlBetweenAttribute attre = attr.IsStart ? _attr : attr;switch (propertyInfo.PropertyType.FullName){case "System.Int32":case "System.Double":case "System.Decimal":item = $" and {attr.FieldName} between {attrb.Value} and {attre.Value}";break;case "System.String":case "System.DateTime":item = $" and {attr.FieldName} between '{attrb.Value}' and '{attre.Value}'";break;}return true;}}/// <summary>/// 检查属性是否被SqlRange特性修饰,并处理/// </summary>/// <param name="propertyInfo"></param>/// <param name="obj"></param>/// <param name="item"></param>/// <returns></returns>static void CheckAttrSqlRange(PropertyInfo propertyInfo, object obj, ref string item, string prefix){SqlRangeAttribute attr = (SqlRangeAttribute)propertyInfo.GetCustomAttribute(typeof(SqlRangeAttribute), false);if (attr == null)return;switch (propertyInfo.PropertyType.FullName){case "System.Int32":case "System.Double":case "System.Decimal":item = $" and {prefix}{attr.FieldName} {attr.Range} {obj} ";break;case "System.String":case "System.DateTime":item = $" and {prefix}{attr.FieldName} {attr.Range} '{obj}' ";break;}return;}/// <summary>/// 检查属性是否被SqlLike特性修饰,并处理/// </summary>/// <param name="propertyInfo"></param>/// <param name="obj"></param>/// <param name="item"></param>static void CheckAttrSqlLike(PropertyInfo propertyInfo, object obj, ref string item, string prefix){SqlLikeAttribute attr = (SqlLikeAttribute)propertyInfo.GetCustomAttribute(typeof(SqlLikeAttribute), false);if (attr == null)return;switch (propertyInfo.PropertyType.FullName){case "System.String":item = $" and ({prefix}{attr.FieldName} like '%{obj}%' or {prefix}{attr.FieldName}='{obj}') ";break;}return;}/// <summary>/// 检查属性是否被SqlIgnoreAttribute特性修饰,如果修饰则不加入到Where条件中/// </summary>/// <param name="propertyInfo"></param>/// <param name="obj"></param>/// <param name="item"></param>/// <returns></returns>static bool CheckAttrSqlIgnore(PropertyInfo propertyInfo, object obj, ref string item){SqlIgnoreAttribute attr = (SqlIgnoreAttribute)propertyInfo.GetCustomAttribute(typeof(SqlIgnoreAttribute), false);if (attr == null)return true;elsereturn false;}

相关文章:

C#自定义特性-SQL

语法 原则 自定义特性必须继承自System.Attribute类&#xff1b; AttributeUsage属性来指定特性的使用范围和是否允许重复等&#xff1b; 在特性类中定义属性&#xff0c;这些属性将用于存储特性值。 示例 using System;// 定义一个自定义特性类 [Attribute…...

协方差矩阵及其计算方法

协方差矩阵&#xff08;Covariance Matrix&#xff09;是一个描述多维数据特征之间相互关系的矩阵&#xff0c;广泛应用于统计学和机器学习中。它用于表示各个特征之间的协方差&#xff0c;是分析多维数据分布和特征依赖性的重要工具。 什么是协方差矩阵&#xff1f; 协方差矩…...

【OH】openHarmony开发环境搭建(基于windows子系统WSL)

前言 本文主要介绍基于windows子系统WSL搭建openHarmony开发环境。 WSL与Vmware虚拟机的区别&#xff0c;可以查看WSL与虚拟机的区别 更详细的安装配置过程可参考微软官网&#xff1a; ​安装 WSL 前提 以下基于windows 111专业版进行配置&#xff0c;windows 10应该也是可以…...

Visual Studio Code 端口转发功能详解

Visual Studio Code 端口转发功能详解 引言 Visual Studio Code&#xff08;简称 VS Code&#xff09;是一个功能强大的源代码编辑器&#xff0c;它支持多种编程语言的语法高亮、智能代码补全、自定义快捷键、代码重构等特性。除了这些基本功能外&#xff0c;VS Code 还提供了…...

Android Framework AMS(14)ContentProvider分析-1(CP组件应用及开机启动注册流程解读)

该系列文章总纲链接&#xff1a;专题总纲目录 Android Framework 总纲 本章关键点总结 & 说明&#xff1a; 说明&#xff1a;本章节主要解读ContentProvider组件的基本知识。关注思维导图中左上侧部分即可。 有了前面activity组件分析、service组件分析、广播组件分析的基…...

Three.js PBR材质

本文将详细介绍Three.js中的PBR&#xff08;Physically Based Rendering&#xff09;材质&#xff0c;包括PBR的基本概念、适用场景、PBR材质的构建以及一些高级应用技巧。 1. PBR&#xff08;Physically Based Rendering&#xff09;基本概念 PBR&#xff0c;即Physically B…...

智谱AI清影升级:引领AI视频进入音效新时代

前几天智谱推出了新清影,该版本支持4k、60帧超高清画质、任意尺寸&#xff0c;并且自带音效的10秒视频,让ai生视频告别了"哑巴时代"。 智谱AI视频腾空出世&#xff0c;可灵遭遇强劲挑战&#xff01;究竟谁是行业翘楚&#xff1f;(附测评案例)之前智谱出世那时体验了一…...

嵌入式硬件电子电路设计(五)MOS管详解(NMOS、PMOS、三极管跟mos管的区别)

引言&#xff1a;在我们的日常使用中&#xff0c;MOS就是个纯粹的电子开关&#xff0c;虽然MOS管也有放大作用&#xff0c;但是几乎用不到&#xff0c;只用它的开关作用&#xff0c;一般的电机驱动&#xff0c;开关电源&#xff0c;逆变器等大功率设备&#xff0c;全部使用MOS管…...

Centos 9 安装 PostgreSQL 16 并支持远程访问

仅列出核心操作&#xff0c;可以解决使用过程中遇到的访问问题。 1 安装 使用dnf源安装 sudo dnf module -y install postgresql:16 2 配置文件夹权限 使用root权限操作 sudo chown postgres:postgres /var/lib/pgsql/datasudo chmod -R 0750 /var/lib/pgsql/data 3 初…...

Dubbo源码解析(三)

一、Dubbo整合Spring启动流程 Dubbo的使用可以不依赖Spring&#xff0c;但是生产环境中Dubbo都是整合到Spring中一起使用&#xff0c;所以本章就解析Dubbo整合Spring的启动流程 一、传统的xml解析方式 一、Dubbo配置解析流程 在Java 中&#xff0c;一切皆对象。在JDK 中使用…...

HarmonyOS Next星河版笔记--界面开发(5)

1.字符串 1.1.字符串拼接 作用&#xff1a;把两个或多个字符串&#xff0c;拼成一个字符串。&#xff08;通常是用来拼接字符串和变量&#xff09; hello world > helloworld 加好作用&#xff1a;拼接 let name:string 小明 console.log(简介信息,名字是 name) …...

Spring Boot3 实战案例合集上线了

Spring Boot3实战案例合集...

在Ubuntu 24.04 LTS上安装飞桨PaddleX

前面我们介绍了《在Windows用远程桌面访问Ubuntu 24.04.1 LTS》本文接着介绍安装飞桨PaddleX。 PaddleX 3.0 是基于飞桨框架构建的一站式全流程开发工具&#xff0c;它集成了众多开箱即用的预训练模型&#xff0c;可以实现模型从训练到推理的全流程开发&#xff0c;支持国内外多…...

Homebrew 命令大全

Homebrew 是 macOS 和 Linux 系统上的一个流行的包管理器&#xff0c;它可以帮助用户轻松地安装、更新和管理软件包。以下是一些常用的 Homebrew 命令&#xff1a; 安装 Homebrew 如果你还没有安装 Homebrew&#xff0c;可以使用以下命令在 macOS 上进行安装&#xff1a; /b…...

Docker+Django项目部署-从Linux+Windows实战

一、概述 1. 什么是Docker Docker 是一个开源的应用容器引擎&#xff0c;支持在win、mac、Linux系统上进行安装。可以帮助我们在一台电脑上创建出多个隔离的环境&#xff0c;比传统的虚拟机极大的节省资源 。 为什么要创建隔离的环境&#xff1f; 假设你先在有一个centos7.…...

前端 JS 实用操作总结

目录 1、重构解构 1、数组解构 2、对象解构 3、...展开 2、箭头函数 1、简写 2、this指向 3、没有arguments 4、普通函数this的指向 3、数组实用方法 1、map和filter 2、find 3、reduce 1、重构解构 1、数组解构 const arr ["唐僧", "孙悟空&quo…...

11.15 机器学习-集成学习方法-随机森林

# 机器学习中有一种大类叫**集成学习**&#xff08;Ensemble Learning&#xff09;&#xff0c;集成学习的基本思想就是将多个分类器组合&#xff0c;从而实现一个预测效果更好的集成分类器。集成算法可以说从一方面验证了中国的一句老话&#xff1a; # 三个臭皮匠&#xff0c…...

【SQL】E-R模型(实体-联系模型)

目录 一、介绍 1、实体集 定义和性质 属性 E-R图表示 2. 联系集 定义和性质 属性 E-R图表示 一、介绍 实体-联系数据模型&#xff08;E-R数据模型&#xff09;被开发来方便数据库的设计&#xff0c;它是通过允许定义代表数据库全局逻辑结构的企业模式&#xf…...

C/C++静态库引用过程中出现符号未定义的处理方式

问题背景&#xff1a; 在接入新库&#xff08;静态库&#xff09;时遇到了符号未定义问题&#xff0c;并发现改变静态库的链接顺序可以解决问题。 问题根源&#xff1a; 静态库是由 .o 文件拼接而成的&#xff0c;链接静态库时&#xff0c;链接器以 .o 文件为单位进行处理。链接…...

『VUE』27. 透传属性与inheritAttrs(详细图文注释)

目录 什么是透传属性&#xff08;Forwarding Attributes&#xff09;使用条件唯一根节点禁用透传属性继承总结 欢迎关注 『VUE』 专栏&#xff0c;持续更新中 欢迎关注 『VUE』 专栏&#xff0c;持续更新中 什么是透传属性&#xff08;Forwarding Attributes&#xff09; 在 V…...

云计算——弹性云计算器(ECS)

弹性云服务器&#xff1a;ECS 概述 云计算重构了ICT系统&#xff0c;云计算平台厂商推出使得厂家能够主要关注应用管理而非平台管理的云平台&#xff0c;包含如下主要概念。 ECS&#xff08;Elastic Cloud Server&#xff09;&#xff1a;即弹性云服务器&#xff0c;是云计算…...

centos 7 部署awstats 网站访问检测

一、基础环境准备&#xff08;两种安装方式都要做&#xff09; bash # 安装必要依赖 yum install -y httpd perl mod_perl perl-Time-HiRes perl-DateTime systemctl enable httpd # 设置 Apache 开机自启 systemctl start httpd # 启动 Apache二、安装 AWStats&#xff0…...

1688商品列表API与其他数据源的对接思路

将1688商品列表API与其他数据源对接时&#xff0c;需结合业务场景设计数据流转链路&#xff0c;重点关注数据格式兼容性、接口调用频率控制及数据一致性维护。以下是具体对接思路及关键技术点&#xff1a; 一、核心对接场景与目标 商品数据同步 场景&#xff1a;将1688商品信息…...

【磁盘】每天掌握一个Linux命令 - iostat

目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat&#xff08;I/O Statistics&#xff09;是Linux系统下用于监视系统输入输出设备和CPU使…...

全球首个30米分辨率湿地数据集(2000—2022)

数据简介 今天我们分享的数据是全球30米分辨率湿地数据集&#xff0c;包含8种湿地亚类&#xff0c;该数据以0.5X0.5的瓦片存储&#xff0c;我们整理了所有属于中国的瓦片名称与其对应省份&#xff0c;方便大家研究使用。 该数据集作为全球首个30米分辨率、覆盖2000–2022年时间…...

uniapp微信小程序视频实时流+pc端预览方案

方案类型技术实现是否免费优点缺点适用场景延迟范围开发复杂度​WebSocket图片帧​定时拍照Base64传输✅ 完全免费无需服务器 纯前端实现高延迟高流量 帧率极低个人demo测试 超低频监控500ms-2s⭐⭐​RTMP推流​TRTC/即构SDK推流❌ 付费方案 &#xff08;部分有免费额度&#x…...

OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别

OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别 直接训练提示词嵌入向量的核心区别 您提到的代码: prompt_embedding = initial_embedding.clone().requires_grad_(True) optimizer = torch.optim.Adam([prompt_embedding...

C++使用 new 来创建动态数组

问题&#xff1a; 不能使用变量定义数组大小 原因&#xff1a; 这是因为数组在内存中是连续存储的&#xff0c;编译器需要在编译阶段就确定数组的大小&#xff0c;以便正确地分配内存空间。如果允许使用变量来定义数组的大小&#xff0c;那么编译器就无法在编译时确定数组的大…...

JS设计模式(4):观察者模式

JS设计模式(4):观察者模式 一、引入 在开发中&#xff0c;我们经常会遇到这样的场景&#xff1a;一个对象的状态变化需要自动通知其他对象&#xff0c;比如&#xff1a; 电商平台中&#xff0c;商品库存变化时需要通知所有订阅该商品的用户&#xff1b;新闻网站中&#xff0…...

Java毕业设计:WML信息查询与后端信息发布系统开发

JAVAWML信息查询与后端信息发布系统实现 一、系统概述 本系统基于Java和WML(无线标记语言)技术开发&#xff0c;实现了移动设备上的信息查询与后端信息发布功能。系统采用B/S架构&#xff0c;服务器端使用Java Servlet处理请求&#xff0c;数据库采用MySQL存储信息&#xff0…...