【发布时间】:2020-01-10 18:39:36
【问题描述】:
我想使用模式匹配来替换多个if 语句,如下面的方法Select<T>() 所示。我想在typeof(T) 上使用switch() 语句。
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) 语句中使用了一个instance,并且像下面这样的代码显然是错误的
switch(typeof(T))
{
case // what here?
}
【问题讨论】:
-
你能分享剩下的代码和最小的可重现样本吗?实际上,类型模式匹配是在 c# 7 中添加的
-
还有两个相关的GitHub线程,switch on System.Type和pattern match via generic constraint
-
@PavelAnikhouski - 我制作了一个 mcve 来展示这个问题。
-
其实this answer 到Switching on type with a generic return type 完全回答了你的问题吗?
-
@ja72 - 是的,答案指出目前还没有实现一个漂亮的方法,指向一些开放的增强线程,并提供了一个hacky解决方法。您当前的代码看起来比解决方法恕我直言。
标签: c# generics pattern-matching c#-7.0