UWP - Release版本中的Reflection_InsufficientMetadata_EdbNeeded

时间:2017-02-08 04:23:35

标签: c# reflection uwp release-mode .net-native

我有一个准确的UWP解决方案,突出了我手头的问题。当我在调试模式下运行应用程序时,它运行正常;在发布模式下,我收到以下错误:

Reflection_InsufficientMetadata_EdbNeeded

基于我到目前为止的搜索,似乎是因为我的应用程序使用了反射,而Release模式使用了.NET Native,它删除了编译器认为不使用的文件。我一直无法找到正确的组合来添加到我的Default.rd.xml运行时指令文件。

app示例创建一个自定义属性(EnumStringValue),该属性应用于MyNumberEnum的枚举值,并且有一个EnumHelper类,允许用户检查给定的字符串是否用作自定义属性值。

具有自定义属性的枚举:

namespace MyLibrary.Core.Models
{
    public enum MyNumberEnum
    {
        Unknown = 0,
        [EnumStringValue("ONE1")]
        One = 1,
        [EnumStringValue("TWO2")]
        Two = 2,
        [EnumStringValue("THREE3")]
        Three = 3
    }
}

自定义属性和枚举助手:

namespace MyLibrary.Core
{
    internal class EnumStringValueAttribute : Attribute
    {
        internal EnumStringValueAttribute(string rawValue)
        {
            this.RawValue = rawValue;
        }
        internal string RawValue { get; set; }
    }

    internal static class EnumHelper
    {
        internal static bool GetCustomAttribute<TEnum>(string value) where TEnum : struct
        {
            var fields = typeof(TEnum).GetRuntimeFields();
            foreach (var field in fields)
            {
                if (field.GetCustomAttributes(typeof(EnumStringValueAttribute), false).Any())
                {
                    string fieldRawValue = ((EnumStringValueAttribute)field.GetCustomAttributes(typeof(EnumStringValueAttribute), false).First()).RawValue;
                    if (fieldRawValue == value)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}

在同一个库中的UWPIssueDemo类的构造函数中调用EnumHelper:

namespace MyLibrary
{
    public class UWPIssueDemo
    {
        public UWPIssueDemo()
        {
            if (!EnumHelper.GetCustomAttribute<MyNumberEnum>("ONE1"))
            {
                throw new IOException("Couldn't find ONE1 (this is unexpected)");
            }
        }
    }
}

在调试模式下,它运行没有问题。在发布模式下,上面的屏幕截图中的异常(Reflection_InsufficientMetadata_EdbNeeded)发生在以下EnumHelper行:

if (field.GetCustomAttributes(typeof(EnumStringValueAttribute), false).Any())

我尝试将以下行添加到Default.rd.xml文件中,但没有看到任何不同的行为:

<Assembly Name="MyLibrary" Dynamic="Required All"/>
<Type Name="MyLibrary.Core.EnumStringValueAttribute" Dynamic="All" Browse="All" Serialize="All"/>

我需要将哪些内容添加到Default.rd.xml文件才能在发布模式下运行此应用程序?

我还将此示例的压缩解决方案上传到https://www.dropbox.com/s/dm3wi3oburvdn1o/UWPIssue.zip?dl=0

1 个答案:

答案 0 :(得分:0)

这不是正确的解决方案,但是将EnumStringValueAttribute类和构造函数的可见性从内部更新为公共允许应用程序运行。

public class EnumStringValueAttribute : Attribute
{
    public EnumStringValueAttribute(string rawValue)
    {
        this.RawValue = rawValue;
    }
    internal string RawValue { get; set; }
}

在Default.rd.xml文件中尝试不同的组合是不成功的。