将对象转换为接口 [英] cast object to interface

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

问题描述




有人可以解释投射到界面背后的想法吗?


例如:

- >我有一个包含Read()方法的IInterface。

- >我有一个对象obj实现IInterface。


为什么有人会这样做,这是什么意思?


对象obj = new Object();

IInterface var =(IInterface)obj

var.Read()

谢谢

Hi,

Can someone explain the idea behind casting to an interface?

For example:
-> I have an IInterface that contains a Read() method.
-> I have an object "obj" that implements IInterface.

Why would someone do the following and what does it mean?

Object obj = new Object();
IInterface var = (IInterface) obj
var.Read()
Thanks

推荐答案

你不能这样做。对象没有实现IInterface。您创建的类型必须实现该界面(同意合同,

意义),以使演员成功。


你不能成功地将任何类型转换为任何其他任意类型。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam。 guard.caspershouse.com


< mr ******** @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
You can''t do this. Object doesn''t implement IInterface. The type that
you create has to implement that interface (agree to the contract, in a
sense), in order for the cast to succeed.

You can''t just cast any type to any other arbitrary type successfully.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<mr********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...


有人可以解释投射到界面背后的想法吗?

例如:
- >我有一个包含Read()方法的IInterface。
- >我有一个对象obj实现IInterface。

为什么有人会做以下事情,它是什么意思?

对象obj = new Object();
IInterface var =(IInterface) obj
var.Read()

谢谢
Hi,

Can someone explain the idea behind casting to an interface?

For example:
-> I have an IInterface that contains a Read() method.
-> I have an object "obj" that implements IInterface.

Why would someone do the following and what does it mean?

Object obj = new Object();
IInterface var = (IInterface) obj
var.Read()
Thanks



>

有人可以解释一下投射到界面背后的想法吗?

例如:
- >我有一个包含Read()方法的IInterface。
- >我有一个对象obj实现IInterface。
为什么有人会这样做以及它意味着什么?

对象obj = new Object();
IInterface var =(IInterface)obj var.Read()
谢谢

Can someone explain the idea behind casting to an interface?

For example:
-> I have an IInterface that contains a Read() method.
-> I have an object "obj" that implements IInterface.
Why would someone do the following and what does it mean?

Object obj = new Object();
IInterface var = (IInterface) obj
var.Read()
Thanks




假设Object obj = new Object();实际上意味着SomeObjectThatImplemetsIInterface

obj = new SomeObjectThatImplemetsIInterface();,在投射到IInterface时我真的没有意义,

。如果确实SomeObjectThatImplemetsIInterface

实现了IInterface那么你c(w)应该只调用obj.Read();


我的猜测是代码是由没有经验的程序员写的。如果我错了,请

纠正我。


Chris



Assuming that "Object obj = new Object();" really means "SomeObjectThatImplemetsIInterface
obj = new SomeObjectThatImplemetsIInterface();", there really is no point,
that I can think of, in casting to IInterface. If it is true that SomeObjectThatImplemetsIInterface
implements IInterface then you c(w)ould just call obj.Read();

My guess is that the code was written by an inexperienced programmer. Please
correct me if I''m wrong.

Chris


通常情况下,如果你有支持接口IInterface的对象,你的

成员变量将被定义为类型IInterface。也。但是,

你最有可能使用反射(直接或通过类

工厂)来创建你的IInterface对象,并且反射返回

引用类型对象,因为它无法传回特定类型

引用。在某些时候,该对象引用必须转换为

接口。 Reflection允许您以编程方式创建在编译时可能甚至不知道的类型。

Normally, if you have objects that support interface "IInterface", your
member variable will be defined as type "IInterface" also. However,
you will most likely use reflection (either directly or via a class
factory) to create your IInterface object, and reflection returns
references to type object because it cannot pass back a specific type
reference. At some point, that object reference must be cast to the
interface. Reflection allows you to programmatically create types that
you may not even know about at compile time.


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

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