简体   繁体   English

Vb.net接口,make方法只接受与参数相同类型的实例

[英]Vb.net Interface, make method only accept instances of same type as parameter

I'm trying to define a method in my vb.net interface that will only accept objects that inherit from the same interface and are also of the same class as the instance which receives the method call. 我正在尝试在我的vb.net接口中定义一个方法,该方法只接受从同一接口继承的对象,并且与接收方法调用的实例属于同一个类。 Is this possible in vb.net? 这可能在vb.net中吗?

Below I try to illustrate with a (invalid) generic type constraint what I wish to do: 下面我尝试用(无效的)泛型类型约束来说明我想做的事情:

Public Interface IFoo

   CompareStuff(Of T as sametype)(obj as T) as Boolean

End Interface

You are looking for the Curiously recurring template pattern . 您正在寻找奇怪的重复模板模式

I'm a little rusty on my VB, so here it is in C# 我的VB上有点生疏,所以这里是C#

public interface IFoo<T>
    where T : IFoo<T>
{
    bool CompareStuff(T obj);
}

Then you implement it like this 然后你像这样实现它

public class Foo : IFoo<Foo>
{
    bool CompareStuff(Foo obj);
}

Check out this article about it from Eric Lippert. 从Eric Lippert看看这篇文章 Pay special attention to the end where he says: 特别注意他说的结尾:

My advice is to think very hard before you implement this sort of curious pattern in C#; 我的建议是在C#中实现这种奇怪的模式之前要认真思考; do the benefits to the customer really outweigh the costs associated with the mental burden you're placing on the code maintainers? 对客户带来的好处是否真的超过了与代理维护者的心理负担相关的成本?

Public Interface IFoo(Of T)
    CompareStuff(obj As T) As Boolean

Use like this: 使用这样:

Public Class Foo Implements IFoo(Of Foo)
    Function CompareStuff(obj As Foo) As Boolean

but unfortunately also allows this to compile: 但不幸的是还允许这个编译:

Public Class Bar Implements IFoo(Of Foo)
    Function CompareStuff(obj As Foo) As Boolean

Can't think of anything properly constraining right now. 现在想不出任何正确的约束。

If type X derives from type Y , and a Y can perform some operation upon another Y , then the Liskov Substitution Principle requires that a Y must be able to perform that same action on an X , and also that an X must be able to perform that same action on a Y . 如果类型X派生自类型Y ,并且Y可以对另一个Y执行某些操作,那么Liskov替换原则要求Y必须能够对X执行相同的操作,并且X必须能够执行对Y同样动作。 Putting those requirements together, an X must be able to perform that operation upon another X . 将这些要求放在一起, X必须能够在另一个X上执行该操作。

Note that it would be possible for an X to be able to perform some operation on another X , without such action being performable by, or upon, a Y . 注意, X可能能够在另一个X上执行某些操作,而这种动作不能由Y执行,也不能在Y

Incidentally, while it's useful to implement IEquatable<T> for structures and sealed classes, the question of whether any object is equal to any other can be answered meaningfully for any two objects of any types. 顺便提一下,虽然对结构和密封类实现IEquatable<T>很有用,但对于任何类型的任何两个对象,可以有意义地回答任何对象是否与任何其他对象相等的问题。 For example, if one has a collection of Cat , one may reasonably try to compare the objects in that collection against a parameter of type SiameseCat (while the collection may contain instances of PersianCat , it might also contain some of type SiameseCat ) or type Animal (some passed-in objects might be of type Dog . but others might be of type Cat ). 例如,如果一个人的集合Cat ,我们可以合理地尝试在集合中的比较对象对类型的参数SiameseCat (而收集可能含有的情况下PersianCat ,它也可能包含某种类型的SiameseCat )或输入Animal (某些传入的对象可能是Dog类型。但其他可能是Cat类型)。 While querying a collection of Cat to see if it contains some particular Dog might be "silly", the collection should have no problem answering the query ("Zero instances found"). 在查询Cat集合以查看它是否包含某些特定的Dog可能是“愚蠢的”,该集合在回答查询时应该没有问题(“找到零实例”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM