【问题标题】:Change property value [duplicate]更改属性值 [重复]
【发布时间】:2021-05-27 02:26:35
【问题描述】:

我有一个要搜索的列表,然后我想将 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 获取值和更改值?

【问题讨论】:

标签: c# reflection properties


【解决方案1】:

问题在于,当您应该传递要更改的对象时,您正在传递属性

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

【讨论】:

  • 如果我确定我只会收到方法中的列表,我会这样做。这是整个方法的开始,它将区分它是什么类型的对象并进行正确的计算;)。
  • 您可以使用 IEnumerable 代替,也可以将通用约束添加到 where T : class 以避免使用原始值或结构调用它
【解决方案2】:

替换此代码:

您应该将对象的实例传递给 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;
    }

【讨论】:

    猜你喜欢
    • 2017-04-16
    • 2013-10-17
    • 1970-01-01
    • 2021-11-23
    • 2016-11-02
    • 1970-01-01
    • 2023-01-08
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多