获取应用于父级中此属性的属性

本文关键字:属性 应用于 获取 | 更新日期: 2023-09-27 18:29:36

是否可以从属性本身内部获取应用于属性的属性值?例如:

public class TestModel
{
    [TestAttribute(Value="TestValue")]
    public TestClass TestProperty { get; set; }
    [TestAttribute(Value="AnotherValue")]
    public TestClass AnotherTestProperty { get; set; }
}
public class TestClass
{
    public string ReturnSomething()
    {
        // Get 'TestValue' or 'AnotherValue'
        // from 'TestAttribute' here depending on
        // which property ReturnSomething() is called on
    }
}

编辑:澄清。我试图在不将任何对父级的引用或任何表示属性名称的字符串传递到ReturnSomething方法中的情况下实现这一点。

获取应用于父级中此属性的属性

这对我有效:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
            TestClass testClass = new TestClass();
            Console.WriteLine("Return-Value: " + testClass.ReturnSomething("TestProperty", typeof(TestModel)));
            Console.WriteLine("Return-Value: " + testClass.ReturnSomething("AnotherTestProperty", typeof(TestModel)));
            Console.ReadLine();
        }
    }
    public class TestAttribute : Attribute
    {
        public string Value
        {
            get;
            set;
        }
    }
    public class TestModel
    {
        [TestAttribute(Value = "TestValue")]
        public TestClass TestProperty { get; set; }
        [TestAttribute(Value = "AnotherValue")]
        public TestClass AnotherTestProperty { get; set; }
    }
    public class TestClass
    {
        public string ReturnSomething(string propertyName, Type modelType)
        {
            string returnValue = "";
            foreach (var property in modelType.GetProperties())
            {
                if (property.Name == propertyName)
                {
                    // Find Attributes
                    Attribute[] cAttributes = property.GetCustomAttributes(true).OfType<Attribute>().ToArray();
                    if (cAttributes != null)
                    {
                        // Iterate throught all attributes
                        foreach (System.Attribute cAttribute in cAttributes)
                        {
                            if (cAttribute is TestAttribute)
                            {
                                returnValue = (cAttribute as TestAttribute).Value;
                                break; // Break after first
                            }
                        }
                    }
                }
            }
            return returnValue;
        }
    }
}

是的,可以使用.net反射在属性本身中获取属性的属性。然而,要做到这一点,您必须充实属性的getter代码,而不是使用自动属性。

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    class TestAttribute : Attribute
    {
        public string Value { get; set; }
    }
    [Test(Value = "Hello")]
    public string MyTestProperty
    {
        get
        {
            var typeInfo = GetType();
            // Get the PropertyInfo descriptor for this property
            var propInfo = typeInfo.GetProperty("MyTestProperty");
            // Get custom attributes applied to the property
            var attr = propInfo.GetCustomAttributes(false).OfType<TestAttribute>().FirstOrDefault();
            // If we have an attribute, then return its Value
            return attr != null ? attr.Value : "No Attribute";
        }
    }