Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Missing methods in WinRT

Discussion in 'Windows' started by theSoenke, Nov 13, 2014.

  1. theSoenke

    theSoenke

    Joined:
    Dec 4, 2012
    Posts:
    106
    I want to port a game to WinRT and i have the problem that a few methods are missing. For example System.Type does not contain the methods GetFields and GetMethods. What is the alternative for this on WinRT?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,627
    Use TypeInfo class instead.
     
  3. theSoenke

    theSoenke

    Joined:
    Dec 4, 2012
    Posts:
    106
    Thanks. Is there an way to automatically use TypeInfo instead of Type, when i export to WinRT? So that i just have to change one thing instead of replacing it everywhere?

    Edit: Shouldn't it be theoretically possible to do something like this:
    Code (CSharp):
    1. #if NETFX_CORE
    2. using Type = System.Reflection.TypeInfo;
    3. #endif
     
    Last edited: Nov 13, 2014
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,627
    They aren't identical, so it won't be as simple.
    It would probably be easier to write your own class for Type with the methods you use.
     
  5. theSoenke

    theSoenke

    Joined:
    Dec 4, 2012
    Posts:
    106
    I trying to do it now with extension methods, but unfortunately C# does not support extension properties to fix the problem with Type.BaseType
     
  6. theSoenke

    theSoenke

    Joined:
    Dec 4, 2012
    Posts:
    106
    Could you maybe give some advice how to do it? I'm not sure how to do it. Everything i tried caused a lot of new trouble
     
  7. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
  8. theSoenke

    theSoenke

    Joined:
    Dec 4, 2012
    Posts:
    106
    I first tried to add missing methods on WinRT in Type with extension methods, but then i still had the problems with properties like BaseType, because C# has not extension properties. The problem with explicit typing is that it can be only used in methods. I also don't want to add hundred times precompiler directives into the code to decide what should be executed on which platform.
    Unfortunately Type and TypeInfo are not identical so this also does not work:
    Code (CSharp):
    1. #if NETFX_CORE
    2. using Type = System.Reflection.TypeInfo;
    3. #endif
    My hope is that i can write a class which makes that possible. So i just have to add these three lines to my scripts.
     
  9. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    How many places in your code are you really using reflection and getting the object types? Maybe there's a better way...
     
  10. theSoenke

    theSoenke

    Joined:
    Dec 4, 2012
    Posts:
    106
    I get 99 errors when building for WinRT ;) Most of them are reflection errors. Currently i don't know a better solution
     
  11. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    That doesn't really explain much. :) What are you using reflection so much for? What is it that you're doing that requires so much reflection and type resolution?
     
  12. theSoenke

    theSoenke

    Joined:
    Dec 4, 2012
    Posts:
    106
    It's not really my code. I'm trying to port UFPS to WinRT
     
  13. theSoenke

    theSoenke

    Joined:
    Dec 4, 2012
    Posts:
    106
    I have one more question about porting to WinRT. It's not possible to use delegate.Method on WinRT. How can i replace that?

    Edit: It can be replaced with GetMethodInfo()
     
    Last edited: Mar 11, 2015
  14. vovkas

    vovkas

    Joined:
    Nov 30, 2015
    Posts:
    16
    Code (CSharp):
    1. namespace System.Reflection
    2. {
    3.     public static class TypeExtendedMethods
    4.     {
    5. #if NETFX_CORE
    6.         public static FieldInfo GetField(this Type type, string fieldName)
    7.         {
    8.             if (type == null || string.IsNullOrEmpty(fieldName)) return null;
    9.  
    10.             var currentType = type;
    11.             do
    12.             {
    13.                 var typeInfo = currentType.GetTypeInfo();
    14.                 var declaredField = typeInfo.GetDeclaredField(fieldName);
    15.                 if (declaredField != null) return declaredField;
    16.                 currentType = typeInfo.BaseType;
    17.             } while (currentType != null);
    18.             return null;
    19.         }
    20. #endif
    21.     }
    22. }
     
  15. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,627
    WinRTLegacy.TypeExtensions class has replacements for GetField, GetMethod and GetProperty.
     
  16. vovkas

    vovkas

    Joined:
    Nov 30, 2015
    Posts:
    16
    http://docs.unity3d.com/Manual/windowsstore-missingtypes.html

    "WinRTLegacy.TypeExtensions has methods GetConstructor(), GetMethod(), GetProperty() for System.Type"

    Unity\Editor\Data\PlaybackEngines\metrosupport\Managed\WinRTLegacy.dll (Unity3D v.4.5.5, v.4.6.9)
    public methods:
    - GetConstructor
    - GetGenericArguments
    - GetInterfaces
    - GetOpImplicitMethodsFromTypeHierarchy
    - GetPublicInstanceProperties
    - IsSubClassOf
    - ReferenceNotEquals
    - IsValueType
    - EnumToBoolean
    - GetTypeCode

    Unity3D v.5.2 have implementation for GetField, GetMethod and GetProperty.
     
  17. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Yep, this completely bit me with my asset as I started getting ambiguous reference issues, especially when trying to build / use with Windows 10 Universal.