Attribute 特性

Posted torey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Attribute 特性相关的知识,希望对你有一定的参考价值。

//数据库表名与类名不一致时,用到的特性
public class TableAttribute : Attribute
{
private string _TableName = null;

public TableAttribute(string tableName)
{
this._TableName = tableName;
}

public string GetTableName()
{
return this._TableName;
}

}

public static class Extend
{
/// <summary>
/// 根据类型获取表名称
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string GetTableName<T>(this T t) where T : new()
{
Type type = t.GetType();
object[] oAttributeList = type.GetCustomAttributes(true);
foreach (var item in oAttributeList)
{
if (item is TableAttribute)
{
TableAttribute attribute = item as TableAttribute;
return attribute.GetTableName();
}
}

return type.Name;
}
}

static void Main(string[] args)
{
try
{
Console.WriteLine("欢迎来到.net高级班vip课程,今天的内容是特性和AOP");
People people = new People();

UserModel user = new UserModel();
user.Id = 1;

string name = user.GetTableName<UserModel>();

string remark = UserState.Normal.GetRemark();

BaseDAL.Save<UserModel>(user);


#region AOP show
Console.WriteLine("***********************");
Decorator.Show();
Console.WriteLine("***********************");
Proxy.Show();
Console.WriteLine("***********************");
CastleProxy.Show();
Console.WriteLine("***********************");
UnityAOP.Show();
#endregion
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}

 

以上是关于Attribute 特性的主要内容,如果未能解决你的问题,请参考以下文章

detectron2报AttributeError: Attribute ‘evaluator_type‘ does not exist in the metadata of dataset(代码片段

C# 自定义特性(Attribute)详解

项目启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedde(代码片段

Attribute(特性)

Attribute特性验证模型model

C#之特性(Attribute)