fieldInfo.SetValue抛出ArgumentException“对象类型无法转换为目标类型”

时间:2021-06-08 10:40:12

This is driving me nuts. Perhaps I'm missing something obvious?

这让我疯了。也许我错过了一些明显的东西?

The fieldInfo.FieldType is correct (DateTime), and the value I'm applying is also a DateTime.

fieldInfo.FieldType是正确的(DateTime),我正在应用的值也是DateTime。

for(int i=0; i<objectArray.Length; i++)
{
    FieldInfo destinationField = GetFieldInfo(i);
    object destinationValue = objectArray[i];

    destinationField.SetValue(this, destinationValue);
}

Edit: even if I set destinationValue to a literal DateTime (DateTime.Now), I still get the exception!

编辑:即使我将destinationValue设置为文字DateTime(DateTime.Now),我仍然得到异常!

3 个解决方案

#1


I am assuming that you want to set a property on your object and not a field (this might be your problem). If so then the following code might help?

我假设您要在对象上设置属性而不是字段(这可能是您的问题)。如果是这样,那么以下代码可能有所帮助

public class Order
{
    public DateTime OrderDateField;
    public DateTime OrderDate { get; set; }
}

object[] orders = new[] { new Order(), new Order(), new Order() };
for (int i = 0; i < orders.Length; i++)
{
    FieldInfo fieldInfo = orders[i].GetType().GetField("OrderDateField");
    fieldInfo.SetValue(orders[i], DateTime.Now);

    PropertyInfo propertyInfo = orders[i].GetType().GetProperty("OrderDate");
    propertyInfo.SetValue(orders[i], DateTime.Now, null);
}

Is that the result you were trying to achieve?

这是你想要达到的结果吗?

Update: The above code updates both the property and field on the Order object.

更新:上面的代码更新Order对象上的属性和字段。

#2


OK, I've figured it out.

好的,我已经弄清楚了。

If you ever see this Exception, there's a good chance the the FieldInfos you are using do not belong to the same object as your target. cough

如果您看到此异常,则您使用的FieldInfos很可能不属于与目标相同的对象。咳嗽

Sorry for being a clot, thanks to all who helped out.

很抱歉是一个凝块,感谢所有帮助过的人。

#3


Change

destinationField.SetValue(this, destinationValue);

To

destinationField.SetValue(objectArray[i], destinationValue);

There was a comment asking about the 'this' reference, but I lack the rep to reply there.

有一条评论询问“这个”参考,但我没有回复那里的回复。

#1


I am assuming that you want to set a property on your object and not a field (this might be your problem). If so then the following code might help?

我假设您要在对象上设置属性而不是字段(这可能是您的问题)。如果是这样,那么以下代码可能有所帮助

public class Order
{
    public DateTime OrderDateField;
    public DateTime OrderDate { get; set; }
}

object[] orders = new[] { new Order(), new Order(), new Order() };
for (int i = 0; i < orders.Length; i++)
{
    FieldInfo fieldInfo = orders[i].GetType().GetField("OrderDateField");
    fieldInfo.SetValue(orders[i], DateTime.Now);

    PropertyInfo propertyInfo = orders[i].GetType().GetProperty("OrderDate");
    propertyInfo.SetValue(orders[i], DateTime.Now, null);
}

Is that the result you were trying to achieve?

这是你想要达到的结果吗?

Update: The above code updates both the property and field on the Order object.

更新:上面的代码更新Order对象上的属性和字段。

#2


OK, I've figured it out.

好的,我已经弄清楚了。

If you ever see this Exception, there's a good chance the the FieldInfos you are using do not belong to the same object as your target. cough

如果您看到此异常,则您使用的FieldInfos很可能不属于与目标相同的对象。咳嗽

Sorry for being a clot, thanks to all who helped out.

很抱歉是一个凝块,感谢所有帮助过的人。

#3


Change

destinationField.SetValue(this, destinationValue);

To

destinationField.SetValue(objectArray[i], destinationValue);

There was a comment asking about the 'this' reference, but I lack the rep to reply there.

有一条评论询问“这个”参考,但我没有回复那里的回复。