Skip to content

Commit

Permalink
Adding extension for issue #205
Browse files Browse the repository at this point in the history
  • Loading branch information
ipjohnson committed Mar 20, 2019
1 parent 522a31b commit 4313005
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Grace.DependencyInjection.Impl;
using System;
using System.Reflection;
using Grace.DependencyInjection.Impl;

namespace Grace.DependencyInjection
{
Expand All @@ -19,5 +21,34 @@ public static IExportTypeSetConfiguration PrioritizePartiallyClosedGenerics(

return configuration;
}

/// <summary>
/// Export a type set by a keyed interface
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="configuration"></param>
/// <param name="keyFunc"></param>
/// <returns></returns>
public static IExportTypeSetConfiguration ByKeyed<T>(this IExportTypeSetConfiguration configuration, Func<Type, object> keyFunc)
{
configuration.ByKeyedTypes(type =>
{
if (typeof(T).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
{
var key = keyFunc(type);
if (key != null)
{
return new[] { new Tuple<Type, object>(type, key) };
}
return null;
}
return null;
});

return configuration;
}
}
}

0 comments on commit 4313005

Please sign in to comment.