Internals are Evil
A while ago I talked about Internal Stripping, not surprisingly, I run into another "WTF is this internal?" class just now. And I think it would be a good sample to see what can cause me to pull the pliers and start messing with things.
The scenario was simple, I was talking with Rob Conery about a webcast he is doing, and we started talking about TDD and repositories in a Linq enabled world.
What we ended up with was the requirement to turn an List<T> to IQueryable<T>, and there isn't easy way to do that. The framework does have an implementation of this, but, naturally, it is marked internal. Not content to have to deal with writing a non trivial Linq to List<T> provider, I wrote the following:
private static IQueryable<T> CreateQueryable<T>(IEnumerable<T> inner)
{
Type queryable = typeof (IQueryable).Assembly.GetType("System.Linq.EnumerableQuery`1").MakeGenericType(typeof (T));
ConstructorInfo constructor = queryable.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
null, new Type[] {typeof (IEnumerable<T>)},new ParameterModifier[0]);
object instance = FormatterServices.GetSafeUninitializedObject(queryable);
constructor.Invoke(instance, new object[] { inner });
return (IQueryable<T>)instance;
}
Not it works.