我正在尝试使用许多扩展基本描述符的泛型和描述符创建一个流畅的接口。我把它放在一个 github repo 中,因为在这里粘贴所有代码会使其不可读。
在阅读了 Eric Lippert 关于类型约束的文章(http://blogs.msdn.com/b/ericlippert/archive/2009/12/10/constraints-are-not-part-of-the-signature.aspx)并阅读之后没有使用通用扩展方法进行类型推断我对这个主题的理解更好一些,但我仍然有问题。
假设您有一些允许流畅调用的类:
var giraffe = new Giraffe();
new ZooKeeper<Giraffe>()
.Name("Jaap")
.FeedAnimal(giraffe);
var reptile = new Reptile();
new ExperiencedZooKeeper<Reptile>()
.Name("Martijn")
.FeedAnimal(reptile)
.CureAnimal(reptile);
这些类看起来像这样:
public class ZooKeeper<T>
where T : Animal
{
internal string name;
internal List<T> animalsFed = new List<T>();
// this method needs to be fluent
public ZooKeeper<T> Name(string name)
{
this.name = name;
return this;
}
// this method needs to be fluent
public ZooKeeper<T> FeedAnimal(T animal)
{
animalsFed.Add(animal);
return this;
}
}
public class ExperiencedZooKeeper<T> : ZooKeeper<T>
where T : Animal
{
internal List<T> animalsCured = new List<T>();
// this method needs to be fluent
// but we must new it in order to be able to call CureAnimal after this
public new ExperiencedZooKeeper<T> Name(string name)
{
base.Name(name);
return this;
}
// this method needs to be fluent
// but we must new it in order to be able to call CureAnimal after this
public new ExperiencedZooKeeper<T> FeedAnimal(T animal)
{
base.FeedAnimal(animal);
return this;
}
// this method needs to be fluent
public ExperiencedZooKeeper<T> CureAnimal(T animal)
{
animalsCured.Add(animal);
return this;
}
}
我试图摆脱ExperiencedZooKeeper
隐藏ZooKeeper
. 不同之处在于new
方法ExperiencedZooKeeper
返回正确的类型。AFAIK没有方法就无法做到这一点new
。
我尝试采用的另一种方法是将“设置器”移动到扩展方法。这适用于 .Name() 方法,但它引入了一个ZooKeeperBase
包含内部字段的方法:
public abstract class ZooKeeperBase
{
internal string name;
}
public class ZooKeeper<T> : ZooKeeperBase
where T : Animal
{
internal List<T> animalsFed = new List<T>();
// this method needs to be fluent
public ZooKeeper<T> FeedAnimal(T animal)
{
animalsFed.Add(animal);
return this;
}
}
public static class ZooKeeperExtensions
{
// this method needs to be fluent
public static TZooKeeper Name<TZooKeeper>(this TZooKeeper zooKeeper, string name)
where TZooKeeper : ZooKeeperBase
{
zooKeeper.name = name;
return zooKeeper;
}
}
但是这种确切的方法不适用于 FeedAnimal(T animal),它需要一个额外的类型参数:
// this method needs to be fluent
public static TZooKeeper FeedAnimal<TZooKeeper, T>(this TZooKeeper zooKeeper, T animal)
where TZooKeeper : ZooKeeper<T>
where T : Animal
{
zooKeeper.animalsFed.Add(animal);
return zooKeeper;
}
这仍然可以并且效果很好,您仍然可以流利地调用它:
new ExperiencedZooKeeper<Reptile>()
.Name("Martijn")
.FeedAnimal(reptile)
.CureAnimal(reptile);
当我尝试使以下方法流利时,真正的问题就开始了:
public static TZooKeeper Favorite<TZooKeeper, T>(this TZooKeeper zooKeeper, Func<T, bool> animalSelector)
where TZooKeeper : ZooKeeper<T>
where T : Animal
{
zooKeeper.favoriteAnimal = zooKeeper.animalsFed.FirstOrDefault(animalSelector);
return zooKeeper;
}
你不能这样打电话Favorite
:
new ExperiencedZooKeeper<Reptile>()
.Name("Eric")
.FeedAnimal(reptile)
.FeedAnimal(new Reptile())
.Favorite(r => r == reptile)
因为它会导致与No type inference with generic extension method相同的问题,但是,这种情况稍微复杂一些,因为我们已经有了一个 Type 参数 TZookKeeper 来描述我们需要的 T。但就像 Eric Lipperts 博客文章一样,类型约束不是签名的一部分:
The type arguments for method 'TestTypeInference5.ZooKeeperExtensions.Favorite<TZooKeeper,T>(TZooKeeper, System.Func<T,bool>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
有关完整代码,请参阅https://github.com/q42jaap/TestTypeInference 这个 repo 中的自述文件实际上解释了我试图解决的现实问题。
所以问题真的是,有没有一种方法可以创建这种流畅的方法风格,而不会将 ZooKeeper 的每个方法添加到 ZooKeeper 的每个子类中,同时new
隐藏 ZooKeeper 本身的方法?