Skip to content

Commit

Permalink
introduce InterfaceMap class
Browse files Browse the repository at this point in the history
  • Loading branch information
Sholtee committed Nov 21, 2022
1 parent 8029964 commit 69863cb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
41 changes: 41 additions & 0 deletions SRC/Private/InterfaceMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/********************************************************************************
* InterfaceMap.cs *
* *
* Author: Denes Solti *
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Solti.Utils.Proxy.Internals
{
/// <summary>
/// Cached interface mappings. Intended for private use only.
/// </summary>
public static class InterfaceMap<TInterface, TImplementation>
where TInterface: class
where TImplementation: TInterface
{
private static IReadOnlyDictionary<MethodInfo, MethodInfo> GetMappings()
{
Dictionary<MethodInfo, MethodInfo> dict = new();

foreach (Type iface in typeof(TInterface).GetHierarchy())
{
InterfaceMapping mapping = typeof(TImplementation).GetInterfaceMap(iface);

for (int i = 0; i < mapping.InterfaceMethods.Length; i++)
{
dict.Add(mapping.InterfaceMethods[i], mapping.TargetMethods[i]);
}
}

return dict;
}

/// <summary>
/// Returns the cached mappings.
/// </summary>
public static IReadOnlyDictionary<MethodInfo, MethodInfo> Mappings { get; } = GetMappings();
}
}
2 changes: 2 additions & 0 deletions SRC/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ Solti.Utils.Proxy.InterfaceInterceptor<TInterface, TTarget>
Solti.Utils.Proxy.InterfaceInterceptor<TInterface, TTarget>.InterfaceInterceptor(TTarget? target) -> void
Solti.Utils.Proxy.InterfaceInterceptor<TInterface, TTarget>.Proxy.set -> void
Solti.Utils.Proxy.InterfaceInterceptor<TInterface, TTarget>.Target.get -> TTarget?
Solti.Utils.Proxy.Internals.InterfaceMap<TInterface, TImplementation>
static Solti.Utils.Proxy.Internals.InterfaceMap<TInterface, TImplementation>.Mappings.get -> System.Collections.Generic.IReadOnlyDictionary<System.Reflection.MethodInfo!, System.Reflection.MethodInfo!>!
virtual Solti.Utils.Proxy.InterfaceInterceptor<TInterface, TTarget>.Invoke(Solti.Utils.Proxy.InvocationContext! context) -> object?
2 changes: 2 additions & 0 deletions SRC/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ Solti.Utils.Proxy.InterfaceInterceptor<TInterface, TTarget>
Solti.Utils.Proxy.InterfaceInterceptor<TInterface, TTarget>.InterfaceInterceptor(TTarget? target) -> void
Solti.Utils.Proxy.InterfaceInterceptor<TInterface, TTarget>.Proxy.set -> void
Solti.Utils.Proxy.InterfaceInterceptor<TInterface, TTarget>.Target.get -> TTarget?
Solti.Utils.Proxy.Internals.InterfaceMap<TInterface, TImplementation>
static Solti.Utils.Proxy.Internals.InterfaceMap<TInterface, TImplementation>.Mappings.get -> System.Collections.Generic.IReadOnlyDictionary<System.Reflection.MethodInfo!, System.Reflection.MethodInfo!>!
virtual Solti.Utils.Proxy.InterfaceInterceptor<TInterface, TTarget>.Invoke(Solti.Utils.Proxy.InvocationContext! context) -> object?

0 comments on commit 69863cb

Please sign in to comment.