Click here to Skip to main content
14,209,109 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a set up similar to this one:

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)



Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190617.3 | Last Updated 22 May 2013
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100