如何获取对象属性的默认值?

发布于 2024-08-21 16:22:16 字数 492 浏览 4 评论 0原文

一些代码:

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,以便我可以进行比较?

出于我的目的,我试图获取由 DefaultValueAttribute 定义的“默认”值。

任何帮助表示赞赏,干杯。

Some code:

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);
        // }
    }
}

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?

For my purpose I am trying to get the 'default' value as defined by DefaultValueAttribute.

Any help is appreciated, cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你没皮卡萌 2024-08-28 16:22:16

下面是我用来获取任何运行时类型的默认值的方法,对于非值类型它将返回“null”,否则它将返回默认值类型(它包括额外缓存值类型)性能):

private static readonly Dictionary<Type, object> DefaultValueTypes 
    = new Dictionary<Type, object>();

public static object GetDefaultValue(Type type)
{
    if (!type.IsValueType) return null;

    object defaultValue;
    lock (DefaultValueTypes)
    {
        if (!DefaultValueTypes.TryGetValue(type, out defaultValue))
        {
            defaultValue = Activator.CreateInstance(type);
            DefaultValueTypes[type] = defaultValue;
        }
    } 

    return defaultValue;
}

Below is the method I use to get a default value of any runtime-type it will return 'null' for non value types otherwise it will return the default value type (it includes caching of value types for extra perf):

private static readonly Dictionary<Type, object> DefaultValueTypes 
    = new Dictionary<Type, object>();

public static object GetDefaultValue(Type type)
{
    if (!type.IsValueType) return null;

    object defaultValue;
    lock (DefaultValueTypes)
    {
        if (!DefaultValueTypes.TryGetValue(type, out defaultValue))
        {
            defaultValue = Activator.CreateInstance(type);
            DefaultValueTypes[type] = defaultValue;
        }
    } 

    return defaultValue;
}
瑾夏年华 2024-08-28 16:22:16

假设您尝试获取 DefaultValue 属性,请在 PropertyInfo 对象上使用 GetCustomAttributes

Assuming you're trying to get the DefaultValue attribute, use GetCustomAttributes on your PropertyInfo objects.

伏妖词 2024-08-28 16:22:16

首先,您必须始终使用您想要的默认值来初始化该字段。

类似的东西

    class UnitManager
    {
      private int value;

      [DefaultValue("0")]
      public int Value
      {
       get { return this.value; }
       set { this.value = value; } 
      }
    } 

然后简单地使用

    UnitManager manager = new UnitManager()

    int startValue = manager.Value;

First, You must always initialize the field with value you want as a default.

Something similar

    class UnitManager
    {
      private int value;

      [DefaultValue("0")]
      public int Value
      {
       get { return this.value; }
       set { this.value = value; } 
      }
    } 

Then simply use

    UnitManager manager = new UnitManager()

    int startValue = manager.Value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文