Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 我们是否可以更改属性信息模式(我们是否可以在扩展方法中将Can Write属性重写为true)_.net_Datetime_Propertyinfo - Fatal编程技术网

.net 我们是否可以更改属性信息模式(我们是否可以在扩展方法中将Can Write属性重写为true)

.net 我们是否可以更改属性信息模式(我们是否可以在扩展方法中将Can Write属性重写为true),.net,datetime,propertyinfo,.net,Datetime,Propertyinfo,我有一个扩展方法,通过它我可以将日期时间转换为通用/本地时区。我遇到一个问题,每当我将datetime列表传递给这个扩展方法时,它都无法转换,并且我在这一行上收到异常消息 Type type = record.GetType(); foreach (var property in type.GetProperties()) { if (property.PropertyType == typeof(DateTime) && ((DateTime)property.GetValue

我有一个扩展方法,通过它我可以将日期时间转换为通用/本地时区。我遇到一个问题,每当我将datetime列表传递给这个扩展方法时,它都无法转换,并且我在这一行上收到异常消息

Type type = record.GetType();
foreach (var property in type.GetProperties())
{
if (property.PropertyType == typeof(DateTime) && ((DateTime)property.GetValue(record, null)) != DateTime.MinValue)
{
property.SetValue(record,                                                 
 ((DateTime)property.GetValue(record, null)).ToUniversalTime());
}
}

在Property.SetValue行上,我无法设置其值,因为在这种情况下Property.CanWrite为false。我只是在传递datetime列表并对其项进行迭代时才遇到此问题。

那么,为什么不在调用
SetValue
之前检查
CanWrite
自己呢?如果您对
DateTime
结构本身进行反思,您可能试图设置确实不可写的日期。我需要根据我的要求将这些日期转换为通用/本地日期。但是该类型的提供程序尚未提供
set
方法。所讨论的属性可能没有任何与之关联的存储位置,因此它没有任何地方可以存储您试图写入的任何内容。例如,同样,财产。它只有一个getter,因为它正在计算一个值。