簡體   English   中英

有沒有辦法讓我訪問 C# 類屬性?

[英]Is there a way for me to access a C# class attribute?

有沒有辦法讓我訪問 C# 類屬性?

例如,如果我有以下課程:

...
[TableName("my_table_name")]
public class MyClass
{
    ...
}

我可以做這樣的事情:

MyClass.Attribute.TableName => my_table_name

謝謝!

您可以使用反射來獲取它。 這是一個完整的包含示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    public class TableNameAttribute : Attribute
    {
        public TableNameAttribute(string tableName)
        {
            this.TableName = tableName;
        }
        public string TableName { get; set; }
    }

    [TableName("my_table_name")]
    public class SomePoco
    {
        public string FirstName { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var classInstance = new SomePoco() { FirstName = "Bob" };
            var tableNameAttribute = classInstance.GetType().GetCustomAttributes(true).Where(a => a.GetType() == typeof(TableNameAttribute)).Select(a =>
            {
                return a as TableNameAttribute;
            }).FirstOrDefault();

            Console.WriteLine(tableNameAttribute != null ? tableNameAttribute.TableName : "null");
            Console.ReadKey(true);
        }
    }    
}

您可以使用Attribute.GetCustomAttribute方法:

var tableNameAttribute = (TableNameAttribute)Attribute.GetCustomAttribute(
    typeof(MyClass), typeof(TableNameAttribute), true);

然而,這對我來說太冗長了,你可以通過以下小擴展方法讓你的生活更輕松:

public static class AttributeUtils
{
    public static TAttribute GetAttribute<TAttribute>(this Type type, bool inherit = true) where TAttribute : Attribute
    {
        return (TAttribute)Attribute.GetCustomAttribute(type, typeof(TAttribute), inherit);
    }
}

所以你可以簡單地使用

var tableNameAttribute = typeof(MyClass).GetAttribute<TableNameAttribute>();

這是一個擴展,通過擴展 object 為您提供屬性助手,可以使事情變得更容易。

namespace System
{
    public static class ReflectionExtensions
    {
        public static T GetAttribute<T>(this object classInstance) where T : class
        {
            return ReflectionExtensions.GetAttribute<T>(classInstance, true);
        }
        public static T GetAttribute<T>(this object classInstance, bool includeInheritedAttributes) where T : class
        {
            if (classInstance == null)
                return null;

            Type t = classInstance.GetType();
            object attr = t.GetCustomAttributes(includeInheritedAttributes).Where(a => a.GetType() == typeof(T)).FirstOrDefault();
            return attr as T;
        }
    }
}

這會將我之前的答案變成:

class Program
{
    static void Main(string[] args)
    {
        var classInstance = new SomePoco() { FirstName = "Bob" };
        var tableNameAttribute = classInstance.GetAttribute<TableNameAttribute>();

        Console.WriteLine(tableNameAttribute != null ? tableNameAttribute.TableName : "null");
        Console.ReadKey(true);
    }
}   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM