将类接口转换为类 [英] Casting a interface to class

查看:92
本文介绍了将类接口转换为类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读OPC客户端源代码。

我不明白的一些语法。



i am reading a OPC client source code.
some syntax i don't understand.

Opc.Da.Subscription group;

          var item = server.CreateSubscription(groupState);   //item is a Interface
          group = (Subscription)item;





这样的语法:object =(class)接口

是否意味着铸造一个接口(项目)到班级?



我尝试过:



group = item;



它返回错误:



syntax like this : object= (class)Interface
does it mean Casting a Interface(item) to a Class?

What I have tried:

group = item;

it return error:

引用:

CS0266无法将类型'Opc.Da.ISubscription'隐式转换为'Opc.Da.Subscription'。存在显式转换(您是否错过了演员表?)

CS0266 Cannot implicitly convert type 'Opc.Da.ISubscription' to 'Opc.Da.Subscription'. An explicit conversion exists (are you missing a cast?)





这就是为什么我猜它意味着将类型转换为类。

但实际上它意味着什么?

谢谢



That why I guess it means Casting Interface to Class.
but what does it means actually?
thanks

推荐答案

假设订阅实现了ISubscription,那么显式演员:

Assuming Subscription implements ISubscription, there is an explicit cast:
Subscription sub = (Subscription) iSub;

但你不能这样隐式地这样做:

But you can't do it implicitly like this:

Subscription sub = iSub;

为什么不呢?好吧......明确被禁止了!

如果你看这里:(这是退役文件,所以你必须下载PDF)



它说:

Why not? Well ... it's explicitly forbidden!
If you look here: (it's retired documentation, so you'll have to download the PDF)

It says:

Quote:

不允许用户定义的转换从接口类型转换为接口类型。特别是,此限制确保在转换为接口类型时不会发生用户定义的转换,并且仅当转换的对象实际实现指定的接口类型时,转换为接口类型才会成功。

User-defined conversions are not allowed to convert from or to interface-types. In particular, this restriction ensures that no user-defined transformations occur when converting to an interface-type, and that a conversion to an interface-type succeeds only if the object being converted actually implements the specified interface-type.

其中意味着您无法定义从接口到具体类型的隐式转换。

Which means you cannot define an implicit conversion from an interface to a concrete type.


您理解的困难可能是因为您的具体示例。您有Subscription和ISubscription,并且在那里有一对一的关系,您可以互换使用它们中的任何一个。如果你看一个更一般的接口例子,你会更好地理解。 Subscription和ISubscription之间的区别在于Subscription只能引用具体的类Subcription,但ISubscription可以引用实现ISubscription的* any *具体类。在你的例子中只有一个这样的类,Subscription,所以很难理解接口和类之间的区别。



下面的例子使用了一个IAnimal接口和两个具体的两种类型的Cat和Dog都实现了这个界面。



Your difficulty in understanding is probably because of your specific example. You have Subscription and ISubscription and there is a one-to-one relationship there and you can really use either of them interchangeably. If you look at a more general example of interfaces you'll gain a better understanding. The difference between Subscription and ISubscription is that Subscription can only reference the concrete class Subcription, however ISubscription can reference *any* concrete class that implements ISubscription. In your example there is only one such class, Subscription, so it's harder to understand the difference between interfaces and classes.

The example below uses an IAnimal interface and two concrete classes of Cat and Dog that both implement this interface.

static Cat GetCatObject()
{
    return new Cat();
}

static IAnimal GetCatByInterface()
{
    return new Cat();
}

static Dog GetDogObject()
{
    return new Dog();
}

static IAnimal GetDogByInterface()
{
    return new Dog();
}

static void Main(string[] args)
{
    IAnimal catA = GetCatByInterface();
            
    // catA is IAnimal so it can point to any object that implements IAnimal
    // GetCatByInterface returns Cat so the code above is valid however catA
    // can point to anything that is IAnimal so the line below is also valid

    catA = GetDogByInterface();

    // so something of type IAnimal can reference anything IAnimal

    // now we'll use concrete classes rather than interfaces

    Cat catC = GetCatObject();

    // the code above is fairly obvious, catC is Cat and GetCatObject returns Cat

    Cat catD = (Cat)GetCatByInterface();

    // catD can only point to an object that is Cat, however GetCatByInterface can return
    // anything that implements IAnimal, and that includes Dog, there is no guarantee it returns Cat.
    // The calling code doesn't know what concrete object GetCatByInterface returns, only that it is IAnimal.
    // So we have to cast whatever comes back to Cat.  Now look at the line below

    Cat catE = (Cat)GetDogByInterface();

    // this code compiles because the calling code still doesn't know what GetDogByInterface
    // returns.  We know it returns IAnimal so it could return Cat, Dog, Chicken, however
    // when the code runs it will throw an exception as the object that GetDogByInterface
    // returns can't be cast to Cat.

    Console.ReadLine();
}


这篇关于将类接口转换为类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