Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new Attribute for static methods. [OnHotReload] #833

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Core/src/Handlers/View/ViewHandlerOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public abstract partial class ViewHandler<TVirtualView, TNativeView> : ViewHandl
protected PropertyMapper _mapper;
static bool HasSetDefaults;

[HotReload.OnHotReload]
static void OnHotReload()
{
HasSetDefaults = false;
}

protected ViewHandler(PropertyMapper mapper)
{
_ = mapper ?? throw new ArgumentNullException(nameof(mapper));
Expand Down
26 changes: 26 additions & 0 deletions src/Core/src/HotReload/HotReloadExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Maui.Hosting;

namespace Microsoft.Maui.HotReload
Expand Down Expand Up @@ -31,5 +34,28 @@ public static void CheckHandlers(this IView view)
}
}

public static List<MethodInfo> GetOnHotReloadMethods(this Type type) => getOnHotReloadMethods(type).Distinct(new ReflectionMethodComparer()).ToList();

static IEnumerable<MethodInfo> getOnHotReloadMethods(Type type, bool isSubclass = false)
{
var flags = BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic;
if (isSubclass)
flags = BindingFlags.Static | BindingFlags.NonPublic;
var foos = type.GetMethods(flags).Where(x => x.GetCustomAttributes(typeof(OnHotReloadAttribute), true).Any()).ToList();
foreach (var foo in foos)
yield return foo;

if (type.BaseType != null)
foreach (var foo in getOnHotReloadMethods(type.BaseType, true))
yield return foo;
}

class ReflectionMethodComparer : IEqualityComparer<MethodInfo>
{
public bool Equals(MethodInfo? g1, MethodInfo? g2) => g1?.MethodHandle == g2?.MethodHandle;

public int GetHashCode(MethodInfo obj) => obj.MethodHandle.GetHashCode();
}

}
}
31 changes: 17 additions & 14 deletions src/Core/src/HotReload/HotReloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,22 @@ public static void RegisterReplacedView(string oldViewType, Type newViewType)
if (!IsEnabled)
return;

Console.WriteLine($"{oldViewType} - {newViewType}");
Action<MethodInfo> executeStaticMethod = (method) =>
{
try
{
method?.Invoke(null, null);
}
catch (Exception ex)
{
Debug.WriteLine($"Error calling {method.Name} on type: {newViewType}");
Debug.WriteLine(ex);
//TODO: Notifiy that we couldnt execute OnHotReload for the Method;
}
};

var onHotReloadMethods = newViewType.GetOnHotReloadMethods();
onHotReloadMethods.ForEach(x => executeStaticMethod(x));

if (typeof(IHotReloadableView).IsAssignableFrom(newViewType))
replacedViews[oldViewType] = newViewType;
Expand All @@ -125,19 +140,7 @@ public static void RegisterReplacedView(string oldViewType, Type newViewType)
RegisterHandler(h, newViewType);
}
}
try
{
//Call static init if it exists on new classes!
var staticInit = newViewType.GetMethod("Init", BindingFlags.Static | BindingFlags.Public);
staticInit?.Invoke(null, null);
}
catch (Exception ex)
{

Debug.WriteLine($"Error calling Init on type: {newViewType}");
Debug.WriteLine(ex);
//TODO: Notifiy that we couldnt hot reload.
}

}


Expand Down
9 changes: 9 additions & 0 deletions src/Core/src/HotReload/OnHotReloadAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
namespace Microsoft.Maui.HotReload
{
[AttributeUsage(AttributeTargets.Method)]
public class OnHotReloadAttribute : Attribute
{

}
}