如何使用Type对象进行转换? [英] How to cast using a Type object?

查看:63
本文介绍了如何使用Type对象进行转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#编写Windows MDI应用程序。我想将一个对象转换为特定类型。我知道该对象属于兼容类型。下面的代码显示了我想要做的,但是没有编译,其中强制转换为(c.ItemType)。产生错误无法找到类型或命名空间。我知道每个控件c都是具有公共属性ItemType返回类型


foreach(mybboBox c in cbControls


name =(string)controlToNamesTable [c]

this.queryArgs.Index =(Int32)allNamesToIndexTable [name]

SettingReqEvents.RaiseSettingReqEvent(this,queryArgs)

c.SelectedItem =(c.ItemType)queryArgs.Dat

基本上我要做的是遍历一组MyComboBox控件,通过引发事件来获取当前设置值。返回的事件参数包含一个包含当前设置的对象Dat。是一个枚举类型,但是枚举类型被重载。我需要在设置SelectedItem属性时将它强制转换为MyComboBox控件所期望的类型。一个枚举类型集示例如下所示


public enum Mod


OFF

CW

TEST

ACTIVE

CA

}


公共枚举Mode_MU


OFF = Mode.O FF

CW = Mode.CW

ALIVE = Mode.ACTIVE

B_TEST = Mode.TEST

D_CAL = Mode.CAL

}


公共枚举Mode_RU


OFF = Mode.OFF

CW = Mode.CW

CALL = Mode.ACTIVE

CELL = Mode.ACTIVE

GTest = Mode.TEST

GCal = Mode.CAL

}


数据对象将包含Mode_MUI或Mode_RUI类型的值,我应该能够转换为类型Mode_MUI。请注意,MyComboBox控件集合中的每个控件都将具有不同的枚举类型,类似于下面提供的示例。所以我希望能够投射Dat。对象为控件已知的类型(ItemType)


有没有办法使用控件的ItemType成员将对象强制转换为正确的类型


谢谢

Dav

I am writing a Windows MDI application in C#. I would like to cast an object to a specific type. I know that the object is of a compatible type. The code below shows what I would like to do, but does not compile, where the cast "(c.ItemType)" produces the error "type or namespace could not be found". I know that each control "c" has a public property "ItemType" which returns a Type

foreach ( MyComboBox c in cbControls

name = (string)controlToNamesTable[ c ]
this.queryArgs.Index = (Int32)allNamesToIndexTable[ name ]
SettingReqEvents.RaiseSettingReqEvent( this, queryArgs )
c.SelectedItem = (c.ItemType)queryArgs.Dat
Basically what I am trying to do is cycle through a collection of MyComboBox controls, obtain the current setting value by raising an event. The event argument returned contains an object Dat which contains the current setting. The setting is of an enumeration type, but the enumeration type is overloaded. I need to cast it to the type expected by the MyComboBox control when setting the SelectedItem property. One example enumeration type set is shown below

public enum Mod

OFF
CW
TEST
ACTIVE
CA
}

public enum Mode_MU

OFF = Mode.OFF
CW = Mode.CW
ALIVE = Mode.ACTIVE
B_TEST = Mode.TEST
D_CAL = Mode.CAL
}

public enum Mode_RU

OFF = Mode.OFF
CW = Mode.CW
CALL = Mode.ACTIVE
CELL = Mode.ACTIVE
GTest = Mode.TEST
GCal= Mode.CAL
}

The "Dat" object will contain a value of either Mode_MUI or Mode_RUI type, which I should be able to cast to type Mode_MUI. Note that each control in the collection of MyComboBox controls will be of a different enumeration type, similar to the example provided below. So I want to be able to cast the "Dat" object to the type known by the control (ItemType)

Is there a way to use the control''s ItemType member to cast the object to the correct type

Thanks
Dav

推荐答案

c.SelectedItem是什么类型的?因为这才是最重要的。如果它是一个对象,

那么就没有必要先抛出它。


但是无论如何,System.ComponentModel.TypeConverter可以做什么你想要的。


-mike

MVP


" Dave Leach" < BA *** @ agilent.com>在消息中写道

新闻:8C ********************************** @ microsof t.com ...
What type is c.SelectedItem? Because that''s what matters. If it''s an object,
then there''s no point casting it first.

But anyways, System.ComponentModel.TypeConverter can do what you want.

-mike
MVP

"Dave Leach" <ba***@agilent.com> wrote in message
news:8C**********************************@microsof t.com...
我正在用C#编写Windows MDI应用程序。我想将
对象转换为特定类型。我知道该对象属于兼容类型。
下面的代码显示了我想要做的事情,但是没有编译,其中
强制转换(c.ItemType)产生错误无法找到类型或命名空间。我知道每个控件c都是具有公共属性ItemType
返回一个类型。

