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

查看:21
本文介绍了通用 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.

内置于 .NET Framework 中的 WinRT 的语言投影使那些讨厌的小细节高度隐蔽.某些 WinRT 类型很容易识别,例如 Windows 命名空间中的任何类型.有些可以两者兼而有之,System.String 既可以是 .NET 类型,也可以包装 WinRT HSTRING..NET Framework 会自动自行解决这个问题.

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 类型的反射很困难.微软无法隐藏两者之间的巨大差异,不得不创建 TypeInfo 类.

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天全站免登陆