Skip to content

Instantly share code, notes, and snippets.

@distantcam
Created December 10, 2020 05:03
Show Gist options
  • Save distantcam/09bc3f3cf445adddb4f3e6c911d7e530 to your computer and use it in GitHub Desktop.
Save distantcam/09bc3f3cf445adddb4f3e6c911d7e530 to your computer and use it in GitHub Desktop.
A Fody weaver to strip C# 9 records markers so the assembly is compatible with C# 8 / Xamarin mono build tools.
using System.Collections.Generic;
using System.Linq;
using Fody;
using Mono.Cecil;
public class StripIsExternalInitWeaver : BaseModuleWeaver
{
public override void Execute()
{
var types = ModuleDefinition.Types.Concat(ModuleDefinition.Types.SelectMany(t => t.NestedTypes));
foreach (var type in types)
{
foreach (var prop in type.Properties)
{
if (prop is null ||
prop.SetMethod is null ||
prop.SetMethod.ReturnType is not RequiredModifierType setReturnType ||
setReturnType.ModifierType.FullName != "System.Runtime.CompilerServices.IsExternalInit" ||
setReturnType.ElementType.FullName != "System.Void")
{
continue;
}
prop.SetMethod.ReturnType = TypeSystem.VoidReference;
}
}
}
public override IEnumerable<string> GetAssembliesForScanning()
{
yield return "netstandard";
yield return "mscorlib";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment