Skip to content

Instantly share code, notes, and snippets.

@mrvoorhe
Created March 20, 2020 13:41
Show Gist options
  • Save mrvoorhe/51c20368469666f665e46c6133b0d984 to your computer and use it in GitHub Desktop.
Save mrvoorhe/51c20368469666f665e46c6133b0d984 to your computer and use it in GitHub Desktop.
protected override void MapType(TypeDefinition type)
{
base.MapType(type);
MapDerivedTypes(type);
MapInterfaceImplementors(type);
}
void MapDerivedTypes(TypeDefinition type)
{
if (!type.IsClass)
return;
var bases = Annotations.GetClassHierarchy(type);
if (bases == null)
return;
foreach (var @base in bases)
{
Annotations.AddDerived(@base, type);
}
}
void MapInterfaceImplementors(TypeDefinition type)
{
MapInterfaceImplementors(type, type);
var bases = Annotations.GetClassHierarchy(type);
if (bases == null)
return;
foreach (var @base in bases)
MapInterfaceImplementors(type, @base);
}
void MapInterfaceImplementors(TypeDefinition originalType, TypeDefinition currentType)
{
foreach (var @iface in currentType.Interfaces)
{
var resolvedInterface = @iface.InterfaceType.Resolve();
if (resolvedInterface == null)
continue;
Annotations.AddImplementor(resolvedInterface, originalType, @iface);
}
}
and over in our UnityAnnotationStore
readonly Dictionary<TypeDefinition, List<InterfaceImplementorInformation>> implementorsOfInterfaces = new Dictionary<TypeDefinition, List<InterfaceImplementorInformation>>();
readonly Dictionary<TypeDefinition, List<TypeDefinition>> derivedTypes = new Dictionary<TypeDefinition, List<TypeDefinition>>();
public System.Collections.ObjectModel.ReadOnlyCollection<InterfaceImplementorInformation> GetImplementors(TypeDefinition @interface)
{
if (implementorsOfInterfaces.TryGetValue(@interface, out List<InterfaceImplementorInformation> implementors))
return implementors.AsReadOnly();
return null;
}
public System.Collections.ObjectModel.ReadOnlyCollection<TypeDefinition> GetDerived(TypeDefinition type)
{
if (derivedTypes.TryGetValue(type, out List<TypeDefinition> derived))
return derived.AsReadOnly();
return null;
}
public void AddDerived(TypeDefinition type, TypeDefinition derived)
{
if (!derivedTypes.TryGetValue(type, out List<TypeDefinition> items))
derivedTypes[type] = items = new List<TypeDefinition>();
items.Add(derived);
}
public void AddImplementor(TypeDefinition @interface, TypeDefinition implementor, InterfaceImplementation interfaceImplementation)
{
if (!implementorsOfInterfaces.TryGetValue(@interface, out List<InterfaceImplementorInformation> items))
implementorsOfInterfaces[@interface] = items = new List<InterfaceImplementorInformation>();
items.Add(new InterfaceImplementorInformation(implementor, interfaceImplementation));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment