Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ENikS committed Dec 27, 2017
1 parent 82891e4 commit a01eb9b
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions src/Utility/PolicyListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public static void SetDefault<TPolicyInterface>(this IPolicyList policies, TPoli

#region Get

public static T Get<T>(this IPolicyList policies, Type type, string name, out IPolicyList list)
{
return (T) policies.Get(type, name, typeof(T), out list);
}

/// <summary>
/// Gets an individual policy.
/// </summary>
Expand Down Expand Up @@ -342,31 +347,11 @@ private static Tuple<Type, string> ParseBuildKey(object buildKey)

public static TPolicyInterface GetPolicy<TPolicyInterface>(this IPolicyList list, INamedType buildKey, out IPolicyList containingPolicyList)
{
return (TPolicyInterface)(list.GetPolicyForKey(typeof(TPolicyInterface), buildKey, out containingPolicyList) ??
list.GetPolicyForOpenType(typeof(TPolicyInterface), buildKey, buildKey.Type, out containingPolicyList) ??
list.GetDefaultForPolicy(typeof(TPolicyInterface), out containingPolicyList));
}

private static IBuilderPolicy GetPolicyForOpenType(this IPolicyList list, Type policyInterface, INamedType buildKey, Type buildType, out IPolicyList containingPolicyList)
{
containingPolicyList = null;
if (null == buildType) return null;

if (buildType.GetTypeInfo().IsGenericType)
{
var newType = buildType.GetGenericTypeDefinition();
return list.Get(newType, buildKey.Name, policyInterface, out containingPolicyList) ??
list.Get(newType, string.Empty, policyInterface, out containingPolicyList);
}

if (buildType.IsArray && buildType.GetArrayRank() == 1)
{
return list.Get(typeof(Array), buildKey.Name, policyInterface, out containingPolicyList) ??
list.Get(typeof(Array), string.Empty, policyInterface, out containingPolicyList);
}

return null;
return (TPolicyInterface)(list.GetPolicyForKey(typeof(TPolicyInterface), buildKey, out containingPolicyList)
?? (buildKey.Type.GetTypeInfo().IsGenericType
? list.Get(buildKey.Type.GetGenericTypeDefinition(), buildKey.Name, typeof(TPolicyInterface), out containingPolicyList) ??
list.Get(null, null, typeof(TPolicyInterface), out containingPolicyList)
: list.Get(null, null, typeof(TPolicyInterface), out containingPolicyList)));
}

}
}

0 comments on commit a01eb9b

Please sign in to comment.