diff --git a/src/HotAvalonia/AvaloniaControlInfo.cs b/src/HotAvalonia/AvaloniaControlInfo.cs
index d39fd8d..099e10b 100644
--- a/src/HotAvalonia/AvaloniaControlInfo.cs
+++ b/src/HotAvalonia/AvaloniaControlInfo.cs
@@ -121,7 +121,7 @@ public AvaloniaControlInfo(
/// The newly compiled populate method, if the compilation was successful.
public object? Load(string xaml, object? control, out MethodInfo? compiledPopulateMethod)
{
- control = AvaloniaControlHelper.Load(xaml, _uri, control, out compiledPopulateMethod);
+ control = AvaloniaControlHelper.Load(xaml, _uri, control, _controlType, out compiledPopulateMethod);
if (control is not null)
Refresh(control);
diff --git a/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs b/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs
index e94a25a..3275079 100644
--- a/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs
+++ b/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs
@@ -95,22 +95,31 @@ private static void OnNewSreMethodBuilder(MethodBuilder methodBuilder, IEnumerab
assembly.AllowAccessTo(parameterType);
}
+ ///
+ public static object Load(string xaml, Uri uri, object? control, out MethodInfo? compiledPopulateMethod)
+ => Load(xaml, uri, control, null, out compiledPopulateMethod);
+
///
/// Loads an Avalonia control from XAML markup and initializes it.
///
/// The XAML markup to load the control from.
/// The URI that identifies the XAML source.
/// The optional control object to be populated.
+ /// The type of the control.
/// The newly compiled populate method, if the compilation was successful.
/// An object representing the loaded Avalonia control.
///
/// This method replaces static resources with their dynamic counterparts before loading the control.
///
- public static object Load(string xaml, Uri uri, object? control, out MethodInfo? compiledPopulateMethod)
+ public static object Load(string xaml, Uri uri, object? control, Type? controlType, out MethodInfo? compiledPopulateMethod)
{
_ = xaml ?? throw new ArgumentNullException(nameof(xaml));
_ = uri ?? throw new ArgumentNullException(nameof(uri));
+ controlType ??= control?.GetType();
+ if (controlType is not null && AvaloniaRuntimeXamlScanner.DynamicXamlAssembly is AssemblyBuilder xamlAssembly)
+ xamlAssembly.AllowAccessTo(controlType);
+
string xamlWithDynamicComponents = MakeStaticComponentsDynamic(xaml);
HashSet oldPopulateMethods = new(AvaloniaRuntimeXamlScanner.FindDynamicPopulateMethods(uri));