【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();
        
    }
}

结果

相关推荐
m0_748233649 分钟前
jank实现C++无缝互操作的技术探索
开发语言·c++
向宇it17 分钟前
【推荐100个unity插件】unity易于使用模块化设计的天空、体积云和天气系统——Enviro 3
游戏·3d·unity·c#·游戏引擎
污斑兔22 分钟前
技术随笔:Node.js ESM 中巧用 `-r dotenv/config` 解决环境变量异步加载问题
开发语言·r语言·node.js
我是唐青枫26 分钟前
C#.NET SqlKata 使用详解:优雅构建动态 SQL 查询
sql·c#·.net
ALex_zry37 分钟前
C++中使用gRPC over Unix Domain Sockets的高性能进程间通信技术解析
开发语言·c++·unix
小年糕是糕手38 分钟前
【C语言】函数栈帧的创建和销毁
java·c语言·开发语言·数据结构·c++·链表
ALex_zry1 小时前
构建通用并发下载工具:用Golang重构wget脚本的实践分享
开发语言·重构·golang
努力努力再努力wz1 小时前
【Linux进阶系列】:信号(下)
java·linux·运维·服务器·开发语言·数据结构·c++
21号 11 小时前
21.事务和锁(重点)
开发语言·数据库
zzzsde1 小时前
【C++】stack和queue:使用&&OJ题&&模拟实现
开发语言·c++