【问题标题】:C# get property value from object using custom attributeC#使用自定义属性从对象获取属性值
【发布时间】:2018-06-21 12:02:41
【问题描述】:

我有一个带有使用自定义属性的属性的 POCO 类:

应用程序状态标志 POCO 类

public class ApplicationStatusFlags
    {
        public int ApplicationId { get; set; }

        [SectionFlag("APPLICANTPERSONALDETAILS")]
        public bool PersonalDetailsStatus { get; set; }

        [SectionFlag("APPLICANTECREGISTRATION")]
        public bool EcRegistrationStatus { get; set; }

        [SectionFlag("APPLICANTCV")]
        public bool CvUpload { get; set; }

        [SectionFlag("APPLICANTSTATEMENT")]
        public bool IceAttributeStatement { get; set; }

        [SectionFlag("APPLICANTCPD")]
        public bool CpdUpload { get; set; }

        [SectionFlag("APPLICANTORGCHART")]
        public bool OrgChartUpload { get; set; }

        [SectionFlag("APPLICANTSPONSORDETAILS")]
        public bool SponsorDetails { get; set; }
    }

节标志属性类

 [AttributeUsage(AttributeTargets.All)]
    public class SectionFlagAttribute : Attribute
    {
        /// <summary>
        /// This constructor takes name of attribute
        /// </summary>
        /// <param name="name"></param>
        public SectionFlagAttribute(string name)
        {
            Name = name;
        }

        public virtual string Name { get; }
    }

我正在尝试通过使用带有节标志名称的字符串来获取这些属性之一的值。

所以如果var foo = "APPLICANTSPONSORDETAILS" 我会得到SponsorDetails 的布尔值。

示例代码

    updateAppStatusFlag.ApplicationId = applicationId;

    var applicationStatuses =
        await _applicationService
            .UpdateApplicationStatusFlagsAsync<ApplicationStatusFlags>(updateAppStatusFlag);

    var foo = "APPLICANTSPONSORDETAILS";

    var type = applicationStatuses.GetType();

    var test = type.
            GetCustomAttributes(false)
            .OfType<SectionFlagAttribute>()
            .SingleOrDefault()
                       ?.Name == foo;

任何想法如何做到这一点?我知道我可以使用反射,但我在让它工作时遇到了问题。

谢谢

【问题讨论】:

  • 你能告诉我们“我知道我可以使用反射,但我在让它工作时遇到了问题。”
  • 如果多个SectionFlag相等怎么办?还是没有?
  • 在您的示例中,所有属性都有不同的部分标志,是否期望多个属性在某个时候共享部分标志,无论是在相同的类中还是在不同的类中?
  • @J.vanLangen 检查编辑
  • @nickgowdy 问题是,你得到的是类的自定义属性,而不是属性......检查我的答案并进行比较。类也可以包含自定义属性...

标签: c# reflection custom-attributes


【解决方案1】:

在您的示例中,您获取的是类的自定义属性而不是属性。

这是一个例子:

private object GetValueBySectionFlag(object obj, string flagName)
{
    // get the type:
    var objType = obj.GetType();

                // iterate the properties
    var prop = (from property in objType.GetProperties()
                // iterate it's attributes
                from attrib in property.GetCustomAttributes(typeof(SectionFlagAttribute), false).Cast<SectionFlagAttribute>()
                // filter on the name
                where attrib.Name == flagName
                // select the propertyInfo
                select property).FirstOrDefault();

    // use the propertyinfo to get the instance->property value
    return prop?.GetValue(obj);
}

注意:这将仅返回包含具有正确名称的 SectionFlagAttribute 的第一个属性。您可以修改该方法以返回多个值。 (就像属性名/值的集合)


用法:

// a test instance.
var obj = new ApplicationStatusFlags { IceAttributeStatement = true };

// get the value by SectionFlag name
var iceAttributeStatement = GetValueBySectionFlag(obj, "APPLICANTSTATEMENT");

如果返回值为null,则找不到标志或属性值为空。

【讨论】:

    猜你喜欢
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    相关资源
    最近更新 更多