列出Silverlight中的附加属性

时间:2009-11-30 14:39:43

标签: c# .net silverlight silverlight-3.0

有人知道如何列出在Silveright 3中的UIElement上设置的附加属性吗?像往常一样有很多WPF解决方案!

干杯

2 个答案:

答案 0 :(得分:1)

简短的回答是:它无法完成。

Silverlight的DependencyObject没有GetLocalValueEnumerator为WPF启用此功能。事实上,提供微光的唯一方法是ReadLocalValue

因此,如果您实际上只对属性的特定子集感兴趣,则可以依次通过ReadLocalValue尝试每个属性。但是对于一般要求非常不满意。

我很好奇,首先会推动这样一项要求的是什么?

答案 1 :(得分:1)

我决定使用的一个非常可怕的暴力破解实现如下:扫描已加载的程序集以搜索依赖项属性。如果存在具有相应Getxxx / Setxxx公共静态方法的依赖项属性,则它是附加属性。显然它有很多漏洞,但现在它会帮我。

我想获得附加属性列表的原因是我可以查看对象@ runtime上设置的附加属性并更改它们,仅用于调试。

以下代码:

public static List<DependencyProperty> GetAttachedProperties(Object element)
    {
        List<DependencyProperty> attachedProperties = new List<DependencyProperty>();

        foreach (AssemblyPart ap in Deployment.Current.Parts)
        {
            StreamResourceInfo sri = Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative));
            Assembly a = new AssemblyPart().Load(sri.Stream);
            GetAttachedProperties(a, attachedProperties);
        }


        return attachedProperties;

    }

    private static void GetAttachedProperties(Assembly a, List<DependencyProperty> attachedProperties)
    {
        foreach (var type in a.GetTypes())
        {
            Debug.WriteLine(type.FullName);
            var dependencyProperties = type.GetFields(BindingFlags.Static | BindingFlags.Public).Where(
                f => typeof (DependencyProperty).IsAssignableFrom(f.FieldType)).Select(f => f);
            foreach (var dp in dependencyProperties)
            {
                FieldInfo info = dp;
                var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public);
                Debug.WriteLine("{0} suitable dp methods found", methods.Count());
                var fields = methods.Where(
                    m => (IsAttachedGetter(m, info) || IsAttachedSetter(m, info))).Select(
                    m => info).ToArray();
                foreach(var field in fields)
                {
                    try
                    {
                        Debug.WriteLine("Adding dependency property {0} from type {1}", dp.Name, type.FullName);
                        attachedProperties.Add((DependencyProperty)field.GetValue(null));
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("Error getting dependency property {0} from type {1}, exception: {2}",
                            dp.Name, type.FullName, e);
                    }
                }
            }
        }
    }

    private static bool IsAttachedSetter(MethodInfo methodInfo, FieldInfo info)
    {
        string setName = string.Format("Set{0}", info.Name.Replace("Property", string.Empty));
        if(methodInfo.Name == setName)
        {
            var methodParams = methodInfo.GetParameters();
            return methodParams.Count() == 2
                   && typeof (DependencyObject).IsAssignableFrom(methodParams[0].ParameterType);
        }
        return false;
    }

    private static bool IsAttachedGetter(MethodInfo methodInfo, FieldInfo info)
    {
        string getName = string.Format("Get{0}", info.Name.Replace("Property", string.Empty));
        if(methodInfo.Name == getName)
        {
            var methodParams = methodInfo.GetParameters();
            return methodParams.Count() == 1 &&
                   methodParams[0].ParameterType.IsAssignableFrom(typeof (DependencyObject));
        }
        return false;
    }