在通用的Windows平台(UWP)反射缺少的属性 [英] Reflection in universal windows platform (UWP) missing properties

查看:818
本文介绍了在通用的Windows平台(UWP)反射缺少的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Type t = obj.GetType();
t.IsEnum;
t.IsPrimitive;
t.IsGenericType
t.IsPublic;
t.IsNestedPublic
t.BaseType
t.IsValueType



上述所有特性中缺少UWP。我现在该如何检查这些类型的?

All of the above properties are missing in UWP. How do I check for these types now?

推荐答案

这是针对一个UWP C#应用程序使用了两套不同的类型。你已经知道了.NET类型,如System.String,但UWP特定类型实际上是引擎盖下的COM接口。 COM互操作是的超级胶水,基本的原因,你也可以写在Javascript和C ++应用程序UWP。和C#,WinRT的是其核心的非托管API。

A C# app that targets UWP uses two distinct sets of types. You already know the .NET types, like System.String, but the UWP specific types are actually COM interfaces under the hood. COM is the super-glue of interop, the basic reason why you can also write UWP apps in Javascript and C++. And C#, WinRT is an unmanaged api at its core.

语言投影的针对WinRT的内置在.NET框架使得那讨厌的小细节高度隐形。一些WinRT的类型很容易识别,任何在Windows命名空间的例子。一些既可以是,以System.String既可以是一个.NET类型和包装一个WinRT的HSTRING。 .NET框架本身自动的数字了这一点。

The language projection for WinRT built into the .NET Framework makes that nasty little detail highly invisible. Some WinRT types are easy to identify, anything in the Windows namespace for example. Some can be both, a System.String can be both a .NET type and wrap a WinRT HSTRING. The .NET Framework automagically figures this out by itself.

很无形的,但也有在抹墙粉一些裂缝。 Type类就是其中之一,用于反射COM类型是困难的。微软无法掩饰两人之间的大的差别,不得不打造的所属类别类

Very invisible, but there are some cracks in the spackle. The Type class is one of them, Reflection for COM types is difficult. Microsoft could not hide the big difference between the two and had to create the TypeInfo class.

您会发现所有缺少的属性早在该类。一些愚蠢的示例代码,显示其在工作中的应用UWP:

You'll find all the missing properties back in that class. Some silly sample code that shows it at work in a UWP app:

using System.Reflection;
using System.Diagnostics;
...

    public App()
    {
        Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
            Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
            Microsoft.ApplicationInsights.WindowsCollectors.Session);
        this.InitializeComponent();
        this.Suspending += OnSuspending;
        // Reflection code...
        var t = typeof(string).GetTypeInfo();
        Debug.WriteLine(t.IsEnum);
        Debug.WriteLine(t.IsPrimitive);
        Debug.WriteLine(t.IsGenericType);
        Debug.WriteLine(t.IsPublic);
        Debug.WriteLine(t.IsNestedPublic);
        Debug.WriteLine(t.BaseType.AssemblyQualifiedName);
        Debug.WriteLine(t.IsValueType);
    }



此代码VS输出窗口的内容:

Content of the VS Output window for this code:

False
False
False
True
False
System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
False

这篇关于在通用的Windows平台(UWP)反射缺少的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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