Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a set up similar to this one:

C#
public class MyAttribute : Attribute
    {
        public string ConfigName { get; set; }
    }

    public class PropertyClass
    {
        [MyAttribute(ConfigName = "Some")]
        public string MyProperty { get; set; }

        public object PropertyValueByAttribute(string attributeName)
        {
            return this.GetType().GetProperties().Where(x => ((MyAttribute)(x.GetCustomAttributes(typeof(MyAttribute), false)[0])).ConfigName == "Some").ToList()[0].GetValue(this, null);
        }
    }


Now, I am trying to find value of MyProperty based on value of the ConfigName parameter for the property attribute. There's a lot of reflection and casting going on here. Is there a better way to achieve this?

P.S. Sorry for the long LINQ statement. I am just trying to figure out the way and ended up with this. Ain't gonna happen in real application.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900