C# metro应用程序中使用反射的静态场
我正在尝试将一个静态字段信息输入metro应用程序,但我没有找到一种方法 我试过: -type.GetRuntimeField -循环中的typeInfo.GetDeclaredField以深入研究每个父类型C# metro应用程序中使用反射的静态场,c#,windows-runtime,microsoft-metro,system.reflection,C#,Windows Runtime,Microsoft Metro,System.reflection,我正在尝试将一个静态字段信息输入metro应用程序,但我没有找到一种方法 我试过: -type.GetRuntimeField -循环中的typeInfo.GetDeclaredField以深入研究每个父类型 /// <summary> /// Gets the field info from the specified name /// </summary> /// <param name="type
/// <summary>
/// Gets the field info from the specified name
/// </summary>
/// <param name="type">The source type</param>
/// <param name="fieldName">The name of the field</param>
/// <returns>The field info if found, null otherwise</returns>
public static FieldInfo GetField(this Type type, string fieldName)
{
var currentType = type;
FieldInfo result = null;
while (result == null && currentType != null)
{
var typeInfo = currentType.GetTypeInfo();
result = typeInfo.GetDeclaredField(fieldName);
currentType = typeInfo.BaseType;
}
return result;
}
。。。我是否遗漏了一些东西,或者在metro应用程序中使用反射获取类型的静态字段
编辑:
好吧,我很抱歉那些在这个问题上浪费时间的人,框架中定义的依赖属性实际上不是只读的静态字段,它们是静态属性。。。当我通常声明我的DPS为字段时,我没有考虑表单示例框架元素。宽度可以是一个属性…
下面是我用来获取字段和属性信息的代码:
public static class TypeExtensions
{
/// <summary>
/// Gets the field info from the specified name
/// </summary>
/// <param name="type">The source type</param>
/// <param name="fieldName">The name of the field</param>
/// <returns>The field info if found, null otherwise</returns>
public static FieldInfo GetField(this Type type, string fieldName)
{
var currentType = type;
FieldInfo result = null;
while (result == null && currentType != null)
{
var typeInfo = currentType.GetTypeInfo();
result = typeInfo.GetDeclaredField(fieldName);
currentType = typeInfo.BaseType;
}
return result;
}
/// <summary>
/// Gets the property info from the specified name
/// </summary>
/// <param name="type">The source type</param>
/// <param name="propertyName">The name of the property</param>
/// <returns>The field info if found, null otherwise</returns>
public static PropertyInfo GetProperty(this Type type, string propertyName)
{
var currentType = type;
PropertyInfo result = null;
while (result == null && currentType != null)
{
var typeInfo = currentType.GetTypeInfo();
result = typeInfo.GetDeclaredProperty(propertyName);
currentType = typeInfo.BaseType;
}
return result;
}
}
public static class DependencyObjectExtensions
{
public static DependencyProperty GetDependencyProperty(this DependencyObject dependencyObject, string propertyName)
{
var dependencyPropertyName = propertyName + "Property";
var type = dependencyObject.GetType();
var fieldInfo = type.GetField(dependencyPropertyName);
if (fieldInfo == null)
{
var propertyInfo = type.GetProperty(dependencyPropertyName);
if (propertyInfo != null)
{
return propertyInfo.GetValue(dependencyObject) as DependencyProperty;
}
}
else
{
var value = fieldInfo.GetValue(dependencyObject);
return value as DependencyProperty;
}
return null;
}
}
非常感谢
问候,,
Charles我没有WinRT中反射的经验,但是如果它像Silverlight一样,如果它的可访问性通常不允许访问,您可能无法访问它。如果是私人的,试着把它公之于众,再给它一次机会。谢谢你的快速回答。它实际上是一个公共静态字段一个DependencyProperty我在运行GetTypeInfo.GetDeclaredField的测试应用程序中没有问题获取静态字段。你能发布一个完整而简单的程序来演示这个问题吗?请包含您正在使用的静态字段的类以及如何调用GetField方法。您可以发布您试图返回的字段的大纲吗?非常感谢Chris,很抱歉浪费时间,我尝试使用自定义静态字段,它很有效,然后我检查了FrameworkElement中的WidthProperty定义,我意识到它实际上是一个属性。。。