Is there a way to find out if a property or variable is set?

As in the title, can I find out the history of a property or variable in terms of whether it was set or not?

The reason for this is because I have some query classes, any properties that have not been set should not be included in the generated query.

I'm currently just trying to add an instance of PropertyInfo (I need a property name, this is vital for generating queries due to mappings) to the list of properties that were set in set {}. This is ugly, although it requires more code for classes that contain only properties and have no logic (i.e., Deleting from a list).

Is there something inline that I can use or a more elegant method?

+3
source share
2 answers

Set properties to zero, for example. type int?instead of type int, so their value nullif they were not set.

+3
source

Assuming you are talking about dynamic entities, you can check for null reflection usage, but that won't say anything if the original value was null (or null in the case of numeric data types).

But the best way is to implement INotifyPropertyChanged and have a list of properties that have been implemented.

public class Item : INotifyPropertyChanged
{
    private List<string> _MaterializedPropertiesInternal;
    private List<string> MaterializedPropertiesInternal
    {
        get
        {
            if (_MaterializedPropertiesInternal==null) 
                _MaterializedPropertiesInternal = new List<string>();
            return _MaterializedPropertiesInternal;
        }
    }

    private ReadOnlyCollection<string> _MaterializedProperties;
    public IEnumerable<string> MaterializedProperties
    {
        get 
        {
            if (_MaterializedProperties==null) _MaterializedProperties = 
              new ReadOnlyCollection<string>(MaterializedPropertiesInternal);
            return _MaterializedProperties;
        }
    }

    private int _MyProperty;
    public int MyProperty
    {
        get { return _MyProperty; }
        set
        {
            _MyProperty = value;
            OnPropertyChanged("MyProperty");
        }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        if(PropertyChanged != null) PropertyChanged(this, 
          new PropertyChangedEventArgs(propertyName));
        MaterializedPropertiesInternal.Add(propertyName);
    }


    public event PropertyChangedEventHandler PropertyChanged;
}
+4
source

Source: https://habr.com/ru/post/1781826/


All Articles