Casting to less generic types 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&lt;T&gt;
object toValidate; // The object which can be validated by using the validator.

// Assume validator is IValidation&lt;string&gt; and toValidate string.

IValidation&lt;object&gt; validation
    = Proxy.CreateGenericInterfaceWrapper&lt;IValidation&lt;object&gt;&gt;( 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&lt;T&gt;
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&lt;string&gt; )
{
    IValidation&lt;string&gt; validation = (IValidation&lt;string&gt;)validator;
    validation.IsValid( (string)toValidate );
}
else if ( validator is IValidation&lt;int&gt; )
{
    IValidation&lt;int&gt; validation = (IValidation&lt;int&gt;)validator;
    validation.IsValid( (int)toValidate );
}
else if ... // and so on to support any possible type ...
Did anybody encounter such a use case before, and found a better way to solve it?


There's the IEnumerable pattern:

public interface IValidation
{
    bool IsValid(object obj);
}

public interface IValidation&lt;T&gt; : IValidation
{
    bool IsValid(T t);
}

public abstract class BaseValidation&lt;T&gt; : IValidation&lt;T&gt;
{
    public bool IsValid(object obj)
    {
        return IsValid((T)obj);
    }

    public abstract bool IsValid(T t);
}


Whether it's better or not is debatable, but it should at least be familiar to anyone who has to maintain your code.