Can't build for Windows Phone 8.1 if using Reflection

Hello,

I’m trying to bring our game to Windows Phone platform. But currently encounter a bug in Reflection.

More specified, I use SSSystem asset from the Asset store, and the author use Reflection to do his work, like so:

src.GetType().InvokeMember(
           propName,
           BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
           System.Type.DefaultBinder,
           src,
           new object[] { data }
       );

But this snippet causes error when trying to build for Windows phone 8.1, with error like:

and

How can this be resolve? Please send help :frowning:

EDIT: I’ve already set the Compilation Override to Use Net Core, if you might ask :smile:

Reflection API has changed in .NET Core, so you must use newer API instead.

something like

#if NETFX_CORE
src.GetType().GetTypeInfo().DeclaredMethods.FirstOrDefault("method").Invoke(src, new[] {data});
#endif

To make it easier, create Windows Phone 8.1 project from Visual Studio, and try to play with reflection API there.

1 Like

Thank you for your help. Here is the code I used to build the project without error into WP8 solution, in case any one stumble against this problem :smile:

    public static void SetPropValue(object src, string propName, object data)
    {

#if NETFX_CORE
       src.GetType().GetTypeInfo()
        .DeclaredMethods
        .Where((methodInfo) => methodInfo.IsPublic && methodInfo.Name.Equals(propName))
        .FirstOrDefault()
        .Invoke(src, new[] {data});
#else
        src.GetType().InvokeMember(
           propName,
           BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
           System.Type.DefaultBinder,
           src,
           new object[] { data }
       );
#endif
    }

@Tomas1856 : The suggestion to make an WP8 project in VS is really cool. It help me a lot. Thank you again for that :smile:. But since there are so much changes in API, do you know any source I can get more information from?

Still wondering why Microsoft make it so tough for Unity to export game into their platform. It should be as smooth as Android, considering the fact that they have .NET source code from the beginning =.=

You can see the API here I think - .NET desktop apps | Free tools and frameworks.

Unity uses Mono in Android, but we cannot use Mono in Windows Store Apps due technical reasons, that’s the main cause of issues like this. This should change when il2cpp will be fully working on Windows Store Apps.

Hi, can you tell me how can I create the Windows Phone 8.1 project in Visual Studio and play the game made in Unity (since I can’t build the game in Unity for 8.1 because of the Reflection error)?

You can’t. By doing so all you will achieve is a project that compiles, but will fail at runtime for exactly the same reasons.

Totally understand you, and that is normal. But is there fix for this Reflection problems?

Is this solution for every method I am using:

#if NETFX_CORE
src.GetType().GetTypeInfo().DeclaredMethods.FirstOrDefault("method").Invoke(src, new[] {data});
#endif

Something like that. You should use TypeInfo, MethodInfo and other *Info classes on .NET Core.
Also, WinRTLegacy.TypeExtensions class (available only when using .NET Core) has some helpers to make it easier:
http://docs.unity3d.com/Manual/windowsstore-missingtypes.html

1 Like

@adam_mehman just beware of performance. Such reflection usage can be really expensive.

1 Like

Thank you guys, will try it and get back to you with the results :slight_smile: