quantum-space-buddies/MirrorWeaver/ImprovedExtensions.cs

40 lines
809 B
C#
Raw Normal View History

using Mono.Cecil;
using System.Collections.Generic;
2022-03-03 03:46:33 +00:00
namespace MirrorWeaver;
public static class ImprovedExtensions
{
2022-03-03 03:46:33 +00:00
/// <summary>
/// filters ONLY public fields instead of all non-private <br/>
/// replaces generic parameter fields with their corresponding argument
/// </summary>
public static IEnumerable<FieldDefinition> FindAllPublicFields_Improved(this TypeReference tr)
{
2022-03-03 03:46:33 +00:00
while (tr != null)
{
2022-03-03 03:46:33 +00:00
var td = tr.Resolve();
foreach (var fd in td.Fields)
{
2022-03-03 03:46:33 +00:00
if (fd.IsStatic || !fd.IsPublic)
{
2022-03-03 03:46:33 +00:00
continue;
}
2022-03-03 03:46:33 +00:00
if (fd.IsNotSerialized)
{
continue;
}
2022-03-03 03:46:33 +00:00
if (fd.FieldType is GenericParameter gp && gp.Owner == td)
{
fd.FieldType = ((GenericInstanceType)tr).GenericArguments[gp.Position];
}
2022-03-03 03:46:33 +00:00
yield return fd;
}
2022-03-03 03:46:33 +00:00
tr = td.BaseType;
}
}
2022-03-03 03:46:33 +00:00
}