foreach(cbControls中的MyComboBox c)
{
name =(string)controlToNamesTable [c];
this.queryArgs .Index =(Int32)allNamesToIndexTable [name];
SettingReqEvents.RaiseSettingReqEvent(this,queryArgs);
c.SelectedItem =(c.ItemType)queryArgs.Dat;
}

基本上我要做的是循环浏览一组
MyComboBox控件,通过引发事件来获取当前设置值。
返回的事件参数包含一个包含<的对象Dat br />当前设置。该设置是枚举类型,但
枚举类型已重载。我需要在设置SelectedItem属性时将其强制转换为MyComboBox控件所期望的类型。一个
示例枚举类型集如下所示。

公共枚举模式
{
OFF,
CW,
TEST, ACTIVE,
CAL
};

公共枚举Mode_MUI
{OFF /> OFF = Mode.OFF,
CW = Mode.CW,
ALIVE = Mode.ACTIVE,
B_TEST = Mode.TEST,
D_CAL = Mode.CAL,
};

公共枚举Mode_RUI {
OFF = Mode.OFF,
CW = Mode.CW,
CALL = Mode.ACTIVE,
CELL = Mode.ACTIVE,
GTest = Mode。测试,
GCal = Mode.CAL,
};

数据 object将包含Mode_MUI或Mode_RUI类型的值,
我应该能够转换为类型Mode_MUI。请注意,MyComboBox控件集合中的每个控件都将具有不同的枚举类型,类似于下面提供的示例。所以我希望能够投射数据。对象为控件已知的类型(ItemType)。

有没有办法使用控件的ItemType成员将对象强制转换为正确的类型?

谢谢,
戴夫
I am writing a Windows MDI application in C#. I would like to cast an
object to a specific type. I know that the object is of a compatible type.
The code below shows what I would like to do, but does not compile, where
the cast "(c.ItemType)" produces the error "type or namespace could not be
found". I know that each control "c" has a public property "ItemType" which
returns a Type.

foreach ( MyComboBox c in cbControls )
{
name = (string)controlToNamesTable[ c ];
this.queryArgs.Index = (Int32)allNamesToIndexTable[ name ];
SettingReqEvents.RaiseSettingReqEvent( this, queryArgs );
c.SelectedItem = (c.ItemType)queryArgs.Dat;
}

Basically what I am trying to do is cycle through a collection of
MyComboBox controls, obtain the current setting value by raising an event.
The event argument returned contains an object Dat which contains the
current setting. The setting is of an enumeration type, but the
enumeration type is overloaded. I need to cast it to the type expected by
the MyComboBox control when setting the SelectedItem property. One
example enumeration type set is shown below.

public enum Mode
{
OFF,
CW,
TEST,
ACTIVE,
CAL
};

public enum Mode_MUI
{
OFF = Mode.OFF,
CW = Mode.CW,
ALIVE = Mode.ACTIVE,
B_TEST = Mode.TEST,
D_CAL = Mode.CAL,
};

public enum Mode_RUI
{
OFF = Mode.OFF,
CW = Mode.CW,
CALL = Mode.ACTIVE,
CELL = Mode.ACTIVE,
GTest = Mode.TEST,
GCal= Mode.CAL,
};

The "Dat" object will contain a value of either Mode_MUI or Mode_RUI type,
which I should be able to cast to type Mode_MUI. Note that each control
in the collection of MyComboBox controls will be of a different
enumeration type, similar to the example provided below. So I want to be
able to cast the "Dat" object to the type known by the control (ItemType).

Is there a way to use the control''s ItemType member to cast the object to
the correct type?

Thanks,
Dave






嗨戴夫,


我同意mike您应该使用TypeConverter,有关更多信息,请参阅:


实现类型转换器
http: //msdn.microsoft.com/library/de...us/cpguide/htm

l / cpconimplementingtypeconverter.asp


谢谢你你的耐心和合作。如果您有任何疑问或

担忧,请随时将其发布在小组中。我正准备等待

的帮助。


祝你好运,

Jeffrey Tan

Microsoft在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有授予任何权利。

Hi Dave,

I agree with mike that you should use TypeConverter, for more information
about how to implement TypeConverter, please refer to:

"Implementing a Type Converter"
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconimplementingtypeconverter.asp

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


foreach(cbControls中的MyComboBox c)
{
queryArgs.Index = c.Index;
SettingReqEvents.RaiseSettingReqEvent(this,queryArgs);
c.SelectedItem =(CAST)queryArgs.Dat;
}
foreach ( MyComboBox c in cbControls )
{
queryArgs.Index = c.Index;
SettingReqEvents.RaiseSettingReqEvent( this, queryArgs );
c.SelectedItem = ( CAST )queryArgs.Dat;
}



queryArgs.Dat是什么类型的?


-mike

MVP


What type is queryArgs.Dat?

-mike
MVP


这篇关于如何使用Type对象进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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