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

结果

相关推荐
安之若素^6 分钟前
启用不安全的HTTP方法
java·开发语言
一个天蝎座 白勺 程序猿23 分钟前
Python(28)Python循环语句指南:从语法糖到CPython字节码的底层探秘
开发语言·python
持梦远方1 小时前
C 语言基础入门:基本数据类型与运算符详解
c语言·开发语言·c++
zzywxc7871 小时前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring
灵性花火1 小时前
Qt的前端和后端过于耦合(0/7)
开发语言·前端·qt
DES 仿真实践家2 小时前
【Day 11-N22】Python类(3)——Python的继承性、多继承、方法重写
开发语言·笔记·python
Code Warrior3 小时前
【每日算法】专题五_位运算
开发语言·c++
沐知全栈开发5 小时前
HTML DOM 访问
开发语言
脑袋大大的6 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
二进制person7 小时前
Java SE--方法的使用
java·开发语言·算法