本文介绍了如何在Silverlight中按名称获取DependencyProperty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我有一个字符串,表示Silverlight中TextBox的DependencyProperty的名称。例如: TextProperty。我需要获得对TextBox实际TextProperty的引用,这是一个DependencyProperty。

Situation: I have a string that represents the name of a DependencyProperty of a TextBox in Silverlight. For example: "TextProperty". I need to get a reference to the actual TextProperty of the TextBox, which is a DependencyProperty.

问题:如何获得对DependencyProperty的引用(在C#中)我所得到的只是属性的名称?

Question: how do I get a reference to a DependencyProperty (in C#) if all I got is the name of the property?

在Silverlight中无法使用DependencyPropertyDescriptor之类的东西。看来我不得不依靠反思来获得参考。有什么建议吗?

Things like DependencyPropertyDescriptor are not available in Silverlight. It seems I have to resort to reflection to get the reference. Any suggestions?

推荐答案

您需要对此进行反思:-

You will need reflection for this:-

 public static DependencyProperty GetDependencyProperty(Type type, string name)
 {
     FieldInfo fieldInfo = type.GetField(name, BindingFlags.Public | BindingFlags.Static);
     return (fieldInfo != null) ? (DependencyProperty)fieldInfo.GetValue(null) : null;
 }

用法:-

 var dp = GetDependencyProperty(typeof(TextBox), "TextProperty");

这篇关于如何在Silverlight中按名称获取DependencyProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 07:37