为什么需要演这个对象? [英] Why does this object need to be cast?

查看:49
本文介绍了为什么需要演这个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个ownerdraw drawmode的示例代码中,为什么'(ComboBox)发送者''

代码行需要在这个事件处理程序中?

是不是cboFont通过托管堆而不是堆栈传递到这个

cboFont_DrawItem事件处理程序中?为什么需要演员? -hazz


,.................

cboFont.Items.AddRange(FontFamily.Families) ;

}


private void cboFont_DrawItem(对象发送者,

System.Windows.Forms.DrawItemEventArgs e){


ComboBox cboFont =(ComboBox)发送者; .........,

In this sample code of ownerdraw drawmode, why does the ''(ComboBox) sender''
line of code need to be there in this event handler?
Isn''t cboFont passed via the managed heap, not the stack, into this
cboFont_DrawItem event handler? Why does it need to be cast? -hazz

,.................
cboFont.Items.AddRange(FontFamily.Families);
}

private void cboFont_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e) {

ComboBox cboFont = (ComboBox) sender; .........,

推荐答案




活动的签名是EventHandler(对象发送者,EventArgs e)


意味着你必须将发送者强制转换为正确的类型。


你会发现这种情况几乎无处不在。


干杯,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局

Hazz <公顷** @ nospameroosonic.net>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Hi,

The signature of the event is EventHandler( object sender, EventArgs e )

meaning that you have to cast sender to the correct type.

You will find this situation almost everywhere.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Hazz" <ha**@nospameroosonic.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
在此ownerdraw drawmode的示例代码,为什么'(ComboBox)
sender''代码行需要在这个事件处理程序中?
不是cboFont通过托管堆传递,而不是堆栈,进入这个
cboFont_DrawItem事件处理程序?为什么需要演员? -hazz

,.................
cboFont.Items.AddRange(FontFamily.Families);
} System.Windows.Forms.DrawItemEventArgs e){

ComboBox cboFont =(ComboBox)发送者; .........,
In this sample code of ownerdraw drawmode, why does the ''(ComboBox)
sender'' line of code need to be there in this event handler?
Isn''t cboFont passed via the managed heap, not the stack, into this
cboFont_DrawItem event handler? Why does it need to be cast? -hazz

,.................
cboFont.Items.AddRange(FontFamily.Families);
}

private void cboFont_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e) {

ComboBox cboFont = (ComboBox) sender; .........,



谢谢Ignacio,但我想我没有清楚地问我的问题。

如果事件处理程序永远不会处理除此之外的任何事情(在这种情况下)

组合框,为什么我需要转换为组合框?

是因为发件人是通用对象类型?运行时不知道

这是一个类型为combobox的对象吗?

如果我通过引用而不是按值传递,则不保留堆

组合框信息?或者这是元数据的东西吗?

这就是为什么我经常在

方法的开头看到''TypeOf''关键字,以测试是否可能作为

参数传入的这个对象是Combobox类型的?或者这是出于其他原因吗?

thx,-hazz

" Ignacio Machin(.NET / C#MVP)" < ignacio.machin AT dot.state.fl.us>在消息新闻中写了

:e1 ************* @ TK2MSFTNGP12.phx.gbl ...
Thank you Ignacio but I guess I didn''t ask my question clearly.
If the eventhandler will never handle anything other than (in this case)
comboboxes, why do I need to cast to comboboxes?
Is it because sender is of a generic object type? Does the runtime not know
that this is an object of type combobox?
If I am passing by reference rather than by value, doesn''t the heap retain
the combobox info? Or is this a metadata thing?
Is that why I also often see the ''TypeOf'' keyword at the beginning of a
method, to test to see if perhaps this object that is passed in as an
argument is of the type Combobox? Or is this for some other reason?
thx, -hazz
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:e1*************@TK2MSFTNGP12.phx.gbl...


事件的签名是EventHandler(对象发送者,EventArgs e)

意味着你必须将发送者强制转换为正确的类型。

你会发现这个几乎无处不在。

干杯,

- << Ignacio Machin,
ignacio.machin at dot.state.fl.us
佛罗里达州交通局

Hazz <公顷** @ nospameroosonic.net>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Hi,

The signature of the event is EventHandler( object sender, EventArgs e )

meaning that you have to cast sender to the correct type.

You will find this situation almost everywhere.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Hazz" <ha**@nospameroosonic.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
在此示例代码中ownerdraw drawmode,为什么'(ComboBox)
sender''代码行需要在这个事件处理程序中?
不是cboFont通过托管堆而不是堆栈传递到这个
cboFont_DrawItem事件处理程序?为什么需要演员? -hazz

,.................
cboFont.Items.AddRange(FontFamily.Families);
} System.Windows.Forms.DrawItemEventArgs e){

ComboBox cboFont =(ComboBox)发送者; .........,
In this sample code of ownerdraw drawmode, why does the ''(ComboBox)
sender'' line of code need to be there in this event handler?
Isn''t cboFont passed via the managed heap, not the stack, into this
cboFont_DrawItem event handler? Why does it need to be cast? -hazz

,.................
cboFont.Items.AddRange(FontFamily.Families);
}

