如何获取对象属性的默认值? [英] How to get the default value of an object property?
本文介绍了如何获取对象属性的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一些代码:
foreach (System.Reflection.PropertyInfo pi in myObject.GetType().GetProperties())
{
if (pi.CanWrite)
{
object value = pi.GetValue(Properties, null);
// if (value is not default)
// {
X.addAttribute(pi.Name, value);
// }
}
}
如果该属性位于其DefaultValue,我想做的就是不调用'X.addAttribute ...'行.我认为可以通过某种方式获取属性的DefaultValue,以便进行比较?
What I'm trying to do is not-call the line 'X.addAttribute...' if the property is at its DefaultValue. I assume there's some way of getting the DefaultValue of a property so I can do a comparison?
出于我的目的,我试图获取DefaultValueAttribute定义的默认"值.
For my purpose I am trying to get the 'default' value as defined by DefaultValueAttribute.
我们非常感谢您的帮助,
Any help is appreciated, cheers.
推荐答案
假设您要获取DefaultValue属性,请在PropertyInfo
对象上使用GetCustomAttributes
.
Assuming you're trying to get the DefaultValue attribute, use GetCustomAttributes
on your PropertyInfo
objects.
这篇关于如何获取对象属性的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文