TypeBuilder.GetMethod allows you to get a method on a generic type closed by a TypeBuilder so it lets me to the following:
TypeBuilder tb = ....
// this would throw a NotSupportedException :
// var ec = typeof(EqualityComparer<>).
// MakeGenericType(tb).GetMethod("get_Default");
// this works:
var ec = TypeBuilder.GetMethod(tb, typeof(EqualityComparer<>).
GetMethod("get_Default");
What doesn’t work (and I can’t figure out how to make it work yet) is this:
Type collectionOf = typeof(ICollection<>).MakeGenericType(tb);
// throws: 'Type must be a type provided by the runtime.
// Parameter name: types'
var colEc = TypeBuilder.GetMethod(collectionOf, typeof(EqualityComparer<>).
GetMethod("get_Default");
// throws NotSupportedException
colEc = typeof(EqualityComparer<>).MakeGenericType(collectionOf).
GetMethod("get_Default");
Anyone know the answer (I hope it’s 42)…?
It’s not completely clear to me what you’re trying to do (it looks like you’re missing some parentheses, among other issues), but if you’re trying to get a
MethodInfoforEqualityComparer<ICollection<YourType>>.get_Default, this works for me: