Custom Attributes on Class Members

Posted by ccook on Stack Overflow See other posts from Stack Overflow or by ccook
Published on 2009-03-10T14:06:59Z Indexed on 2011/01/09 16:53 UTC
Read the original article Hit count: 292

I am using a Custom Attribute to define how a class's members are mapped to properties for posting as a form post (Payment Gateway). I have the custom attribute working just fine, and am able to get the attribute by "name", but would like to get the attribute by the member itself.

For example:

getFieldName("name");

vs

getFieldName(obj.Name);

The plan is to write a method to serialize the class with members into a postable string.

Here's the test code I have at this point, where ret is a string and PropertyMapping is the custom attribute:

foreach (MemberInfo i in (typeof(CustomClass)).GetMember("Name"))
{
    foreach (object at in i.GetCustomAttributes(true))
    {
        PropertyMapping map = at as PropertyMapping;
        if (map != null)
        {
            ret += map.FieldName;
        }
    }
}

Thanks in advance!

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection