I need to adjust the datetime of a bunch of objects.
I'd like to loop through the properties of the class and if the type is dateTime adjust accordingly.
Is there any kind of 'describe type' built in goodness I can use?
C# DateTime is a structure of value Type like int, double etc. It is available in System namespace and present in mscorlib. dll assembly. It implements interfaces like IComparable, IFormattable, IConvertible, ISerializable, IComparable, IEquatable.
To set dates in C#, use DateTime class. The DateTime value is between 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D.
The actual data in a DateTime is a single long integer. If it were a class, every time you created a new object, 8 bytes would be allocated on the heap and another 8 bytes would be allocated on the stack for the pointer. So by making a DateTime a struct, it effectively cuts the memory requirements in half.
You can use reflection for this.
Your scenario might look somewhat like this:
static void Main(string[] args) { var list = new List<Mammal>(); list.Add(new Person { Name = "Filip", DOB = DateTime.Now }); list.Add(new Person { Name = "Peter", DOB = DateTime.Now }); list.Add(new Person { Name = "Goran", DOB = DateTime.Now }); list.Add(new Person { Name = "Markus", DOB = DateTime.Now }); list.Add(new Dog { Name = "Sparky", Breed = "Unknown" }); list.Add(new Dog { Name = "Little Kid", Breed = "Unknown" }); list.Add(new Dog { Name = "Zorro", Breed = "Unknown" }); foreach (var item in list) Console.WriteLine(item.Speek()); list = ReCalculateDOB(list); foreach (var item in list) Console.WriteLine(item.Speek()); }
Where you want to re-calculate the Birthdays of all Mammals. And the Implementations of the above are looking like this:
internal interface Mammal { string Speek(); } internal class Person : Mammal { public string Name { get; set; } public DateTime DOB { get; set; } public string Speek() { return "My DOB is: " + DOB.ToString() ; } } internal class Dog : Mammal { public string Name { get; set; } public string Breed { get; set; } public string Speek() { return "Woff!"; } }
So basicly what you need to do is to use Relfection, which is a mechanizm to check types and get the types properties and other things like that in run time. Here is an example on how you add 10 days to the above DOB's for each Mammal that got a DOB.
static List<Mammal> ReCalculateDOB(List<Mammal> list) { foreach (var item in list) { var properties = item.GetType().GetProperties(); foreach (var property in properties) { if (property.PropertyType == typeof(DateTime)) property.SetValue(item, ((DateTime)property.GetValue(item, null)).AddDays(10), null); } } return list; }
Just remember that using reflection can be slow, and it is slow generally.
However, the Above will print this:
My DOB is: 2010-03-22 09:18:12 My DOB is: 2010-03-22 09:18:12 My DOB is: 2010-03-22 09:18:12 My DOB is: 2010-03-22 09:18:12 Woff! Woff! Woff! My DOB is: 2010-04-01 09:18:12 My DOB is: 2010-04-01 09:18:12 My DOB is: 2010-04-01 09:18:12 My DOB is: 2010-04-01 09:18:12 Woff! Woff! Woff!
It's called Reflection.
var t = this; var props = t.GetType().GetProperties(); foreach (var prop in props) { if (prop.PropertyType == typeof(DateTime)) { //do stuff like prop.SetValue(t, DateTime.Now, null); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With