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

结果

相关推荐
Code Warrior40 分钟前
【每日算法】专题五_位运算
开发语言·c++
沐知全栈开发3 小时前
HTML DOM 访问
开发语言
脑袋大大的4 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
二进制person5 小时前
Java SE--方法的使用
java·开发语言·算法
OneQ6665 小时前
C++讲解---创建日期类
开发语言·c++·算法
码农不惑6 小时前
2025.06.27-14.44 C语言开发:Onvif(二)
c语言·开发语言
Coding小公仔7 小时前
C++ bitset 模板类
开发语言·c++
小赖同学啊8 小时前
物联网数据安全区块链服务
开发语言·python·区块链
shimly1234568 小时前
bash 脚本比较 100 个程序运行时间,精确到毫秒,脚本
开发语言·chrome·bash