如何在UWP中获取内容属性的名称? [英] How to get the name of the Content Property in UWP?

查看:44
本文介绍了如何在UWP中获取内容属性的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过

public string GetContentProperty(Type type)
{
     var contentPropertyAttribute = type.GetTypeInfo().GetCustomAttribute<ContentPropertyAttribute>();
     return contentPropertyAttribute?.Name;
}

但是它总是返回null.

But it always returns null.

在WPF中可以正常运行.

In WPF it works OK.

推荐答案

我研究这个问题的时间比我应有的时间长,但仍然不知道完整的答案.我衷心希望其他人能提供更好的选择.

I have been looking at this for longer than I really should have, and still don't know the complete answer. I sincerely hope someone else comes along and provides a better option.

到目前为止,我发现在UWP上,对 GetCustomAttributes()的调用将返回一个空的枚举.起初,我认为这可能与为UWP程序集进行的类型剥离有关,但是我可以在Debug构建中重现该问题,而无需使用使用.NET Native工具链编译" 选项.启用,因此编译后的程序集应包含完整的类型信息.并且即使我修改了 Default.rd.xml 文件以包括< Type Name ="Windows.UI.Xaml.Markup.ContentPropertyAttribute" Dynamic ="Required All"/> (从理论上讲,可能会省略 ContentPropertyAttribute 类型),这无济于事.

What I've found so far is that on UWP, the call to GetCustomAttributes() returns an empty enumeration. At first I thought this might have something to do with the type-stripping done for UWP assemblies, but I can reproduce the issue in a Debug build, without the "Compile with .NET Native tool chain" option enabled, so the compiled assembly should include complete type information. And even if I modify the Default.rd.xml file to include <Type Name="Windows.UI.Xaml.Markup.ContentPropertyAttribute" Dynamic="Required All"/> (on the theory that maybe the ContentPropertyAttribute type was being omitted), that doesn't help.

所以,我对正在发生的事情一无所知.但是,与此同时,这是您可以使用的方法的一个版本:

So, I'm at a loss as to what exactly is going on. However, in the meantime, here is a version of your method that will work:

static string GetContentProperty<TSource>()
{
    return typeof(TSource).GetTypeInfo().CustomAttributes
        .Where(a => a.AttributeType == typeof(ContentPropertyAttribute))
        .FirstOrDefault()?.NamedArguments.Cast<CustomAttributeNamedArgument?>()
        .Where(n => n.Value.MemberName == "Name")
        .FirstOrDefault()?.TypedValue.Value.ToString();
}

(不是让 Type 对象传入,而是让它通用,让该方法完成查找类型的工作.)

(Rather than pass the Type object in, I just made it generic and let the method do the work of finding the type.)

在上文中,我将 CustomAttributeNamedArgument 值类型转换为可为空的类型,因此我可以使用 FirstOrDefault(),这比实现它更方便.枚举,检查其长度,然后获取第一个非空元素.

In the above, I convert the CustomAttributeNamedArgument value type to a nullable type so I can use the FirstOrDefault(), which I find more convenient than having to materialize the enumeration, inspecting its length, and then retrieving the first element if it's non-empty.

即使 GetCustomAttributes()方法仍然有效,这一事实仍然使我认为这与某事有关,而Uem编译后的UWP程序集却将其丢弃了.类型信息.但是不幸的是,我对UWP的特定领域了解不足,无法说明.

The fact that this works even while the GetCustomAttributes() method does not still makes me think that this is somehow related to something that a compiled UWP assembly does that discards type information. But unfortunately, I don't know enough about that specific area of UWP to say how.

我将是第一个同意上述内容不是非常好的选择的人.必须获取属性的声明信息,而不是属性本身,然后在该数据中搜索属性名称,最后必须将数据中的未类型化值属性转换回 string 被返回,那都是非常混乱且不理想的.

I would be the first to agree that the above is not a terribly great option. Having to get the declaration information for the attribute rather than the attribute itself, then searching for the property name in that data, and finally having to cast the untyped value property from the data back to string so it can be returned, that's all very messy and not ideal.

但是确实有效.就是这样.:)

But it does work. So, there's that. :)

这篇关于如何在UWP中获取内容属性的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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