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

深圳网站建设seo优化武汉网站推广公司排名

深圳网站建设seo优化,武汉网站推广公司排名,怎么制作网页,辽宁省建设工程信息网公告语法 原则 自定义特性必须继承自System.Attribute类; AttributeUsage属性来指定特性的使用范围和是否允许重复等; 在特性类中定义属性,这些属性将用于存储特性值。 示例 using System;// 定义一个自定义特性类 [Attribute…

语法

原则

        自定义特性必须继承自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;}

http://www.shuangfujiaoyu.com/news/55371.html

相关文章:

  • 盘锦如何做百度的网站运营培训班学费大概多少
  • 个人做网站花多少钱网站如何做推广
  • 北京市政府网站首都之窗百度网站管理员工具
  • sfda的网站的建设特点做企业推广的公司
  • 咸阳市城乡建设规划局网站如何在百度发布广告
  • 杭州电商网站策划设计seo智能优化软件
  • wordpress 摘要 回车手机流畅优化软件
  • 苍南做网站哪里找公司网站建设哪个好
  • 男女做羞羞羞的网站软文代写服务
  • 百度收录动态网站是不是比静态难seo专员招聘
  • 靠谱的建站团队典型的口碑营销案例
  • 专业外贸网站制作价格贵州seo培训
  • 电商运营 网站运营网页设计培训学校
  • asp网站免费模板下载北京seo排名厂家
  • 网站建设 域名 空间黑帽seo技术培训
  • 做网站建设需要什么资质广告招商
  • 源码开发网站建设制作企业网站
  • 怎么做学校官方网站名片seo什么意思
  • 运城网站建设多少钱网站优化公司开始上班了
  • wordpress的替代win10优化
  • 广州海珠做网站搜索引擎优化实训心得
  • 中国建设银行金华分行网站seo优化排名易下拉效率
  • 深圳做网站-信科网络seo排名优化北京
  • java librec 网站开发手机优化软件
  • 网站链接文字seo排名优化软件有
  • 网站与规划设计思路廊坊百度快照优化排名
  • 筑云电商网站建设公司网站seo优化总结
  • 淘宝运营跟做网站哪种工资高富阳seo关键词优化
  • 中国最贵的域名交易福州百度seo代理
  • 关于营销的网站有哪些内容清远新闻最新