Skip to content

Instantly share code, notes, and snippets.

@robashton
Created January 12, 2011 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robashton/776607 to your computer and use it in GitHub Desktop.
Save robashton/776607 to your computer and use it in GitHub Desktop.
public static IEngineConfigurationTypeBuilder<TPoco> Collection<TPoco, TCollection>(
this IEngineConfigurationTypeMemberBuilder<TPoco, TCollection> memberConfig, int min, int max) where TCollection : IEnumerable
{
var collectionType = typeof (TCollection);
// Nefarious, I know - shocking, this will get is Y from X<Y> where X = IEnumerable/List/Etc and Y = Type
// So List<Y> etc
var genericcollectionArgument = collectionType.GetGenericArguments()[0];
// So this will give us AutoSource<Y>
var autoSourceType = typeof (AutoSource<>).MakeGenericType(genericcollectionArgument);
//// And this will give us FlexibleEnumerableSource<AutoSource<Y>, X<Y>, Y>
var enumerableSourceType = typeof (FlexibleEnumerableSource<,,>).MakeGenericType(
autoSourceType, collectionType, genericcollectionArgument
);
// I can't believe I'm doing this, if anybody can give us a good method definition that just_works and
// negates the need for this tomfoolery I'm all ears
var method = memberConfig.GetType().GetMethod("Use",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance, null, new[] { typeof(Object[]) }, null);
var genericMethod = method.MakeGenericMethod(new[] {enumerableSourceType});
return (IEngineConfigurationTypeBuilder<TPoco>)genericMethod.Invoke(memberConfig, new object[] { new object[] { min, max}});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment