【问题标题】:C# Reflection - Get Property of an ObjectC# 反射 - 获取对象的属性
【发布时间】:2016-07-12 20:33:38
【问题描述】:

我正在尝试在类中设置对象的属性,但我无法获取该属性。

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 返回 null。

这是我要访问的属性:

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

如果重要,该类将存储为 dll。

感谢您的帮助。

【问题讨论】:

  • 好吧,您目前正在尝试设置值(您正在调用SetValue)并且您没有显示设置器......如果你会显示minimal reproducible example

标签: c# reflection properties


【解决方案1】:

dControl.GetType() 返回System.Reflection.FieldInfo 的类型,因为这就是dControl 的类型。

你想要GetFieldType()

同样,您需要一个实例来传递给SetValue()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 2011-02-24
    • 2010-12-29
    相关资源
    最近更新 更多