使用带单例对象的开关 [英] Use switch with singleton objects

查看:65
本文介绍了使用带单例对象的开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们不能使用带有单件对象的开关?我知道编译器

不能像常量值一样优化它们,但对于字符串

也是如此,并且它们在交换机中是允许的。


原因是我经常使用单例类作为枚举'

的替代品,因为当
$ b $时,枚举不会为我提供(可本地化的)名称或描述b例如我用它们填充组合框。


另外我不喜欢我不能在枚举类中放任何代码

虽然我经常有很多代码应该与

enum放在一起。

也许某些.NET会提供一个可扩展且灵活的枚举类

功能就像Java一样。

Why can''t we use switches with singleton objects? I know that the compiler
cannot optimize them like constant values but the same is true for strings
and they are allowed in switches.

The reason is that I often use singleton classes as replacement for enum''s
because enums does''nt provide me a (localizeable) names or descriptions when
I for example fill comboboxes with them.

Additionally I don''t like that I can''t put any code in the enum class
although I often have a lot of code that should be placed together with the
enum.
Maybe someaday .NET will provide an extensible and flexible enum class
feature like Java does.

推荐答案




首先你不能标记一个类型作为单身,你使它在代码中表现为一个单独的
单身。这是一个在语言中不存在的概念。

因此它将作为编译器的常规对象。


现在,你对期望如何给案件赋予独特的价值吗?


FRankly我不明白为什么班级需要单身人士。

你能进一步说明吗?解释为什么这个?

欢呼,

-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


" cody" <德******** @ gmx.de>在消息中写道

news:uX ************** @ tk2msftngp13.phx.gbl ...


First of all you cannot mark a type as singleton, you make it behave as a
singleton in code. That is a concept that does not exist in the language.
Therefore it will behave as a regular object to the compiler.

Now, how you expect to give unique values to the case(s) ?

FRankly I don''t understand why the need of the class being a singleton.
Could you further explain why this?
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"cody" <de********@gmx.de> wrote in message
news:uX**************@tk2msftngp13.phx.gbl...
为什么不能我们使用带有单件对象的开关?我知道编译器不能像常量值那样对它们进行优化,但对于字符串也是如此,它们在交换机中是允许的。

原因是我经常使用单例类作为enum'的替代
因为enums没有提供我(可本地化的)名称或描述
当我例如用它们填充组合框时。
此外,我不喜欢我不能在枚举类中添加任何代码
虽然我经常有很多代码应该与
枚举放在一起。
也许someaday .NET将提供像Java那样可扩展且灵活的枚举类

Why can''t we use switches with singleton objects? I know that the compiler
cannot optimize them like constant values but the same is true for strings
and they are allowed in switches.

The reason is that I often use singleton classes as replacement for enum''s
because enums does''nt provide me a (localizeable) names or descriptions
when
I for example fill comboboxes with them.

Additionally I don''t like that I can''t put any code in the enum class
although I often have a lot of code that should be placed together with
the
enum.
Maybe someaday .NET will provide an extensible and flexible enum class
feature like Java does.



cody写道:
为什么我们不能使用带有单例对象的开关?我知道编译器不能像常量值一样优化它们,但对于字符串
也是如此,它们在交换机中是允许的。


编译器甚至看不出它们是否是单身。


下面是否有问题?


类型t = o.GetType();

if(t == typeof(Singleton1)){

//做东西;

else if(o == typeof(Singleton2))

//做东西;

else

抛出新的ArgumentException (

string.Format(" Unknown type:{0}",t)," o"));


原因是我经常使用单例类替代枚举'
因为枚举不会为我提供(可本地化的)名称或描述,例如我用它们填充组合框。
Why can''t we use switches with singleton objects? I know that the compiler
cannot optimize them like constant values but the same is true for strings
and they are allowed in switches.
The compiler can''t even see whether they are singletons.

Is there a problem with the below?

Type t = o.GetType();
if ( t == typeof(Singleton1) ) {
// do stuff;
else if ( o == typeof(Singleton2) )
// do stuff;
else
throw new ArgumentException(
string.Format("Unknown type: {0}", t), "o"));

The reason is that I often use singleton classes as replacement for enum''s
because enums does''nt provide me a (localizeable) names or descriptions when
I for example fill comboboxes with them.




呃,你不能使用实例吗?哪个会给你一个类似的开关:


if(o == singleton1){

//做东西;

else if(o == singleton2)

//做东西;

else

抛出新的ArgumentException(" Unknown object", o);


-

Helge Jensen

mailto:他********* *@slog.dk

sip:他********** @ slog.dk

- =>塞巴斯蒂安的封面音乐: http://ungdomshus.nu < = -



Uh, couldn''t you use instances? Which would give you a similar "switch":

if ( o == singleton1 ) {
// do stuff;
else if ( o == singleton2 )
// do stuff;
else
throw new ArgumentException("Unknown object", "o");

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-


目前我们正在使用以下方案。

编译器可以确保传递的值真的是唯一的,因为我们

assign

在创建对象时的静态只读值,我认为编译器应该能够

来识别这种模式。


公共类EkpSchema:EnumWrapper

{

public static readonly EkpSchema ImmerLetzter = new EkpSchema(0," Immer

letzter Einkaufspreis" ;);

public static readonly EkpSchema EkpNeu = new EkpSchema(1,EKP Neu nach

Abverkauf);

public static readonly EkpSchema ImmerDurchschnitt = new EkpSchema(2,Immer

durchschnittl.EKP);

public static readonly EkpSchema EkpManuell = new EkpSchema(3,EKP

manuell") ;


EkpSchema(int id,string bez)

:base(id,bez)

{

}


public static EkpSchema GetByID(int val)

{

return(EkpSchema)EnumWrapper.GetByID(typeof (EkpSchema),val);

}


public static EkpSchema [] GetValues()

{

return(EkpSchema [])EnumWrapper.GetValues(typeof(EkpSchema));

}


}
Currently we are using the following scheme.
The Compiler can be sure that the values passed are really unique since we
assign
static readonly values at object creation time, I think the compiler should
be able
to recognize this pattern.

public class EkpSchema : EnumWrapper
{
public static readonly EkpSchema ImmerLetzter = new EkpSchema(0, "Immer
letzter Einkaufspreis");
public static readonly EkpSchema EkpNeu = new EkpSchema(1, "EKP Neu nach
Abverkauf");
public static readonly EkpSchema ImmerDurchschnitt = new EkpSchema(2, "Immer
durchschnittl. EKP");
public static readonly EkpSchema EkpManuell = new EkpSchema(3, "EKP
manuell");

EkpSchema(int id, string bez)
:base(id,bez)
{
}

public static EkpSchema GetByID(int val)
{
return (EkpSchema)EnumWrapper.GetByID(typeof(EkpSchema), val);
}

public static EkpSchema[] GetValues()
{
return (EkpSchema[])EnumWrapper.GetValues(typeof(EkpSchema));
}

}


这篇关于使用带单例对象的开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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