Skip to content

Commit

Permalink
Add method index to MetaType for faster lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Jan 19, 2021
1 parent a8b97a8 commit a1046cf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private bool VerifyIfBaseImplementsGetObjectData(Type baseType, MetaType model,
throw new ArgumentException(message);
}

getObjectData = model.Methods.FirstOrDefault(m => m.Method == getObjectDataMethod);
getObjectData = model.FindMethod(getObjectDataMethod);

serializationConstructor = baseType.GetConstructor(
BindingFlags.Instance | BindingFlags.Public |
Expand Down
8 changes: 8 additions & 0 deletions src/Castle.Core/DynamicProxy/Generators/MetaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
namespace Castle.DynamicProxy.Generators
{
using System.Collections.Generic;
using System.Reflection;

internal class MetaType
{
private readonly ICollection<MetaEvent> events = new TypeElementCollection<MetaEvent>();
private readonly ICollection<MetaMethod> methods = new TypeElementCollection<MetaMethod>();
private readonly Dictionary<MethodInfo, MetaMethod> methodsIndex = new Dictionary<MethodInfo, MetaMethod>();
private readonly ICollection<MetaProperty> properties = new TypeElementCollection<MetaProperty>();

public IEnumerable<MetaEvent> Events
Expand All @@ -46,11 +48,17 @@ public void AddEvent(MetaEvent @event)
public void AddMethod(MetaMethod method)
{
methods.Add(method);
methodsIndex.Add(method.Method, method); // shouldn't get added twice
}

public void AddProperty(MetaProperty property)
{
properties.Add(property);
}

public MetaMethod FindMethod(MethodInfo method)
{
return methodsIndex.TryGetValue(method, out var metaMethod) ? metaMethod : null;
}
}
}

0 comments on commit a1046cf

Please sign in to comment.