mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-25 19:02:24 +01:00
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>
This commit is contained in:
50
src/WireMock.Net.GraphQL/Utils/ReflectionUtils.cs
Normal file
50
src/WireMock.Net.GraphQL/Utils/ReflectionUtils.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user