C# 使用自定义属性从对象获取属性值

nic*_*wdy 1 c# reflection custom-attributes

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

节标志属性类

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

我试图通过使用带有部分标志名称的字符串来获取这些属性之一的值。

所以如果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;
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Jer*_*gen 6

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

这是一个例子:

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

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


用法:

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

// get the value by SectionFlag name
var iceAttributeStatement = GetValueBySectionFlag(obj, "APPLICANTSTATEMENT");
Run Code Online (Sandbox Code Playgroud)

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