0
得到WPF附加屬性我已經定製的附加屬性:無法通過反射
public class Freezable
{
public static bool GetIsFrozen(DependencyObject obj)
{
return (bool)obj.GetValue(IsFrozenProperty);
}
public static void SetIsFrozen(DependencyObject obj, bool value)
{
obj.SetValue(IsFrozenProperty, value);
}
public static readonly DependencyProperty IsFrozenProperty =
DependencyProperty.RegisterAttached("IsFrozen", typeof(bool), typeof(UIElement), new UIPropertyMetadata(false));
}
此屬性,代碼和值測試存儲成功。
Freezable.SetIsFrozen(control, value);
var correctValue =Freezable.GetIsFrozen(control);
但是,然後我試圖通過反射得到我的財產。我有一個異常,無法找到屬性(getMethod爲null,setMethod爲null)!
public void FindAttachedProperty(Type elementType, string propertyName)
{
MethodInfo getMethod = elementType.GetMethod("Get" + propertyName, BindingFlags.Public | BindingFlags.Static);
MethodInfo setMethod = elementType.GetMethod("Set" + propertyName, BindingFlags.Public | BindingFlags.Static);
}
Canvas.Left,Canvas.Right working well and without any problems。我做錯了什麼,爲什麼我不能得到我的附屬財產!?