【发布时间】:2016-07-13 14:47:02
【问题描述】:
我正在尝试在类中设置对象的属性,但错误提示为 Object does not match target type。
FieldInfo dControl = window.GetType().GetField("dControl", BindingFlags.NonPublic | BindingFlags.Instance);
if (dControl == null) { Debug.Log ("dControl is null"); return;}
Type typeDC = dControl.FieldType;
PropertyInfo inPreviewMode = typeDC.GetProperty("InPreviewMode", BindingFlags.Public | BindingFlags.Instance);
if (inPreviewMode == null) { Debug.Log ("dControl.InPreviewMode is null"); return;}
bool value = false;
inPreviewMode.SetValue(dControl, value, null);
这是我要访问的属性:
public class DControl : TimeArea
{
public bool InPreviewMode
{
get
{
return dState.IsInPreviewMode;
}
set
{
if (cutscene != null)
{
...
}
}
dState.IsInPreviewMode = value;
}
...
}
感谢您的帮助。
【问题讨论】:
-
为什么
dState.IsInPreviewMode = value;在set之外? -
您是否希望该字段始终是
DControl的实例?您可以获取字段值并进行转换。事实上,你需要做类似inPreviewMode.SetValue(dControl.GetValue(window), value, null)的事情。
标签: c# reflection properties