是否有可能使"这种类型的"在C#泛型?能使、类型、QUOT、泛型

由网友(灭婊达人i)分享简介:类的理论问题。相当长可以随意跳过,如果你没有心情的理论。的假设你有两个类,一个继承了另一个。基类是通用的,并且具有,在密闭型必须返回这个封闭类型的一些实例方法。 Imagine that you have two classes, one inherited from another. The base clas...

类的理论问题。相当长可以随意跳过,如果你没有心情的理论。的

假设你有两个类,一个继承了另一个。基类是通用的,并且具有,在密闭型必须返回这个封闭类型的一些实例方法。

Imagine that you have two classes, one inherited from another. The base class is generic and has a method that in the closed type must return some instance of this closed type.

这样的(注 ??? 文本):

public class Adapter<T>
{
 public virtual ??? DoSomething()
 {
  ...
 }
}

public class AdaptedString : Adapter<String>
{
 public override AdaptedString DoSomething()
 {
  ...
 }
}

我不能这样做,因为没有办法指将从泛型类型派生一个封闭的类型。 (对不起,碎语,只是不知道该如何前preSS的。)没有关键字代替设置 ??? 来指定该方法将返回这将是从这个泛型类型派生类型的实例。

I can't do it because there is no way to refer to a closed type that will be derived from a generic type. (Sorry for broken language, just don't know how to express it.) There is no keyword to set in place of ??? to specify that this method will return instance of type that would be derived from this generic type.

相反,我可以明确地使用合格的类型名称的通用基础的解决办法。但它看起来是多余的。

Instead, I can use a workaround of explicitly passing the type name to the generic base. But it looks redundant.

public class Adapter<TThis,T>
{
 public virtual TThis DoSomething()
 {
  ...
 }
}

public class AdaptedString : Adapter<AdaptedString,String>
{
 public override AdaptedString DoSomething()
 {
  ...
 }
}

如果在基类中我需要访问 TThis 实例的成员,我要补充的约束。这一次,它看起来丑陋 - 注意约束:

And if in the base class I need to access members of TThis instance, I have to add a constraint. This time it looks ugly - note the constraint:

public class Adapter<TThis,T>
 where TThis : Adapter<TThis, T>
{
 protected int _field; 

 ...

 public bool Compare( TThis obj )
 {
  return _field == obj._field;
 }
}

public class AdaptedString : Adapter<AdaptedString,String>
{
 ...
}

是的,这是所有的工作,但它会更好看,如果我可以简单地使用一些关键字,而不是 ??? 第一code片段。类似 thistype

Yes, it is all working, but it would look better if I can simply use some keyword instead of ??? in first code fragment. Something like "thistype".

你觉得可以吗?是它有用吗?或者,也许这只是普通的愚蠢?

How do you think will it work? Is it useful? Or maybe this is just plain stupid?

推荐答案

这没有什么,这使得此模式更容易,而事实上该模式是不太防弹无论如何 - 因为你可以有:

There's nothing which makes this pattern easier, and in fact the pattern isn't quite bulletproof anyway - because you can have:

class TypeA : Adapter<TypeA, string>

class TypeB : Adapter<TypeA, string> // Bug!

下面第二行是完全合法的 - 类型A 是一个有效的类型参数的 TThis 类型参数,甚至虽然这不是我们想要的。基本类型系统不会让我们的前preSS的概念,T必须是这种类型的。

The second line here is entirely legal - TypeA is a valid type argument for the TThis type parameter, even though it's not what we wanted. Basically the type system doesn't let us express the concept of "T must be this type."

我不同意那些谁说,这是一个坏或无用的模式,但是。我发现它很有用(如果合并)的 Protocol Buffers的 - 这将是的多的更糟离不开它。例如:

I disagree with those who say it's a bad or useless pattern, however. I've found it useful (if complicated) in Protocol Buffers - which would be much worse off without it. For example:

Foo foo = new Foo.Builder { Name="Jon" }.Build();

是行不通的,如果 Foo.Build()不强类型的返回,即使在构建指定方法IBuilder&LT; ...&GT;

wouldn't work if Foo.Build() wasn't strongly typed to return Foo, even though the Build method is specified in IBuilder<...>.

这是值得避免这一点,如果你很容易的可以的,只是因为它变得如此复杂 - 但我不认为这是一个有用的模式要知道

It's worth avoiding this if you easily can simply because it gets so complicated - but I do think it's a useful pattern to know.

阅读全文

相关推荐

最新文章