Given a constrained generic method can I call a non-generic method passing the actual type of the generic parameter

后端 未结 2 704
借酒劲吻你
借酒劲吻你 2021-01-21 21:25

Awkward title I know, this is best explained in code. Given a set of classes:

public abstract class MyBaseType
{
    public string Message { get; set; }
}

publi         


        
2条回答
  •  无人共我
    2021-01-21 22:20

    The problem here is you are not really expressing a generality. You have to implement OutputFormattedTypeInfo for each and every base type, so you might as well forget about the generic method and simply use the overloads (you do not need generics here):

    public void OutputFormattedTypeInfo(BaseType theType)
    {
        // ...
    }
    
    public void OutputFormattedTypeInfo(MySuperType theType)
    {
        // ...
    }
    
    public void OutputFormattedTypeInfo(MyOtherSuperType theType)
    {
        // ...
    }
    

提交回复
热议问题