mercredi 24 février 2021

Change property value

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 ?





Aucun commentaire:

Enregistrer un commentaire