简体   繁体   English

无法通过反射获取WPF附加属性

[英]Cannot get WPF attached property via Reflection

I have custom attached property in shared lib: 我在共享库中有自定义附加属性:

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));
}

This property was tested in code and value was successfully stored. 此属性已通过代码测试,并且值已成功存储。

Freezable.SetIsFrozen(control, value);
var correctValue =Freezable.GetIsFrozen(control);

But then i am trying to get my property via Reflection. 但是后来我试图通过反射来获得我的财产。 And I have exception that property cannot be find (getMethod is null and setMethod is null)! 而且我有一个例外,那就是找不到属性(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. Canvas.Left,Canvas.Right工作正常,没有任何问题。 What i am doing wrong and why i cannot get my attached property!? 我在做错什么,为什么我无法获得附加财产!

Your code works fine for me. 您的代码对我来说很好。 My best guess is that when you are calling FindAttachedProperty method you are giving the System.Windows.Freezable as elementType instead of your own implementation from library. 我最好的猜测是,当您调用FindAttachedProperty方法时, FindAttachedProperty System.Windows.Freezable为elementType而不是您自己的库实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM