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

C# 获取应用于父级中此属性的属性,c#,reflection,attributes,C#,Reflection,Attributes,是否可以从属性本身内部获取应用于属性的属性值? 例如: public class TestModel { [TestAttribute(Value="TestValue")] public TestClass TestProperty { get; set; } [TestAttribute(Value="AnotherValue")] public TestClass AnotherTestProperty { get; set; } } public cl

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

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";
        }
    }

如何确保propertyName是TestProperty?这只是一个示例,不是一个完全可以使用的代码。稍后hje可以为它或其他东西创建一个参数。但这就是它的工作方式。@Selman22我改进了我的代码。他的类名是TestClass,所以我认为他只想要一个示意图答案…当然你可以这样做,但它仍然不能回答问题。他希望在不传递显式参数的情况下执行此操作,比如调用TestModel.TestProperty.ReturnSomething时,它应该返回TestValue,这在我看来是不可能的。不,他说:调用哪个属性ReturnSomething,请阅读完整的问题。