Andrew C.
Andrew C.

Reputation: 461

C# Reflection - Get Property of an Object

I'm trying to set a property of an object in a class, but I can't get the property.

FieldInfo dControl = window.GetType().GetField("dControl", BindingFlags.NonPublic | BindingFlags.Instance);
if (dControl == null) { Debug.Log ("dControl is null"); return;}

PropertyInfo inPreviewMode = dControl.GetType().GetProperty("InPreviewMode", BindingFlags.Public | BindingFlags.Instance);
if (inPreviewMode == null) { Debug.Log ("dControl.InPreviewMode is null"); return;}

inPreviewMode.SetValue(dControl, false, null);

inPreviewMode returns null, however.

This is the property I'm trying to access:

public class DControl : TimeArea
{
    public bool InPreviewMode
    {
        get
        {
            return dState.IsInPreviewMode;
        }
        ...
    }
    ...
}

The class is stored as a dll if that matters.

Help is appreciated.

Upvotes: 0

Views: 749

Answers (1)

SLaks
SLaks

Reputation: 887215

dControl.GetType() returns the type for System.Reflection.FieldInfo, since that's what dControl is.

You want GetFieldType().

Similarly, you need an instance to pass to SetValue().

Upvotes: 2

Related Questions