Files
WireMock.Net/src/WireMock.Net.GraphQL/Utils/ReflectionUtils.cs
Stef Heyenrath 0597a73e0e Create GraphQL project (#1334)
* 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>
2025-08-10 19:00:22 +02:00

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);
});
}
}