Reputation: 461
I'm trying to set a property of an object in a class, but the error says 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);
This is the property I'm trying to access:
public class DControl : TimeArea
{
public bool InPreviewMode
{
get
{
return dState.IsInPreviewMode;
}
set
{
if (cutscene != null)
{
...
}
}
dState.IsInPreviewMode = value;
}
...
}
Help is appreciated.
Upvotes: 0
Views: 112
Reputation: 136074
The first parameter of SetValue
is the instance for which to set the value on. ie, it is expecting an instance of DControl
- your code passes it an instance of FieldInfo
.
So you might have to get that instance via reflection:
DControl ctrl = (DControl)dControl.GetValue(window);
And then pass that to the set value
inPreviewMode.SetValue(ctrl, value, null);
Upvotes: 2