Relayer interaction between extensions and engine

This commit is contained in:
Ryan Nowak 2018-02-19 14:00:31 -08:00
parent 13824c418e
commit b644ecfeaa
2 changed files with 26 additions and 23 deletions

View File

@ -27,5 +27,29 @@ namespace Microsoft.AspNetCore.Razor.Language
public override string ExtensionName { get; }
public Assembly Assembly { get; }
public RazorExtensionInitializer CreateInitializer()
{
// It's not an error to have an assembly with no initializers. This is useful to specify a dependency
// that doesn't really provide any Razor configuration.
var attributes = Assembly.GetCustomAttributes<ProvideRazorExtensionInitializerAttribute>();
foreach (var attribute in attributes)
{
// Using extension names and requiring them to line up allows a single assembly to ship multiple
// extensions/initializers for different configurations.
if (!string.Equals(attribute.ExtensionName, ExtensionName, StringComparison.Ordinal))
{
continue;
}
// There's no real protection/exception handling here because this set isn't really user-extensible
// right now. This would be a great place to add some additional diagnostics and hardening in the
// future.
var initializer = (RazorExtensionInitializer)Activator.CreateInstance(attribute.InitializerType);
return initializer;
}
return null;
}
}
}

View File

@ -159,29 +159,8 @@ namespace Microsoft.AspNetCore.Razor.Language
// lid on how things work until we add official support for extensibility everywhere. So, this is
// intentionally inflexible for the time being.
var extension = extensions[i] as AssemblyExtension;
if (extension == null)
{
continue;
}
// It's not an error to have an assembly with no initializers. This is useful to specify a dependency
// that doesn't really provide any Razor configuration.
var attributes = extension.Assembly.GetCustomAttributes<ProvideRazorExtensionInitializerAttribute>();
foreach (var attribute in attributes)
{
// Using extension names and requiring them to line up allows a single assembly to ship multiple
// extensions/initializers for different configurations.
if (!string.Equals(attribute.ExtensionName, extension.ExtensionName, StringComparison.Ordinal))
{
continue;
}
// There's no real protection/exception handling here because this set isn't really user-extensible
// right now. This would be a great place to add some additional diagnostics and hardening in the
// future.
var initializer = (RazorExtensionInitializer)Activator.CreateInstance(attribute.InitializerType);
initializer.Initialize(builder);
}
var initializer = extension?.CreateInitializer();
initializer?.Initialize(builder);
}
}
}