如何获得的WinRT类的属性 [英] How to get properties of a class in WinRT

查看:90
本文介绍了如何获得的WinRT类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#和XAML一个Windows 8应用程序。我有与在构造函数中设置相同的方式相同类型的许多特性的类。相反,写作和分配为每手的属性我想获得某种类型上我的课的所有属性的列表,并将它们全部在foreach。

I am writing a Windows 8 application in C# and XAML. I have a class with many properties of the same type that are set in the constructor the same way. Instead of writing and assignment for each of the properties by hand I want to get a list of all the properties of certain type on my class and set them all in a foreach.

在正常。NET我会写这个

In "normal" .NET I would write this

var properties = this.GetType().GetProperties();
foreach (var property in properties)
{
    if (property.PropertyType == typeof(Tuple<string,string>))
    property.SetValue(this, j.GetTuple(property.Name));
}



其中,Ĵ是我的构造函数的参数。在WinRT的()不存在的GetProperties。智能感知为 this.GetType()不显示任何有用的东西,我可以使用。

where j is a parameter of my constructor. In WinRT the GetProperties() does not exist. Intellisense for this.GetType(). does not show anything useful I could use.

推荐答案

您需要使用的 GetRuntimeProperties 而不是的GetProperties

var properties = this.GetType().GetRuntimeProperties();
// or, if you want only the properties declared in this class:
// var properties = this.GetType().GetTypeInfo().DeclaredProperties;
foreach (var property in properties)
{
    if (property.PropertyType == typeof(Tuple<string,string>))
    property.SetValue(this, j.GetTuple(property.Name));
}

这篇关于如何获得的WinRT类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