mardi 2 juin 2020

How can I know both class are from same Generic class

Here is sample code. How can I know both Foo and Bar are from same class Base<>?

class Program
{
    static void Main(string[] args)
    {
        var foo = typeof(Foo).IsAssignableFrom(typeof(Base<,>));
        var bar = typeof(Bar).IsAssignableFrom(typeof(Base<,>));
    }
}

public abstract class Base<TInput, TOutput>
{
    public abstract TOutput Run(TInput input);
}


public class Foo : Base<int, string>
{
    public override string Run(int input)
    {
        return input.ToString();
    }
}

public class Bar : Base<string, string>
{
    public override string Run(string input)
    {
        return input.Replace(".", "").ToString();
    }
}

enter image description here





Aucun commentaire:

Enregistrer un commentaire