Resource Manager fix for F# File Resources (Bugzilla 53515) (#825)
authorRob Lyndon <rob.lyndon@gmail.com>
Wed, 12 Apr 2017 10:57:13 +0000 (11:57 +0100)
committerRui Marinho <me@ruimarinho.net>
Wed, 12 Apr 2017 10:57:13 +0000 (11:57 +0100)
* Resource Manager fix for F# File Resources (Bugzilla 53515)

* Changed spaces to tabs and changed the argument name in the ResourceManager.GetId() function.

* Improved the type checking in GetId().

Xamarin.Forms.Platform.Android/ResourceManager.cs

index 147fec8..402e52b 100644 (file)
@@ -74,23 +74,23 @@ namespace Xamarin.Forms.Platform.Android
 
                public static void Init(Assembly masterAssembly)
                {
-                       DrawableClass = masterAssembly.GetTypes().FirstOrDefault(x => x.Name == "Drawable");
-                       ResourceClass = masterAssembly.GetTypes().FirstOrDefault(x => x.Name == "Id");
+                       DrawableClass = masterAssembly.GetTypes().FirstOrDefault(x => x.Name == "Drawable" || x.Name == "Resource_Drawable");
+                       ResourceClass = masterAssembly.GetTypes().FirstOrDefault(x => x.Name == "Id" || x.Name == "Resource_Id");
                }
 
                internal static int IdFromTitle(string title, Type type)
                {
                        string name = Path.GetFileNameWithoutExtension(title);
                        int id = GetId(type, name);
-                       return id; // Resources.System.GetDrawable (Resource.Drawable.dashboard);
+                       return id;
                }
 
-               static int GetId(Type type, string propertyName)
+               static int GetId(Type type, string memberName)
                {
-                       FieldInfo[] props = type.GetFields();
-                       FieldInfo prop = props.Select(p => p).FirstOrDefault(p => p.Name == propertyName);
-                       if (prop != null)
-                               return (int)prop.GetValue(type);
+                       object value = type.GetFields().FirstOrDefault(p => p.Name == memberName)?.GetValue(type)
+                               ?? type.GetProperties().FirstOrDefault(p => p.Name == memberName)?.GetValue(type);
+                       if (value is int)
+                               return (int)value;
                        return 0;
                }
        }