Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find all properties of type DateTime in an class?

Tags:

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?

like image 480
Chin Avatar asked Mar 22 '10 08:03

Chin


People also ask

What is the data type for DateTime in C#?

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.

Does C# have a date type?

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.

Why DateTime is a structure?

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.


2 Answers

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! 
like image 62
Filip Ekberg Avatar answered Oct 04 '22 17:10

Filip Ekberg


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);      } } 
like image 22
Axarydax Avatar answered Oct 04 '22 16:10

Axarydax