-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding extension points to property directive resolver.
- Loading branch information
Milan Mikuš
committed
Oct 12, 2023
1 parent
bcd839a
commit a8aea52
Showing
5 changed files
with
81 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Framework/Framework/Compilation/Directives/ResolvedBaseTypeDirectiveCompiler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using DotVVM.Framework.Compilation.Parser.Dothtml.Parser; | ||
using DotVVM.Framework.Compilation.ControlTree; | ||
using DotVVM.Framework.Compilation.ControlTree.Resolved; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using System.Collections.Immutable; | ||
|
||
namespace DotVVM.Framework.Compilation.Directives | ||
{ | ||
public class ResolvedBaseTypeDirectiveCompiler : BaseTypeDirectiveCompiler | ||
{ | ||
private static readonly Lazy<ModuleBuilder> DynamicMarkupControlAssembly = new(CreateDynamicMarkupControlAssembly); | ||
|
||
public ResolvedBaseTypeDirectiveCompiler(IReadOnlyDictionary<string, IReadOnlyList<DothtmlDirectiveNode>> directiveNodesByName, IAbstractTreeBuilder treeBuilder, string fileName, ImmutableList<NamespaceImport> imports) | ||
: base(directiveNodesByName, treeBuilder, fileName, imports) | ||
{ | ||
} | ||
|
||
protected override ITypeDescriptor? GetOrCreateDynamicType(ITypeDescriptor baseType, string typeName) | ||
{ | ||
if (DynamicMarkupControlAssembly.Value.GetType(typeName) is { } type) | ||
{ | ||
return new ResolvedTypeDescriptor(type); | ||
} | ||
|
||
var declaringTypeBuilder = | ||
DynamicMarkupControlAssembly.Value.DefineType(typeName, TypeAttributes.Public, ResolvedTypeDescriptor.ToSystemType(baseType)); | ||
var createdTypeInfo = declaringTypeBuilder.CreateTypeInfo()?.AsType(); | ||
|
||
return createdTypeInfo is not null | ||
? new ResolvedTypeDescriptor(createdTypeInfo) | ||
: null; | ||
} | ||
|
||
private static ModuleBuilder CreateDynamicMarkupControlAssembly() | ||
{ | ||
var newDynamicAssemblyName = $"DotvvmMarkupControlDynamicAssembly-{Guid.NewGuid()}"; | ||
var assemblyName = new AssemblyName(newDynamicAssemblyName); | ||
var assemblyBuilder = | ||
AssemblyBuilder.DefineDynamicAssembly( | ||
assemblyName, | ||
AssemblyBuilderAccess.Run); | ||
|
||
// For a single-module assembly, the module name is usually | ||
// the assembly name plus an extension. | ||
var mb = | ||
assemblyBuilder.DefineDynamicModule(newDynamicAssemblyName); | ||
return mb; | ||
} | ||
} | ||
|
||
} |