GetCustomAttributes for enum returns an empty array

I am currently working on upgrading a generic Windows 8.1 application to a Windows 10 UWP application. There is a piece of code that worked perfectly before it did not work in my Windows 10 UWP application.

I have an enumeration that looks like this:

public enum EStaticFile { [StringValue("Path of a file")] CONFIG_FILE_1, [StringValue("Path of a file")] CONFIG_FILE_2 } 

When I try to get an attribute for any of the enum values, it always returns an empty array. I am using the following code:

 public static StringValue GetStringValueAttribute(this Enum aEnumValue) { Type type = aEnumValue.GetType(); FieldInfo fieldInfo = type.GetRuntimeField(aEnumValue.ToString()); StringValue[] attributes = fieldInfo.GetCustomAttributes(typeof(StringValue), false) as StringValue[]; if (attributes.Length > 0) { return attributes[0]; } return null; } 

GetCustomAttributes always returns an empty array, therefore attributes. The length is always 0, so the function returns null;

Has something changed in Windows 10 so it doesn't work?

Thanks a lot!

+5
source share
1 answer

I finally found a solution to my problem.

My solution contains C ++ projects, so I activated .Net Native, as it was said in every tutorial that I saw about porting a Windows 8.1 application to a Windows 10 UWP application, which is good, but the UseDotNetNativeToolChain property is set to true that causes the problem. To fix the problem, simply set it to false in the entire damaged project file and it will start working again! And .Net Native is still working!

Hope this helps someone!

+1
source

All Articles