更改 属性 值

Change property value

我有一个要搜索的列表,然后我想将 DateTime 类型的变量更改为当地时间。

  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;
        }

我正在努力解决我想要更改该值的部分。我如何从 PropertyInfo 获取值和更改值?

问题是当您应该传递要更改的对象时却传递了 属性

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

我认为您还可以像其他人所说的那样改进泛型的使用

像这样

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;
 }

替换此代码:

您应该将对象实例传递给 getValue() 或 setValue() 而不是 属性

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;
    }