private void cboFont_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e) {

ComboBox cboFont = (ComboBox) sender; .........,






" Hazz" <公顷** @ nospameroosonic.net>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

"Hazz" <ha**@nospameroosonic.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
谢谢Ignacio,但我想我没有清楚地问我的问题。
如果事件处理程序永远不会处理除此之外的任何事情(在这种情况下)
组合框,为什么我需要转换为组合框?是因为发件人属于通用对象类型吗?运行时是否知道这是一个类型为combobox的对象?
如果我通过引用而不是值传递,那么堆不会保留组合框信息吗?或者这是元数据的东西吗?
这就是为什么我经常在
方法的开头看到''TypeOf''关键字,以测试是否可能将此对象作为一个传入
参数是Combobox的类型?或者这是出于其他原因吗?
thx,-hazz
Thank you Ignacio but I guess I didn''t ask my question clearly.
If the eventhandler will never handle anything other than (in this case)
comboboxes, why do I need to cast to comboboxes?
Is it because sender is of a generic object type? Does the runtime not
know that this is an object of type combobox?
If I am passing by reference rather than by value, doesn''t the heap retain
the combobox info? Or is this a metadata thing?
Is that why I also often see the ''TypeOf'' keyword at the beginning of a
method, to test to see if perhaps this object that is passed in as an
argument is of the type Combobox? Or is this for some other reason?
thx, -hazz




首先,不要使用泛型这个词来形容一般的东西。这个

会让你感到困惑,当你转移到.NET 2.0并且实际上有

泛型类型。


至于你的问题。事件处理程序由一般代理定义,

在整个.NET框架中用于简单事件。因此即使

你的组合框知道它的类型,代表也没有,你必须求助于

向下转换。


组合框触发事件并将自己作为发送者发送,并在

过程中将其上传到Object。为了以比对象更多的方式访问实例,您必须将其向下转换为适当的类型。如果你只需要
需要它的名字你可以向下转换为Component,如果你需要它的位置

你可以向下转向控制。但是,如果你想知道它已经被选中了

项目你需要向下投射到ComboBox。


向下倾斜是危险的,因为你必须知道什么类型实例可以投射

到。这是仿制药将尝试用参数化类型为我们解决的问题。

这是一件非常好的事情。


..NET不仅仅是关于托管数据(即有垃圾收集器),还有
托管代码。因此,如果您尝试将一个ComboBox的实例从

对象引用向下转换为StringBuilder,则公共语言运行时(CLR)将检查它是否为b $ b如果你尝试的话,ok cast并抛出InvalidCastException。


对于像C ++和Delphi这样的非托管代码,情况也是如此。如果你做了一个非法的非法演员,你真的把事搞砸了,你最后用一个参考

指向一种类型的对象,使用错误类的方法,通常是会给你带来臭名昭着的常规保护错误或访问违规行为

异常。这也是为什么C ++

和Delphi比.NET和Java运行得更好的原因。前者

不检查我们做什么,后者在我们这样做时握着我们的手。


C#中的强制转换实际上是方法,称为强制转换运算符,即使它们通过CIL编译器对常见类型

系统(CTS)中的大多数类型进行了非常优化。


C#引入了两种类型的转换运算符; implict和explicit-casts。

在System.Byte和System.Int32上使用强制转换运算符的示例:


myInt = myByte; //隐式转换,8位总是可以成为32位

值,它会抛出异常。


如果你在C#中定义一个类,你可以提供它你自己的强制转换操作符。

因此,一个名为MyComboBox的子类可以成为一个StringBuilder,如果你想要的话,可以用
。 =)


但请注意笑脸。在超载

运算符,甚至是强制运算符之前,你应该非常小心。只有当

存在从一种类型到另一种类型的自然而简单的转换时,才应使用强制转换运算符。注意

a System.Byte可以毫不费力地转换为System.Int32,因此

支持一个强制转换操作符,同时使其成为一个涉及的字符串

实际工作,所以你必须调用方法.ToString()


希望这让你不那么困惑。=)


- Michael S




First thing, don''t use the word generic to describe something general. This
will just make you confused when you move to .NET 2.0 and actually have
generic types.

As for your problem. The event handler is defined by a general delegate that
is used all over the .NET framework for simple events. Hence even though
your combobox knows its type, the delegate does not and you must resort to
downcasting.

The combobox triggers the event and sends itself as sender and in the
process it gets upcasted to Object. In order to access the the instance as
more than a Object you must downcast it to an appropriate type. If you only
need it''s name you could downcast to Component and if you need it''s position
you could downcast to Control. However, if you want to know it''s selcected
item you need to downcast to ComboBox.

Downcasting is dangerous as you must know what type the instance can be cast
to. This is what generics will try to solve for us with parameterized types.
It''s a very nifty thing to have.

..NET is not only about managed data (ie having a garbage collector) but also
managed code. Hence if you try to downcast an instance of a ComboBox from an
Object-reference into a StringBuilder the Common Language Runtime (CLR) will
check if it''s an ok cast and throw a InvalidCastException if you tried.

The same is not true for unmanaged code like C++ and Delphi. If you make an
illegal cast you really screw things up and you wind up with a reference
pointing to one type of object using methods of the wrong class, typically
giving you the infamous ''General Protection Fault'' or ''Access Violation''
exception when the code being called makes non sense. This is also why C++
and Delphi sport much better performance than .NET and Java. The former
don''t check what we do and the latter hold our hands while we do it.

Casts in C# is actually methods, called cast operators, even though they get
very much optimized by the CIL-compiler for most types in the Common Type
System (CTS).

C# introduces two types of cast operators; implict- and explicit-casts.
Example with cast operators on System.Byte and System.Int32:

myInt = myByte; //implicit cast, a 8-bit can always become an 32-bit
myByte = (Byte)myInt; //explicit cast needed as an int may contain a large
value which throws an exception.

If you define a class in C# you can supply it with your own cast operators.
Hence a subclass called MyComboBox could very much become a StringBuilder,
if you''d want. =)

But note the smiley. You should be very careful before overloading
operators, even cast operators. Cast operators should only be used when
there is a natural and simple conversion from one type to another. Note how
a System.Byte can be cast to System.Int32 without effort and therefor
supports a cast operator for it, while making it into a string involve
actual work and so you must call the method.ToString()

Hope this made you less confused.=)

- Michael S



这篇关于为什么需要演这个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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