As per title, can I find out the history of a property or variable, in terms of whether it has been set or not?
Reason for this is I have some query classes, any properties that have not been set should not be included in the generated query.
Currently I am just trying to add a PropertyInfo instance (I need the name of the property, this is vital to the query generati开发者_C百科on due to mappings) to a list of properties which have been set in set{}. This is ugly though as it requires more code for what should be classes that only contain properties and no logic (i.e. no removal of stufffrom a list).
Is there something built-in I can use or a more elegant method?
Assuming you're talking about dynamic entities, you could check for null using reflection but this won't tell you anything if the original value was null (or zero in case of numeric data-types).
But best way is to make it implement INotifyPropertyChanged and have a list of properties that have been materialized.
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;
}
Make the properties nullable, e.g. of type int?
instead of type int
, so that their value is null
if they haven't been set.
精彩评论