C# C使用自定义属性从对象获取属性值
我有一个POCO类,其属性使用自定义属性: 应用程序状态标志POCO类 节标志属性类 有什么办法吗?我知道我可以使用反射,但我在让它工作时遇到了问题C# C使用自定义属性从对象获取属性值,c#,reflection,custom-attributes,C#,Reflection,Custom Attributes,我有一个POCO类,其属性使用自定义属性: 应用程序状态标志POCO类 节标志属性类 有什么办法吗?我知道我可以使用反射,但我在让它工作时遇到了问题 谢谢在您的示例中,您得到的是类的customattributes,而不是属性 以下是一个例子: private object GetValueBySectionFlag(object obj, string flagName) { // get the type: var objType = obj.GetType();
谢谢在您的示例中,您得到的是类的customattributes,而不是属性 以下是一个例子:
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);
}
如果返回值为null,则找不到标志或属性值为null。在您的示例中,您将获得类的customattributes,而不是属性 以下是一个例子:
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);
}
如果返回值为null,则找不到标志或属性的值为null。能否向我们展示“我知道我可以使用反射”,但在使其工作时遇到问题?如果多个SectionFlag相等,该怎么办?或者没有?在您的示例中,所有属性都有不同的节标志,是否期望多个属性在某个点(在相同或不同的类中)共享节标志?@J.vanLangen Checkedit@nickgowdy问题是,您得到的是类的customattributes,而不是属性。。。检查我的答案并比较。类也可以包含customattributes…你能告诉我们我知道我可以使用反射,但我在使它工作时遇到问题吗?如果多个SectionFlag相等怎么办?或者没有?在您的示例中,所有属性都有不同的节标志,是否期望多个属性在某个点(在相同或不同的类中)共享节标志?@J.vanLangen Checkedit@nickgowdy问题是,您得到的是类的customattributes,而不是属性。。。检查我的答案并比较。类还可以包含自定义属性。。。
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;
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);
}
// a test instance.
var obj = new ApplicationStatusFlags { IceAttributeStatement = true };
// get the value by SectionFlag name
var iceAttributeStatement = GetValueBySectionFlag(obj, "APPLICANTSTATEMENT");