.net-native enum.GetValues trouble
Asked Answered
L

2

10

I'm trying to make my app (for Windows 10) working under .NET native.

I got stuck with the following issue: Enum.GetValues fails at runtime with metadata is missing. I managed to simplify test case for this problem (in real life my code looks different). In portable library I have:

public enum enumValues
{        
    A1,     
    B1,        
    C1,
}

public class fff
{
    public static object GetClass2Value()
    {
        return enumValues.B1;
    }
}

In my Universal Windows app I call the following code:

Array aaa = Enum.GetValues(fff.GetClass2Value().GetType());

I receive the following exception:

Additional information: 'enumlibportable.enumValues[]' is missing metadata.

The problem is that I have no idea what to add to Default.rd.xml. I've tried to add different rd strings (enum subtype, enumValues class, enumValues[] etc.) using microsoft tool http://go.microsoft.com/fwlink/?LinkID=392859, but had no luck.

UPDATE: I know that the following code will work for my testcase Enum.GetValues(typeof(enumValue)), but I can't use it in my real project since I don't know the exact enum type in my real project.

Ladder answered 8/9, 2015 at 7:59 Comment(3)
I assume the real code is a subclassing/interface case? Can you please elaborate on that a bit?Milepost
Well, it's too long to explain my real project, that's why I spend lot of time finding the cause of the problem and preparing sample. I described well enough my problem. The problem is that optimization deletes enum metadata since it's not referenced in application explicitly. I need to tell compiler not to delete this metadata.Ladder
Can't you make the return type generic?Fabrianne
L
5

It doesn't make sense to me, but the following RD string worked for my testcase:

<Type Name="enumlibportable.enumValues[]" Browse="Required All"/>
Ladder answered 8/9, 2015 at 10:16 Comment(2)
One thing that is useful is to use the overly generous directive: <Assembly Name="Application" Dynamic="Required All" />. This will make all of the types in your application have enough information so that reflection of this type will work. It works so well, it's included in the blank UWP template under Properties\Default.rd.xml! HTHCroze
I found this line in my project, it was there <Assembly Name="Application" Dynamic="Required All" /> and it does not work in my case. I suspect this is a bug in .NET native.Ladder
B
0

May be you can try this might help you

Array aaa = (enumValues) Enum.Parse(typeof(enumValues), fff.GetClass2Value());
Barnaba answered 8/9, 2015 at 8:14 Comment(1)
In my real project I don't know the exact type and it could be different enum types, I can't use this approach, unfortunately. And this is not a proper solution, I think. If I add some new enum I will have to add this workaround for each enum. This should be done somehow through runtime definitions.Ladder

© 2022 - 2024 — McMap. All rights reserved.