简体   繁体   English

如何在WinRT中从TypeInfo获取Type?

[英]How to get Type from TypeInfo in WinRT?

I want to register all my view models for serialization, by convention. 我希望按照惯例注册所有用于序列化的视图模型。

However the following code will not compile because the var viewmodel in the foreach loop is of type TypeInfo : 但是,以下代码将无法编译,因为foreach循环中的var viewmodel的类型为TypeInfo

protected override void OnRegisterKnownTypesForSerialization()
{
    var viewModels = this.GetType().GetTypeInfo().Assembly.DefinedTypes
            .Where(t => _viewModelNameRegex.IsMatch(t.FullName))
            .ToList();

    foreach (var viewmodel in viewModels)
    {
        SessionStateService.RegisterKnownType(viewmodel);
    }
}

Apparently TypeInfo does not inherit from Type : 显然, TypeInfo不会从Type继承:

public abstract class TypeInfo : MemberInfo, IReflectableType

Unlike the full featured version , which does inherit from Type . 完整功能版本不同,后者继承自Type

So how can I get to Type from a WinRT TypeInfo ? 那么如何从WinRT TypeInfo获取Type

TypeInfo inherits from Type in the standard .NET library, but in the portable library it is declared as: TypeInfo继承自标准.NET库中的Type ,但在可移植库中,它声明为:

public abstract class TypeInfo : MemberInfo, IReflectableType

The function AsType() returns the closest thing to the traditional Type 函数AsType()返回与传统Type最接近的东西

public virtual Type AsType()

Which returns Type weakly related to the TypeInfo above 返回与上面的TypeInfo弱相关的Type

public abstract class Type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM