I am starting to use generics and been unable to really find the simple beginners guide to them yet so I am just doing trial and error.
I want to convert this
public void CreateTask(Task task, Student student) { Task convertToUtcTime = task.ConvertToUtcTime(student); session.Save(convertToUtcTime); } public static Task ConvertToUtcTime(this Task task, Student student) { if (student != null) { TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(student.TimeZoneId); task.DueDate = TimeZoneInfo.ConvertTimeToUtc(task.DueDate, info); } return task; } to be generic
I started trying this(I have no complied it yet so this might not even work)
public void Create<T>(T entity, Student student) { T convertToUtcTime = entity.ConvertToUtcTime(student); session.Save(convertToUtcTime); } public static T ConvertToUtcTime(this T entity, Student student) { if (student != null) { TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(student.TimeZoneId); entity.DueDate = TimeZoneInfo.ConvertTimeToUtc(entity.DueDate, info); } return entity; } Now what confuses the heck out of me is how do I cast "entity" to a Task object but now only that but other objects that I need to convert like an Appointment that needs a timezone.
I just can't figure out how I could make it convert different timeszones for all my different objects.
Edit 2
// also used with nhibernate hence why everything is virtual public class Task :IEntity { public virtual int TaskId { get; private set; } public virtual DateTime DueDate { get; set; } public virtual Task ConvertToUtcTime2(Student student) { DateTime s = DueDate ; // not use to returning "this" but seems only way how to get the object back. // I also realized I can do this as well this.ConvertToUtcTime(Student); // So I am still using my extension method and no need to duplicate that code. return this; } } public interface IEntity { IEntity ConvertToUtcTime2(Student student); // more methods/properties } public void Create<T>(T entity, Student student) where T: IEntity { entity.ConvertToUtcTime2(student); } // call generic method. nhibernateRepo.Create(task, student);