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

结果

相关推荐
晓晓hh15 小时前
JavaSe学习——基础
java·开发语言·学习
清水白石00815 小时前
Python 内存陷阱深度解析——浅拷贝、深拷贝与对象复制的正确姿势
开发语言·python
phltxy15 小时前
算法刷题|模拟思想高频题全解(Java版)
java·开发语言·算法
愚者游世15 小时前
template学习大纲
开发语言·c++·程序人生·面试·visual studio
阿里嘎多学长15 小时前
2026-03-11 GitHub 热点项目精选
开发语言·程序员·github·代码托管
宵时待雨15 小时前
C++笔记归纳10:继承
开发语言·数据结构·c++·笔记·算法
csbysj202015 小时前
TypeScript String
开发语言
bugcome_com15 小时前
C# 泛型(Generic)完全指南:从基础到高级应用
c#
小温冲冲15 小时前
QML vs Qt Widgets:深度对比与选型实战指南
开发语言·c++·qt
smchaopiao15 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法