简体   繁体   中英

How can I constrain a type on a generic method based on a generic type on the class

I am trying to implement a generic method but need to cast an object of one generic type (T1 - defined on the class) to another generic type (T2 defined on the method). Other than defining T2 on the class (which I'd rather not because it won't always be needed), is there a way of achieving this?

I'm after something like this:

public class SomeClass<T1>
{
    public void SomeMethod<T2>(T1 someParameter) where T1 : T2
    {
        T2 someVariable = (T2) someParameter;
    }
}

It seems the constraint will only work the wrong way around, that is where T2:T1 works (but is obviously wrong for my purpose), but where T1:T2 doesn't.

update The reason I need to cast T1 to T2, is I use the result in a database insert method which uses reflection on the Interface to determine what columns to insert into. The interface is used to prevent trying to insert into computed columns for instance. So T2 would be this interface whereas T1 would be an original object (which would have more fields). Hence casting T2:T1 would not be correct.

You can do this fine* with is (or as ) keyword - the only constraint nexcessary on T2 is that it is a class

public class SomeClass<T1>
{
    public void SomeMethod<T2>(T1 someParameter) where T2 : class
    {
        if(!(someParameter is T2 t2))
        {           
            throw new Exception("Invalid type");
        }
        Console.WriteLine($"Hello from {t2}");
    }
}

Live example: https://dotnetfiddle.net/C5Brhn

(* fine, although it is a runtime check, not the compile-time check your original question hinted at)

With .NET5 you can use a better expression is not

public class SomeClass<T1>
{
    public void SomeMethod<T2>(T1 someParameter) where T2 : class
    {
        if(someParameter is not T2 t2)
        {           
            throw new Exception("Invalid type");
        }
        Console.WriteLine($"Hello from {t2}");
    }
}

Note that based on this comment

T2 is a subset of the fields on T1 so it is T1 that derives from T2

I have used T1=Lion and T2=Animal

So, you want a type that is a base of T1 . You can't do that directly, but you could do the following, though you would have to prove the relationship when using the method by specifying the type arguments. There may also be ways to defeat the constraint.

Basically, introduce a type parameter that "is a" T1 , and then you can introduce a type parameter that is a base of that type. This ends up looking like this:

public class SomeClass<TOriginal> {
   public void SomeMethod<TSubstitute, TBase>(
      TOriginal someParameter
   ) where TSubstitute : TOriginal, TBase {
        TBase someVariable = (TBase)(TSubstitute)someParameter;

        ...
    }
}

When using it, TOriginal and TSubstitute will usually be the same type. This doesn't explicitly say that TOriginal is descended from TBase , but it does say there exists some other type TSubstitute that is descended from both (and 'descended from' includes being the same type).

It sounds like you want to ensure that T2 is in some way castable or convertible to T1 , you may want to consider the use of a common interface between the two objects as a constraint for SomeMethod .

Heres a short example:

public interface ISomeInterface
{

}

public class SomeBaseClass : ISomeInterface
{

}

public class SomeClass<T1> where T1 : SomeBaseClass
{
    public void SomeMethod<T2>(T1 someParameter) where T2 : ISomeInterface
    {
        if (someParameter is T2 commonInterfacedObject)
        {
            T2 someVariable = commonInterfacedObject;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM