dimanche 24 mars 2019

C# Get Functions that have an attribute [duplicate]

This question already has an answer here:

Hello Guys, I try to get all functions that have an attribute from type TestAttribute. I need the function name (e.g. TestFunction) and in the attribute the PropertyName (TestAttribute.PropertyName)

namespace WpfApp2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MemberInfo[] memberInfo = typeof(MainWindow).GetMethods();
            var test = memberInfo.Select(a => 
                               a.GetCustomAttributes<TestAttribute>());

            //Code...  
        }

        [TestAttribute("MyProperty")]
        public void TestFunction()
        {

        }

        public void NoAttributeFunction()
        {

        }
    }

    public class TestAttribute : Attribute
    {
        public string PropertyName { get; set; }

        public TestAttribute(string property)
        {
            PropertyName = property;
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire