-1

I have a list that I would like to search and then I want to change the variables that are of the DateTime type to the local time.

  public static T LocalTime<T>(T value, string locationTimeZone)
        {

            if(value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(List<>))
            {

                IList collection = (IList)value;
                foreach (var element in collection)
                {
                    PropertyInfo[] props =  element.GetType().GetProperties();
                    foreach (var property in props)
                    {
                        if (property.PropertyType == typeof(System.DateTime?))
                        {
                            var localTime = LocalTimeConvert("Alaskan Standard Time", DateTime.Now);
                            property.SetValue(property, localTime);
                        } 
                        else if (property.PropertyType == typeof(System.DateTime))
                        {
                            property.SetValue(property, ((System.DateTime)property.GetValue(property, null)).AddDays(10), null);
                        }
                    }
                }
            }
            return value;
        }

I'm struggling with a part where I want to change that value. How I can get an value and change value from PropertyInfo ?

Trevor
  • 7,777
  • 6
  • 31
  • 50
Forey
  • 81
  • 7
  • 1
    Does this answer your question? [Setting a property by reflection with a string value](https://stackoverflow.com/questions/1089123/setting-a-property-by-reflection-with-a-string-value) or https://stackoverflow.com/questions/7718792/can-i-set-a-property-value-with-reflection or maybe https://stackoverflow.com/questions/9404523/set-property-value-using-property-name – Trevor Feb 24 '21 at 18:14
  • This seems like a very strange use of generics. Why not just write a function that accepts/returns an `IEnumerable`? – John Wu Feb 24 '21 at 18:17

2 Answers2

1

replace this code :

you should pass instance of object to getValue() or setValue() not property

public static T LocalTime<T>(T value, string locationTimeZone)
    {

        if (value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(List<>))
        {

            IList collection = (IList)value;
            foreach (var element in collection)
            {
                PropertyInfo[] props = element.GetType().GetProperties();
                foreach (var property in props)
                {
                    if (property.PropertyType == typeof(System.DateTime?))
                    {
                        var localTime = LocalTimeConvert("Alaskan Standard Time", DateTime.Now);
                        property.SetValue(element, localTime);
                    }
                    else if (property.PropertyType == typeof(System.DateTime))
                    {
                        property.SetValue(element, ((System.DateTime)property.GetValue(element, null)).AddDays(10), null);
                    }
                }
            }
        }
        return value;
    }
Sign
  • 113
  • 8
0

the problem is that you are passing the property when you should be passing the object you want to change

property.SetValue(property, ((System.DateTime)property.GetValue(property, null)).AddDays(10), null);

I think you can also improve your use of generics like others stated

something like this

public static IList<T> LocalTime<T>(IList<T> value, string locationTimeZone)
{

    foreach (var element in collection)
    {
        PropertyInfo[] props =  element.GetType().GetProperties();
        foreach (var property in props)
        {
            if (property.PropertyType == typeof(System.DateTime?))
            {
                var localTime = LocalTimeConvert("Alaskan Standard Time", DateTime.Now);
                property.SetValue(property, localTime);
            }
            else if (property.PropertyType == typeof(System.DateTime))
            {
                property.SetValue(property, ((System.DateTime)property.GetValue(property, null)).AddDays(10), null);
            }
        }
     }

     return value;
 }
CaveCoder
  • 791
  • 3
  • 17
  • I would do it like that If I would be sure that I will receive only List in method. This is the beginning of the whole method which will distinct what kind of object it is and do the proper calculation ;). – Forey Feb 24 '21 at 18:30
  • 1
    You can use IEnumerable instead, also you can add generic constrains to where T : class to avoid this being called with primitive values or structs – CaveCoder Feb 24 '21 at 18:34