I have a barebones UWP solution that highlights my issue at hand. When I run the app in Debug mode, it runs fine; in Release mode, I receive the following error:
Based on my search so far, it seems like it is due to the fact that my app uses reflection, and Release mode uses .NET Native which strips out files that the compiler doesn't think is used. I have been unable to find the right combination to add to my Default.rd.xml runtime directives file.
The app sample creates a custom attribute (EnumStringValue) that is applied to enum values of MyNumberEnum, and has an EnumHelper class that lets the user check to see if a given string is used as a custom attribute value.
The enum with custom attributes:
namespace MyLibrary.Core.Models
{
public enum MyNumberEnum
{
Unknown = 0,
[EnumStringValue("ONE1")]
One = 1,
[EnumStringValue("TWO2")]
Two = 2,
[EnumStringValue("THREE3")]
Three = 3
}
}
The custom attribute and enum helper:
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;
}
}
}
The EnumHelper is called in the constructor of a UWPIssueDemo class in the same library:
namespace MyLibrary
{
public class UWPIssueDemo
{
public UWPIssueDemo()
{
if (!EnumHelper.GetCustomAttribute<MyNumberEnum>("ONE1"))
{
throw new IOException("Couldn't find ONE1 (this is unexpected)");
}
}
}
}
In Debug Mode, it runs without issue. In Release Mode, the exception in the screenshot above (Reflection_InsufficientMetadata_EdbNeeded) occurs on the following line of EnumHelper:
if (field.GetCustomAttributes(typeof(EnumStringValueAttribute), false).Any())
I have tried adding the following lines to my Default.rd.xml file, but have not seen any different behavior:
<Assembly Name="MyLibrary" Dynamic="Required All"/>
<Type Name="MyLibrary.Core.EnumStringValueAttribute" Dynamic="All" Browse="All" Serialize="All"/>
What do I need to add to the Default.rd.xml file to be able to run this app in Release Mode?
I have also uploaded the zipped solution of this sample to https://www.dropbox.com/s/dm3wi3oburvdn1o/UWPIssue.zip?dl=0.
It's not the right solution, but updating the visibility of the EnumStringValueAttribute class and constructor from internal to public allowed the application to run.
public class EnumStringValueAttribute : Attribute
{
public EnumStringValueAttribute(string rawValue)
{
this.RawValue = rawValue;
}
internal string RawValue { get; set; }
}
Trying different combinations in the Default.rd.xml file were unsuccessful.