The source code, or rather the concept I'd like to get reviewed is what now allows me to do the following during reflection:object validator;  // An object known to implement IValidation<T>object toValidate; // The object which can be validated by using the validator.// Assume validator is IValidation<string> and toValidate string.IValidation<object> validation    = Proxy.CreateGenericInterfaceWrapper<IValidation<object>>( validator );validation.IsValid( toValidate ); // This works! No need to know about the type.// Assuming the validator validates strings, this will throw an InvalidCastException.//validation.IsValid( 10 );I no longer need to know about concrete types during reflection where I know types will match. It looks kind of like covariance but the result only works because types are guaranteed to match. This allows me to support any type, since no manual type checks need to be done to cast to the correct interface type.The CreateGenericInterfaceWrapper method uses RunSharp to emit the code. This source code, and an extended explanation can be found on my blog.My main concerns are:Did anybody encounter such a use case before, and found a better way to solve it?Did I write something which I could just have found in an existing library?To further clarify why I wrote this solution, I copied the example from my blog of the code I would have to write to be able to use the IsValid method during reflection.object validator;  // An object known to implement IValidation<T>object toValidate; // The object which can be validated by using the validator.// 'validator' can be any type validator which can validate 'toValidate'.if ( validator is IValidation<string> ){    IValidation<string> validation = (IValidation<string>)validator;    validation.IsValid( (string)toValidate );}else if ( validator is IValidation<int> ){    IValidation<int> validation = (IValidation<int>)validator;    validation.IsValid( (int)toValidate );}else if ... // and so on to support any possible type ...
Casting to less generic types
c#;proxy;reflection