【发布时间】:2016-07-08 20:10:18
【问题描述】:
有这个类:
public static class Command
{
public const string SET_STB_MEDIA_CTRL = "SET STB MEDIA CTRL ";
public static string ECHO = "ECHO";
public static string SET_CHANNEL = "SET CHANNEL ";
public static string GET_VOLUMN = "GET VOLUMN";
public static string GET_MAX_VOLUMN = "GET MAX VOLUMN ";
public string SET_STB_MEDIA_LIST = "SET STB MEDIA LIST ";
}
然后:
public static class MultimediaConstants
{
public const string VIDEO = "video";
public const string AUDIO = "audio";
public const string PHOTO = "photo";
public const string ALL = "all";
public const string BACKGROUND_MUSIC = "background_music";
public const string TV = "tv";
public const string ACTION_PLAY = "play";
}
重点是,我想要这样的东西:
public static string SET_STB_MEDIA_CTRL (MultimediaConstants type, MultimediaConstants action)
{
return Command.SET_STB_MEDIA_CTRL + "type:" + type + "action:" + action;
}
所以这个方法的结果应该是:
SET STB MEDIA CTRL type:tv action:play
方法的调用将是:
SET_STB_MEDIA_CTRL (MultimediaConstants.TV, MultimediaConstants.ACTION_PLAY);
【问题讨论】:
-
你不能要求静态类的实例作为方法参数,因为你不能创建静态类的实例
-
这些不是枚举。这些是类。您可以使用
enum关键字而不是类来创建枚举。然后你可以使用你想要的值。 -
@Sehnsucht 这就是为什么他想要
Enum of strings,就像 java 让你做的那样 -
@SamIam 我明白了;我只是解释了为什么这是不可能的
-
@RichardBarker 它们在概念上是枚举。在一般 CS 意义上,这是对该术语的适当使用。然而,C# 的枚举实现无法做到这一点。这使得这些不是 C# 枚举,但在一般意义上称它们为枚举仍然适用。