Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetCustomAttributes for an enum value return an empty Array

I'm currently working on upgrading a windows 8.1 universal app to a windows 10 UWP app. There is a part of code that was working perfectly before that doesn't work anymore in my Windows 10 UWP app.

I have an enum 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 the attribute for any of the enum values, it always returns an empty array. I use 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, so attributes.Length is always 0, so the function returns null;

Has something changed in Windows 10 that prevent this from working?

Thanks a lot!

like image 658
mgarant Avatar asked Nov 09 '22 12:11

mgarant


1 Answers

I finally found the solution to my problem.

My solution contains C++ projects, so I activated the .Net Native, like it was said in every tutorials I saw about migrating a Windows 8.1 app to a Windows 10 UWP app, which is ok, but doing so sets the property UseDotNetNativeToolChain to true, which causes the problem. To fix the problem, just set it to false everywhere in your faulty project file and it will start working again! And .Net Native still works!

Hope it helps someone!

like image 135
mgarant Avatar answered Nov 14 '22 22:11

mgarant