情况:我有一个字符串,表示 Silverlight 中文本框的 DependencyProperty 的名称。例如:“文本属性”。我需要获取对 TextBox 的实际 TextProperty 的引用,这是一个 DependencyProperty。
问题:如果我得到的只是属性的名称,我如何获得对 DependencyProperty 的引用(在 C# 中)?
DependencyPropertyDescriptor 之类的东西在 Silverlight 中不可用。看来我必须诉诸反射(reflection)才能获得引用。有什么建议吗?
最佳答案
你需要反射(reflection):-
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");
关于c# - 如何在 Silverlight 中按名称获取 DependencyProperty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2220474/