简体   繁体   English

有没有办法知道 object 属性值是否已设置或是否为默认值?

[英]Is there a way to know if object property value is already set or is it default?

I am looking for a way to find out if object property value is already set or if it's still default.我正在寻找一种方法来确定 object 属性值是否已设置或是否仍为默认值。 My class:我的class:

class TestModel
{
    public string TestStringProperty { get; set; }
    public int TestIntProperty { get; set; }
    public bool TestBoolProperty { get; set; }
}

and I initialize it:我初始化它:

TestModel model = new()
{
    TestBoolProperty = false,
    TestStringProperty = "testValue",
    TestIntProperty = 1,
};

Later I run this code, to get current and default values for those properties:稍后我运行这段代码,以获取这些属性的当前值和默认值:

foreach (PropertyInfo propertyInfo in model.GetType().GetProperties())
{
    object? currentValue = model.GetType()?.GetProperty(propertyInfo.Name)?.GetValue(model, null);
    object? defaultValue = propertyInfo.GetValue(Activator.CreateInstance(dummyModel.GetType()), null);
    Trace.WriteLine($"Current value for property {propertyInfo.Name} is: {currentValue?.ToString()}");
    Trace.WriteLine($"Default value for property {propertyInfo.Name} is: {defaultValue?.ToString()}");
}

and the output is: output 是:

Current value for property TestStringProperty is: testValue
Default value for property TestStringProperty is: 
Current value for property TestIntProperty is: 1
Default value for property TestIntProperty is: 0
Current value for property TestBoolProperty is: False
Default value for property TestBoolProperty is: False

However, TestBoolProperty (bool) value is the same as default value (false).但是,TestBoolProperty (bool) 值与默认值 (false) 相同。 If I hadn't initialized it, it would still be default.如果我没有初始化它,它仍然是默认的。

My question: Is there a way to find out if this property is set, even if its value the same as default?我的问题:有没有办法查明是否设置了此属性,即使它的值与默认值相同?

Use backing fields and a boolean value:使用支持字段和 boolean 值:

class TestModel
{
    private string _testStringProperty;
    private bool _testStringPropertySet;

    public string TestStringProperty
    {
        get { return _testStringProperty; }
        set { _testStringProperty = value; _testStringPropertySet = true; }
    }

Then you can check the boolean field in addition to the property value.那么除了属性值,还可以查看boolean字段。

As far as I know there's no way to tell the difference between a default value and an assigned value if that value is the same as the default.据我所知,如果默认值与默认值相同,则无法区分默认值和分配值。 @DiplomacyNotWar provided a way around that. @DiplomacyNotWar 提供了一种解决方法。 Another option is to use Nullable and check the HasValue property.另一种选择是使用Nullable并检查HasValue属性。 This will be null until the field is set (or if it was set to null explicitly).这将是null ,直到该字段被设置(或者如果它被显式设置为null )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为属性设置默认值 - Set a default value to a property 有没有一种优雅的方法来为c#中的属性设置默认值? - Is there an elegant way to set a default value for a property in c#? 如何为XAML中表示的对象设置默认值属性 - How to set the default value property for an object represented in XAML 如何在实现接口的每个对象上设置属性的默认值? - How to set a default value of a property on every object that implements an interface? 在自定义属性上设置默认值 - Set default value on a custom property 映射到 CosmosDB 集合时,有什么方法可以将默认值设置为 Entity Framework Core 实体的属性 - Is there any way to set default value to a property of Entity Framework Core entity when mapping to CosmosDB collection 有没有办法在配置实体时将默认值(例如基于 IOptions)设置为忽略的属性? - Is there a way to set a default value (for example based on IOptions) to ignored property when configuring entities? 在实例化之前设置属性默认值 - Set property default value before instantiation 设置WCF DataMember属性的默认值 - Set Default Value Of WCF DataMember Property 将默认值设置为属性并使其可序列化 - Set a default value to a property and make it serializable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM