Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
beyaz committed Jan 8, 2025
1 parent 5899b85 commit 0d35b6e
Showing 1 changed file with 67 additions and 4 deletions.
71 changes: 67 additions & 4 deletions ReactWithDotNet/ILCodeGeneration/MetadataHelper2.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,77 @@
using Mono.Cecil;
using ReactWithDotNet.Tokenizing;

namespace ReactWithDotNet;

public class MetadataHelper2
class MyClass1_2
{
internal static object[] GetMetadata(MethodDefinition methodDefinition)
public static string M1(string p0, int p1,string p3)
{
return
return "A";
}
}
class MyClass1_1
{
public static string M1(string p0, int p1,string p3)
{
return "A";
}
}
class MyClass1
{
public static string M1(string p0, int p1,string p3, MyClass1_1 p4, MyClass1_2 p5)
{
return "A";
}
}

public static class MetadataHelper2
{
static uint[] GetToken(this TypeReference typeReference)
{
if (typeReference == typeReference.Module.TypeSystem.String)
{
return [1,(uint)typeReference.MetadataType];
}

return [(uint)typeReference.MetadataToken.TokenType,typeReference.MetadataToken.RID];
}
internal static (uint Token, object[] Value) GetMetadata(MethodDefinition methodDefinition)
{
return (methodDefinition.MetadataToken.ToUInt32(),
[
methodDefinition.Name,
];
methodDefinition.ReturnType.GetToken(),
methodDefinition.Parameters.Select(p=>new object[]
{
p.Name,
p.ParameterType.GetToken()
})
]);
}

public static object Inspect()
{
var methodDefinition = GetTypeDefinition<MyClass1>().Methods.First(x => x.Name == nameof(MyClass1.M1));

return GetMetadata(methodDefinition);
}

public static TypeDefinition GetTypeDefinition<T>()
{
var assemblyDefinition = AssemblyDefinition.ReadAssembly(typeof(MetadataHelper2).Assembly.Location);

foreach (var moduleDefinition in assemblyDefinition.Modules)
{
foreach (var typeDefinition in moduleDefinition.Types)
{
if (typeDefinition.FullName == typeof(T).FullName)
{
return typeDefinition;
}
}
}

throw new Exception("Type not found");
}
}

0 comments on commit 0d35b6e

Please sign in to comment.