相关疑难解决方法(0)

类成员的自定义属性

我正在使用自定义属性来定义类的成员如何映射到属性以作为表单发布(支付网关)发布.我有自定义属性工作正常,并能够通过"名称"获取属性,但希望成员本身获取属性.

例如:

getFieldName("name");
Run Code Online (Sandbox Code Playgroud)

VS

getFieldName(obj.Name);
Run Code Online (Sandbox Code Playgroud)

计划是编写一个方法,将带有成员的类序列化为一个postable字符串.

这是我此时的测试代码,其中ret是一个字符串,PropertyMapping是自定义属性:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

c# reflection attributes custom-attributes

8
推荐指数
1
解决办法
3210
查看次数

标签 统计

attributes ×1

c# ×1

custom-attributes ×1

reflection ×1