c#——特性

定义

特性的定义——用于在运行时传递程序中各种元素(类,方法、结构、枚举、组件等)的行为信息的声明性标签。----特性不等于注释。

,net 有两种类型特性——预定义特性和自定义特性

创建自定义特性

创建的自定义特性的命名方式——自定义特性+Attribute。该类继承于Attribute

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

namespace WinformControl.CustAttrs
{
    //指定特性应用于哪些元素
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method)]
    public class RemarkAttribute : Attribute
    {


        public string Description { get; set; }
        public RemarkAttribute(string desp)
        {
            Description = desp;
        }

    }  
}

然后在编写类时如下进行自定义特性的添加——

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinformControl.CustAttrs;

namespace WinformControl
{
    //自定义特性
    [Remark("UserInfos")]
   public  class UserInfo
    {
        //[Remark("空参构造函数")],只对类,属性,方法有效
        public UserInfo()
        {
            Console.WriteLine("空参构造函数方法");
        }

        public UserInfo(int aga,string name)
        {
            Console.WriteLine(aga.ToString()+name);
        }

        [Remark("用户ID")]
        public int UserID
        {
            get;
            set;
        }
        [Remark("用户年龄")]
        public  int UserAge
        {
            get;set;
        }
        [Remark("用户名字")]
        public string UserName
        {
            get;set;
        }
        private string gender = "female";
        public  string Gender
        {
            get;set;
        }
        [Remark("无参Show方法")]
        public void Show()
        {
            Console.WriteLine("你好");
        }
        [Remark("传参Show方法")]
        public void Show(UserInfo userInfo)
        {
            Console.WriteLine(userInfo.gender);
        }
        [Remark("静态show方法")]
        public static void ShowInfo()
        {
            Console.WriteLine("调用静态方法");
        }
    }
}

编写一个获取类型注释的类——

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WinformControl.CustAttrs;

namespace WinformControl
{
  public   class RemarkHelper
    {
        /// <summary>
        /// 获取指定类型的注释
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetDescription<T>()
        {
            Type type = typeof(T);
            string desp = "";
           RemarkAttribute attr= type.GetCustomAttribute<RemarkAttribute>();
            if (attr != null)
            {
                desp = attr.Description;
            }
            return desp;
        }
        /// <summary>
        /// 获取指定成员的注释
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        public static string GetProDescription(PropertyInfo prop)
        {
            string desp = "";
            RemarkAttribute arrt = prop.GetCustomAttribute<RemarkAttribute>();//获取属性的自定义特性
            if (arrt != null)
            {
                desp = arrt.Description;
            }
            return desp;
        }

        public static string GetMethodDescription(MethodInfo method)
        {
            string desp = "";
            RemarkAttribute arrt = method.GetCustomAttribute<RemarkAttribute>();//获取属性的自定义特性
            if (arrt != null)
            {
                desp = arrt.Description;
            }
            return desp;
        }
    }
}

使用——

 #region 特性
            UserInfo user1 = new UserInfo();
            user1.UserID = 1;
            user1.UserName = "黎明";

            //类、属性、方法、注释文件拿到
            Type userType = typeof(UserInfo);

            //获取指定类型的特性
            Console.WriteLine($"CourseInfo的注释信息:" + RemarkHelper.GetDescription<UserInfo>());
            //获取属性上的注释
            var props = userType.GetProperties();
            foreach(var p in props)
            {
                Console.WriteLine($"{p.Name} 的注释信息:" + RemarkHelper.GetProDescription(p));
            }
            //获取方法的特性
            var methods = userType.GetMethods();
            foreach (var m in methods)
            {
                Console.WriteLine($"{m.Name} 的注释信息:" + RemarkHelper.GetMethodDescription(m));
            }

            #endregion

总结——

  //1.声明自定义特性——新建一个自定义特性类
  //2.构建特性——在我们想要应用的属性或者类,方法等上构建特性
  //3.应用自定义特性——使用这些特性(主要是通过反射)


扩展方法与特性的应用

表名映射用到的就是就是扩展方法加特性来实现的。

新建一个表特性类——

[AttributeUsage(AttributeTargets.Class)]
    public  class TableAttribute:Attribute
    {
        public string TableName { get; set; }
        public TableAttribute(string tableName)
        {
            TableName = tableName;
        }
    }

然后我们需要理解什么是扩展方法——就是一个静态类中的静态方法

在调用时,能够直接在扩展方法指定的类型的变量后面直接  变量 .方法  就可以i调用,而不需要用类似与StringBuilder.GetInt(变量)来实现。

例如—


    public static  class StringHelper
    {
        /// <summary>
        /// 将字符串转换为Int类型   扩展方法:两个static  然后传入的变量前面要加this
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int GetInt(this string str)
        {
            int reInt = 0;
            int.TryParse(str, out reInt);
            return reInt;
        }
    }

使用时——

有区别

  string str = "1";
                int intVal = str.GetInt();//调用扩展方法
                int val1 = StringHelper.GetInt(str);

 

 静态方法创建如下——

//扩展方法:静态类中静态方法
     public static  class AttributeHelper
    {
        /// <summary>
        /// 获取指定类型对应的表名
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
         public static string GetTableName(this Type type)
        {
            string tableName = "";
            TableAttribute attr = type.GetCustomAttribute<TableAttribute>();
            if (attr != null)
            {
                tableName = attr.TableName;
            }
            else
                tableName = type.Name;
            return tableName;
        }
    }

使用时——

 //表名映射:
                TableAttribute tableAttr = courseType.GetCustomAttribute<TableAttribute>();
                string tableName = "";
                if (tableAttr!=null)
                {
                   tableName= tableAttr.TableName;
                }
                else
                {
                    tableName = courseType.Name;
                }
                Console.WriteLine($"CourseInfo对应的表名是:{tableName}");

                //表名映射实现步骤:
                //1.建特性:TableAttribute
                //2.应用特性,指定真实表名
                //3.封装扩展方法:GetTableName(this  Type type)
                //4.指定类型的Type对象调用 GetTableName()即可获取到表名


                string tableName1 = courseType.GetTableName();
                course.ShowCourse();//实例方法调用

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matrix Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值