I have a considerable number of strings in my application that need to be cleared each time I get new data from my source. I'd like to use something akin to string.Empty, but I am unsure of how to implement this. Ideally, I'd also like to do this only once, rather than for each separate string.
Pseudo-code:
foreach (string in application)
{
this.empty
}
Am I thinking on the right track?
Some of my code is as follows:
classtoinstantiate
public string Str1;
private string str1 {get {return Str1;}}
public void DoStuff()
{
doStuff();
}
private void doStuff()
{
//dostuff
}
And Form1.cs
classtoinstantiate class1 = new classtoinstantiate();
class.DoStuff();
//I would like to then clear the *public* iteration of string Str1 here,
//before I DoStuff() again.
String.Empty represents a not null empty string.
If you want to clear a large amount of data (string/non string) you can encapsulate all of the variables in one class and create a Clean() method that goes through all the variables and clears them or instantiate that class when you need a fresh copy when you set the default values in the constructor.
The use of class.Empty is from what I understand to have a well defined instance of what is an empty instance.
Given your comments I get the feeling that you only want to clear the strings, have a look at this C# like pseudo code:
public void ClearString(IEnumerable<object> stuffToClear)
{
// go through all the objects to clear
foreach (var item in stuffToClear)
{
// get the properties to clear
var props = from prop in item.GetType().GetProperties()
where prop.PropertyType == typeof(string) // or another type or filter
select prop;
for (var p in props)
{
// clear it
p.SetValue(item, string.Empty);
}
}
}
Not that I'm writing this in freehand, all calls will surely not be correct.
That's the basic OOP concept: construct object when you need it, destroy at the end. Constructing part always deals with default values, which is exactly what you need.
For managed objects (string) simply create a new instance of a class holding all data what has to be reset (cleared):
class SomeDataStorage
{
// default is null
public string Data1 {get; set;}
private string _data2 = "default value";
public string Data2 { get {return _data2;} set {_data2 = value;}}
}
Then you construct this object when you need it
foreach (string in application)
{
var data = new SomeDataStorage(); // default values
...
}
It will be automagically destroyed when going out of scope (leaving { } or exiting function).
For unmanaged objects, implement IDisposable and consider to use using() { } often to auto-dispose.
You can have application-wide instance of SomeDataStorage. Simply assign a new object (construct new instance) to reset values to default.
To make it even more clear:
class App
{
public SomeDataStorage MyData;
public App()
{
Reset();
}
// call this when you need to init for the first time or simply reset to default
public void Reset()
{
MyData = new SomeDataStorage();
}
}
I suggest to put all your strings in to a class and dispose the object if you get new data
public class StringCollection
{
public string StringProp1 { get; set; }
public string StringProp2 { get; set; }
public string StringProp3 { get; set; }
public string StringProp4 { get; set; }
// .... more properties here
// this property won't be touched when clearing
public int SomeOtherProperty{ get; set; }
public void ClearStrings()
{
// returns all public properties
foreach (var prop in this.GetType().GetProperties())
{
// "clear" only properties of type String and those that have a public setter
if (prop.PropertyType == typeof(string) && prop.CanWrite)
prop.SetValue(this, string.Empty, null); // <- "clear" value of the property
}
}
or, in a more general manner - use extension methods:
public class StringCollection
{
public string StringProp1 { get; set; }
public string StringProp2 { get; set; }
public string StringProp3 { get; set; }
public string StringProp4 { get; set; }
public int SomeOtherProperty { get; set; }
}
public class SomeOtherClass
{
public string a1 { get; set; }
public string a2 { get; set; }
public string a3 { get; set; }
public DateTime d1 { get; set; }
public int SomeOtherProperty { get; set; }
}
public static class MyExtensions
{
public static void ClearStrings(this Object obj)
{
// returns all public properties
foreach (var prop in obj.GetType().GetProperties())
{
// "clear" only properties of type String and those that have a public setter
if (prop.PropertyType == typeof(string) && prop.CanWrite)
prop.SetValue(obj, string.Empty, null); // <- "clear" value of the property
}
}
}
use the code:
StringCollection scol2 = new StringCollection();
// ... do soemthing
scol2.ClearStrings();
SomeOtherClass obj = new SomeOtherClass();
// ... do something
obj.ClearStrings();
Related
There are few properties that need to be in sync and required null check before it's use. They are on different class.
What will be the better way to keep them in sync?
Example:
public class A
{
public string Name { get; set; }
public string VNumber { get; set; }
public string VLNumber { get; set; }
}
//Method One has below check:
if (new[] { A.Name, A.VNumber, A.VLNumber }
.Any(string.IsNullOrEmpty))
{
//Some error message;
}
//Somewhere else:
public class B
{
public string Name { get; set; }
public string VNumber { get; set; }
public string VLNumber { get; set; }
}
//Method Two has below check:
if (string.IsNullOrEmpty(B.Name))
{
return false;
}
if (string.IsNullOrEmpty(B.VNumber))
{
return false;
}
if (string.IsNullOrEmpty(B.VLNumber))
{
return false;
}
Since class A and class B has the same properties, they could be one (or at least inherit a common base class). The null check also seems to make use of the same logic, so it would be reasonable to put it in the base class too.
// Abstract base class (no instance of it can be made).
public abstract class BaseClass
{
public string Name { get; set; }
public string VNumber { get; set; }
public string VLNumber { get; set; }
// Logic for the common null checks.
// This logic will be used for all of BaseClass's sub classes.
public bool AnyPropertyIsNull()
{
return string.IsNullOrEmpty(Name)
|| string.IsNullOrEmpty(VNumber)
|| string.IsNullOrEmpty(VLNumber);
}
}
// Inherits base class
public class A : BaseClass
{ }
// Inherits base class
public class B : BaseClass
{ }
Example usage of the classes:
var b = new B();
bool bHasNullValues = b.AnyPropertyIsNull();
If you want a universal solution to check any class's public and string properties then you need to use reflections in c# (see Reflections in c#).
For this case you may use this method to check any class's string and public properties to null or empty:
public bool CheckAnyStringPublicPropertyIsNotEmpty(object obj)
{
var t = obj.GetType();
var properties = t.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(w => w.CanRead && w.CanWrite)
.Where(w => w.PropertyType == typeof(string))
.Where(w => w.GetGetMethod(true).IsPublic)
.Where(w => w.GetSetMethod(true).IsPublic);
foreach (var prop in properties)
{
var propValue =(t.GetProperty(prop.Name).GetValue(obj,null)??string.Empty).ToString();
if (string.IsNullOrEmpty(propValue))
return false;
}
return true;
}
and you may use something like this in your case:
var aObj = new A();
var result1 = CheckAnyStringPublicPropertyIsNotEmpty(aObj); //false
var bObj = new B(){Name="BName",VLNumber="2",VNumber="3"}
var result2 = CheckAnyStringPublicPropertyIsNotEmpty(bObj); //true
I am trying to print out an object that implements TableEntity class, without those that should be Ignored regarding the persistence. The approach I generally use to print out objects is to use the StatePrinter.
public class MyEntity : TableEntity
{
public string MyProperty { get; set; }
[IgnoreProperty]
public string MyIgnoredProperty { get; set; }
public override string ToString()
{
Stateprinter printer = new Stateprinter();
return printer.PrintObject(this);
}
}
While this works pretty good for any kind of classes, with this MyEntity class it also prints the MyIgnoredProperty. Is there a clever way to also ignore the properties that have [IgnoredProperty] as attribute when printing out the object?
You can configure what fields/properties the Stateprinter cares about by configuring what "field harvester" to use.
Here's a simple field harvester that only returns public properties without the 'IgnoreProperty' attribute.
class PersistencePropertiesHarvester : IFieldHarvester
{
public bool CanHandleType(Type type)
{
return typeof(TableEntity).IsAssignableFrom(type);
}
public List<SanitizedFieldInfo> GetFields(Type type)
{
var fields = new HarvestHelper().GetFieldsAndProperties(type);
return fields.Where(IsPerstistenceProperty).ToList();
}
private static bool IsPerstistenceProperty(SanitizedFieldInfo field)
{
return
// Only return properties ...
field.FieldInfo.MemberType == MemberTypes.Property
&&
// ... that has a public get method ...
(field.FieldInfo as PropertyInfo)?.GetGetMethod(false) != null
&&
// ... that does not have the IgnoreProperty attribute
field.FieldInfo.GetCustomAttribute<IgnoreProperty>() == null
;
}
}
Then you use it like this:
public class MyEntity : TableEntity
{
public string MyProperty { get; set; }
[IgnoreProperty]
public string MyIgnoredProperty { get; set; }
public override string ToString()
{
Stateprinter printer = new Stateprinter();
printer.Configuration.Add(new PersistencePropertiesHarvester());
return printer.PrintObject(this);
}
}
And the result of new MyEntity().ToString() is now
new MyEntity()
{
MyProperty = null
}
I need to instantiate a list-property where the generic type can be anything.
So my Main-method looks like this: (In real, ParsingObject<T> are objects I get from a service)
public static void Main()
{
Parser parser = new Parser();
parser.AddAnObject(
new ParsingObject<int>{PropertyName = "FirstProperty", Active=true, DefaultValue=1}
);
parser.AddAnObject(
new ParsingObject<bool>{PropertyName = "SecondProperty", Active=false, DefaultValue=false}
);
parser.Parse();
}
ParsingObject gets any type (I think only string, bool, int,...) as generic. Now in my parser I need to add this object into a List<ParsingObject<T>> like:
public class Parser
{
private readonly List<ParsingObject<T>> _listOfObjects = new List<ParsingObject<T>>();
public void AddAnObject<T>(ParsingObject<T> item)
{
_listOfObjects.Add(item);
}
public void Parse()
{
foreach(var item in _listOfObjects.Where(w=>Active))
{
DoSomething(item);
}
}
}
but I know, I cannot set T as generic argument when instantiating the list (compiler is crying..).
So I could solve this with using ArrayList - but then I can't access the properties of each object. (See the Parse()-method)
for completeness, here is my ParsingObject<T>-class:
public class ParsingObject<T>
{
public string PropertyName { get; set; }
public bool Active { get; set; }
public T DefaultValue { get; set; }
}
Any idea how I could solve this? I cannot modify the ParsingObject<T>-class.
Depending on what exactly is your end goal, maybe something like this would be sufficient:
public class ParsingObjectBase
{
public string PropertyName { get; set; }
public bool Active { get; set; }
public Type ValueType { get; protected set; }
public object DefVal { get; protected set; }
}
public class ParsingObject<T> : ParsingObjectBase
{
public object DefaultValue
{
get { return (T)DefVal; }
set { DefVal = value; }
}
public ParsingObject()
{
ValueType = typeof(T);
}
}
private readonly List<ParsingObjectBase> _listOfObjects = new List<ParsingObjectBase>();
public void AddAnObject<T>(ParsingObject<T> item)
{
_listOfObjects.Add(item);
}
public void Parse()
{
foreach(var item in _listOfObjects.Where(w=>w.Active))
{
DoSomething(item); //do what exactly?
}
}
You obviously can't do without casting either to concrete ParsingObject<T> or DefVal value in this case, but you have Type information stored in one place and have access to your specific properties. Maybe changing ValueType to some kind of enum would be easier to use with switch?
So I am using reflection to loop through the properties of one object and populating the values on a different object with properties of the same name. This works great but the problem comes when the property type is a collection. I want to be able to loop through each of the objects in the source collection and populate the same list with objects in the source collection.
public class SourceMessage
{
public string Name { get; set; }
public int Version { get; set; }
public IList<ValueDefinition> Values { get; set; }
}
public class ValueDefinition
{
public string Name { get; set; }
public string Value { get; set; }
}
public class TargetObject
{
public TargetObject()
{
Values = new List<TargetValueDefinition>();
}
public string Name { get; set; }
public int Version { get; set; }
public IList<TargetValueDefinition> Values { get; set; }
}
public class TargetValueDefinition
{
public string Name { get; set; }
public string Value { get; set; }
}
Then I use Reflection to populate the target from the source.
public static void PopulateFromMessage<T, TS>(ref T targetEntity, TS message)
{
var sourceType = typeof(TS);
var targetType = typeof(T);
foreach (var targetPropInfo in targetType.GetProperties())
{
if (sourceType.GetProperty(targetPropInfo.Name) != null)
{
var obj = sourceType.GetProperty(targetPropInfo.Name);
if (obj.PropertyType.Namespace == "System.Collections.Generic")
{
//var x = targetType.GetProperty(targetPropInfo.Name);
//PopulateFromMessage(ref x, sourceType.GetProperty(targetPropInfo.Name));
continue;
}
targetPropInfo.SetValue(targetEntity, sourceType.GetProperty(targetPropInfo.Name).GetValue(message), null);
}
}
}
So calling this would be like this:
private void DenormalizeMessage(SourceMessage message)
{
var newTargetObject = new TargetObject();
PopulateFromMessage(ref newTargetObject , message);
}
I can identify when the property is a collection but am uncertain of how to create new TargetValueDefinitions and populate them with the values from ValueDefinitions. In the end it is pretty much a copy of the SourceMessage in the form of a TargetObject.
This all stems from receiving messages and transforming them into objects with the same property names.
If your problem is iterating through items contained inside a single property when it is a collection, then the key would be to read the property value into a dynamic variable and not an object variable that is by default, this way you could use a foreach for it.
dynamic propVal = inputProperty.GetValue(item);
foreach (var subItem in propVal)
{
//do your stuff
}
Disclaimer: This is extremely unsafe to do and makes a lot of assumptions but it should puth you on the right path.
Change you method to this:
public static void PopulateFromMessage<T, TS>(T targetEntity, TS message)
{
var sourceType = typeof (TS);
var targetType = typeof (T);
foreach (var targetPropInfo in targetType.GetProperties())
{
if (targetPropInfo.PropertyType.IsGenericType)
{
if (targetPropInfo.PropertyType.GetGenericTypeDefinition() == typeof(IList<>))
{
var originalList = sourceType.GetProperty(targetPropInfo.Name).GetValue(message) as IList;
if (originalList != null)
{
var argumentType = targetPropInfo.PropertyType.GetGenericArguments();
var listType = typeof (List<>);
var concreteType = listType.MakeGenericType(argumentType);
var newList = Activator.CreateInstance(concreteType) as IList;
foreach (var original in originalList)
{
var targetValue = Activator.CreateInstance(argumentType[0]);
// do this yourself. Here we're converting ValueDefinition to TargetValueDefinition
// targetValue.Fill(original);
}
targetPropInfo.SetValue(targetEntity, newList);
}
}
}
else
{
if (sourceType.GetProperty(targetPropInfo.Name) != null)
{
var obj = sourceType.GetProperty(targetPropInfo.Name);
if (obj.PropertyType.Namespace == "System.Collections.Generic")
{
//var x = targetType.GetProperty(targetPropInfo.Name);
//PopulateFromMessage(ref x, sourceType.GetProperty(targetPropInfo.Name));
continue;
}
targetPropInfo.SetValue(targetEntity, sourceType.GetProperty(targetPropInfo.Name).GetValue(message), null);
}
}
}
}
You should create a interface for each class (implement the methods and properties on interface) and implement it in each class. After, in function PopulateFromMessage should specify the interface allowed in method, with this you can use directly the properties of class with T and TS generic types.
I am pretty new to C#, so please go easy on me.
I have a major issue that been holding me up for days.
Problem:
We have a web application and use MVC4, when a document is opened, all the values in the model are created in the backingstore in session , by calling the method SaveValues()
public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
public Dictionary<string, object> BackingStore = new Dictionary<string, object>();
public Dictionary<string, object> Changes = new Dictionary<string, object>();
public bool HasChanges { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void SaveValues()
{
// Expensive, to use reflection, especially if LOTS of objects are going to be used.
// You can use straight properties here if you want, this is just the lazy mans way.
this.GetType().GetProperties().ToList().ForEach(tProp => { BackingStore[tProp.Name] = tProp.GetValue(this, null); Changes[tProp.Name] = ""; });
HttpContext.Current.Session["SbackingStore"] = BackingStore;
HasChanges = false;
}
public void RevertValues()
{
// Again, you can use straight properties here if you want. Since this is using Property setters, will take care of Changes dictionary.
this.GetType().GetProperties().ToList().ForEach(tProp => tProp.SetValue(this, BackingStore[tProp.Name], null));
HasChanges = false;
}
public void OnPropertyChanged(string propName, object propValue)
{
// If you have any object types, make sure Equals is properly defined to check for correct uniqueness.
if (HttpContext.Current.Session["SbackingStore"] != null)
{
if (propValue == null) propValue = "";
BackingStore = (Dictionary<string, object>)HttpContext.Current.Session["SbackingStore"];
if (BackingStore[propName].Equals(propValue))
{ }
else
{
Changes[propName] = propValue;
HasChanges = true;
}
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
}
I have a class setup like so , which contains ;
public class VisitViewModel : NotifyPropertyChangedBase
{
public Activity ActivityVM { get; set; }
public VBSSteps VBSStepsVM { get; set; }
public ProductTime ProductTimeVM { get; set; }
public OtherPST OtherPSTVM { get; set; }
public TimeRange TimeRangeVM { get; set; }
}
Each class that falls into the above VisitViewModel class and are coded like the below example. They inherit the NotifyPropertyChangedBase ( I will not post all the classes here as too much info ) ;
public class Activity : NotifyPropertyChangedBase
{
public int Id { get; set; }
public string NotesID { get; set; }
public string SubID { get; set; }
public string Form { get; set; } // Form Name
private string _custNumber;
[MobileCRM.Resources.LocalizedString.LocalizedDisplayName("CustomerNumber")]
[DataType(DataType.Text)]
[Required]
public string CustNumber
{
get { return _custNumber; }
set { _custNumber = value; OnPropertyChanged("CustNumber", value); }
}
private string _companyName;
[MobileCRM.Resources.LocalizedString.LocalizedDisplayName("CustomerName")]
[DataType(DataType.Text)]
[Required]
public string CompanyName
{
get { return _companyName; }
set { _companyName = value; OnPropertyChanged("CompanyName", value); }
}
}
Now the issue is, the values that are created in the backing store ( session ) like the below ( when I expand any one of them i.e. ActivityVM, that contains the Keys and values I want. );
[0] {[ActivityVM, MobileCRM.Models.Activity]}
[1] {[VBSStepsVM, MobileCRM.Models.VBSSteps]}
[2] {[ProductTimeVM, MobileCRM.Models.ProdcutTime]}
[3] {[OtherPSTVM, MobileCRM.Models.OtherPST]}
[4] {[TimeRangeVM, MobileCRM.Models.TimeRange]}
[5] {[HasChanges, False]}
The problem with this is the code I use to get the values and compare the changed data, cannot find the value as they are stored as propertys.... can anybody suggest a way around this ?
Maybe when the values are saved, I could loop through each class and add all the values in each class to the backing store, therefore stopping the values being save as properties.
Code to get values from the backing store and perform the compare ( to see if the data has changed )
public void OnPropertyChanged(string propName, object propValue)
{
// If you have any object types, make sure Equals is properly defined to check for correct uniqueness.
if (HttpContext.Current.Session["SbackingStore"] != null)
{
if (propValue == null) propValue = "";
BackingStore = (Dictionary<string, object>)HttpContext.Current.Session["SbackingStore"];
if (BackingStore[propName].Equals(propValue)) // Errors here : gives The given key was not present in the dictionary.
You have this code:
public void SaveValues()
{
// Expensive, to use reflection, especially if LOTS of objects are going to be used.
// You can use straight properties here if you want, this is just the lazy mans way.
this.GetType().GetProperties().ToList().ForEach(tProp => { BackingStore[tProp.Name] = tProp.GetValue(this, null); Changes[tProp.Name] = ""; });
HttpContext.Current.Session["SbackingStore"] = BackingStore;
HasChanges = false;
}
All clases inherit the base class with this method. So whenever you call SaveValues method on any of your derived classes the HttpContext.Current.Session["SbackingStore"] get ovveriden with new backingstore, that is why you get "A key is missing in dictionary".