【C#学习——特性】

前言

C#特性学习、主要是用在数据库连接时如何动态创建对应的表,正常开发应该使用如Entity Framework等ORM框架实现自动创建生成。

代码

1、声明特性

csharp 复制代码
[AttributeUsage(AttributeTargets.Property)]
public class PrimaryKeyAttribute : Attribute { }

[AttributeUsage(AttributeTargets.Property)]
public class ForeignKeyAttribute : Attribute
{
    public string ReferenceTable { get; }
    public string ReferenceColumn { get; }

    public ForeignKeyAttribute(string referenceTable, string referenceColumn)
    {
        ReferenceTable = referenceTable;
        ReferenceColumn = referenceColumn;
    }
}

[AttributeUsage(AttributeTargets.Property)]
public class RequiredAttribute : Attribute { }

[AttributeUsage(AttributeTargets.Property)]
public class UniqueAttribute : Attribute { }

2、定义特性方法

csharp 复制代码
//特性方法
public static class AttributeMethod
{
    //反射+泛型打印特性名
    public static void GetAttributies<T>()
    {
        Type objType = typeof(T);
        foreach (var property in objType.GetProperties())
        {
            Console.WriteLine($"Property: {property.Name}");

            if (property.GetCustomAttribute<PrimaryKeyAttribute>() != null)
            {
                Console.WriteLine("  - Primary Key");
            }
            if (property.GetCustomAttribute<ForeignKeyAttribute>() != null)
            {
                var foreignKey = property.GetCustomAttribute<ForeignKeyAttribute>();
                Console.WriteLine($"  - Foreign Key: References {foreignKey.ReferenceTable}.{foreignKey.ReferenceColumn}");
            }
            if (property.GetCustomAttribute<RequiredAttribute>() != null)
            {
                Console.WriteLine("  - Required");
            }
            if (property.GetCustomAttribute<UniqueAttribute>() != null)
            {
                Console.WriteLine("  - Unique");
            }
        }
    }
}

3、创建类并指定特性

csharp 复制代码
public class User
{
    [PrimaryKey]
    public int Id { get; set; }

    [Required]
    [Unique]
    public string Username { get; set; }

    [Required]
    public string Password { get; set; }

    [ForeignKey("Role", "Id")]
    public int RoleId { get; set; }
}

public class Role
{
    [PrimaryKey]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
}

调用方法获取并打印特性名

csharp 复制代码
internal class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("输出User特性:");
        AttributeMethod.GetAttributies<User>();
        Console.WriteLine();
        Console.WriteLine("输出Role特性:");
        AttributeMethod.GetAttributies<Role>();
        
        Console.ReadKey();
        
    }
}

结果

相关推荐
大飞pkz2 小时前
【设计模式】责任链模式
开发语言·设计模式·c#·责任链模式
gplitems1233 小时前
Gunslinger – Gun Store & Hunting WordPress Theme: A Responsible
开发语言·前端·javascript
大飞pkz4 小时前
【设计模式】六大基本原则
开发语言·设计模式·c#·六大原则
iCxhust4 小时前
Intel8259汇编串口接收转C语言
c语言·开发语言·汇编
掘根5 小时前
【Qt】布局管理器
开发语言·qt
半夏知半秋5 小时前
skynet-socket.lua源码分析
服务器·开发语言·学习·架构·lua
西猫雷婶6 小时前
random.shuffle()函数随机打乱数据
开发语言·pytorch·python·学习·算法·线性回归·numpy
来生硬件工程师6 小时前
CH582 GPIO
c语言·开发语言·单片机
椒颜皮皮虾6 小时前
DeploySharp开源发布:让C#部署深度学习模型更加简单
c#·openvino
fly-phantomWing6 小时前
在命令提示符页面中用pip命令行安装Python第三方库的详细步骤
开发语言·python·pip