mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-15 14:53:37 +01:00
* Create new project for GraphQL * ... * . * ok? * Update src/WireMock.Net.Shared/Extensions/AnyOfExtensions.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * -- * ... --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
50 lines
1.8 KiB
C#
50 lines
1.8 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Reflection.Emit;
|
|
|
|
namespace WireMock.Utils;
|
|
|
|
internal static class ReflectionUtils
|
|
{
|
|
private const string DynamicModuleName = "WireMockDynamicModule";
|
|
private static readonly AssemblyName AssemblyName = new("WireMockDynamicAssembly");
|
|
private const TypeAttributes ClassAttributes =
|
|
TypeAttributes.Public |
|
|
TypeAttributes.Class |
|
|
TypeAttributes.AutoClass |
|
|
TypeAttributes.AnsiClass |
|
|
TypeAttributes.BeforeFieldInit |
|
|
TypeAttributes.AutoLayout;
|
|
private static readonly ConcurrentDictionary<string, Type> TypeCache = new();
|
|
|
|
public static Type CreateType(string typeName, Type? parentType = null)
|
|
{
|
|
return TypeCache.GetOrAdd(typeName, key =>
|
|
{
|
|
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess.Run);
|
|
var moduleBuilder = assemblyBuilder.DefineDynamicModule(DynamicModuleName);
|
|
|
|
var typeBuilder = moduleBuilder.DefineType(key, ClassAttributes, parentType);
|
|
|
|
// Create the type and cache it
|
|
return typeBuilder.CreateTypeInfo()!.AsType();
|
|
});
|
|
}
|
|
|
|
public static Type CreateGenericType(string typeName, Type genericTypeDefinition, params Type[] typeArguments)
|
|
{
|
|
var genericKey = $"{typeName}_{genericTypeDefinition.Name}_{string.Join(", ", typeArguments.Select(t => t.Name))}";
|
|
|
|
return TypeCache.GetOrAdd(genericKey, _ =>
|
|
{
|
|
var genericType = genericTypeDefinition.MakeGenericType(typeArguments);
|
|
|
|
// Create the type based on the genericType and cache it
|
|
return CreateType(typeName, genericType);
|
|
});
|
|
}
|
|
} |