首页 > 解决方案 > 我可以对这样的代码使用模式匹配吗(在泛型上)

问题描述

我想使用模式匹配来替换多个if语句,如下所示的方法Select<T>()。我想switch()typeof(T).

static class Program
{
    static void Main(string[] args)
    {
        var doc = new Document();
        IWire select = doc.Select<IWire>();
    }

    public static T Select<T>(this Document document) where T : class, IGeneric
    {
        var t = typeof(T);
        if (t.IsAssignableTo(typeof(IWire)))
        {
            return document.SelectEntity(EntityType.Wire) as T;
        }
        if (t.IsAssignableTo(typeof(ISolid)))
        {
            return document.SelectEntity(EntityType.Solid) as T;
        }
        if (t.IsAssignableTo(typeof(ISurface)))
        {
            return document.SelectEntity(EntityType.Surface) as T;
        }
        // imagine a lot of if statements here
        return null;
    }
}

public enum EntityType
{
    Wire,
    Surface,
    Solid
}

public interface IGeneric
{
}

public interface IWire : IGeneric
{
}
public interface ISurface : IGeneric
{
}
public interface ISolid : IGeneric
{
}

public class Document
{
    public IGeneric SelectEntity(EntityType entity)
    {
        throw new NotImplementedException();
    }
}

我见过的所有例子都在语句中使用了一个实例switch(thing),下面这样的代码显然是错误的

switch(typeof(T))
{
    case // what here?
}

标签: c#genericspattern-matchingc#-7.0

解决方案


因此,基于评论的答案是目前C#还不能这样做(在此处此处提出)。

但是有一个 hack,在 h​​ttps://stackoverflow.com/a/46769048/380384 中有说明尽管不是完全相同的副本。

我可以使用的解决方案如下(感谢@dbc 为我指明了正确的方向)。

public static T Select<T>(this Document document) where T : class, IGeneric
{
    var t = typeof(T);
    switch (true)
    {
        case var _ when t.IsAssignableFrom(typeof(IWire)):
            return document.SelectEntity(EntityType.Wire) as T;

        case var _ when t.IsAssignableFrom(typeof(ISurface)):
            return document.SelectEntity(EntityType.Surface) as T;

        case var _ when t.IsAssignableFrom(typeof(ISolid)):
            return document.SelectEntity(EntityType.Solid) as T;

        default:
            return null;
    }

}

但就可读性和设计意图而言,它可能确实是对if语句顺序的改进。


推荐阅读