简体   繁体   English

具有枚举类型的泛型类型

[英]Generic type with enum type

I'm writing code for parameter class that being used by some kind of commands. 我正在为某种命令正在使用的参数类编写代码。 This command has two main Issues: 此命令有两个主要问题:

  • OptionalValues - This is the optional values to be selected. OptionalValues-这是要选择的可选值。
  • SelectedValue - The user choice. SelectedValue-用户选择。

The user input is: select one member from enum. 用户输入是:从枚举中选择一个成员。

The code: 编码:

public enum eUserChoice
{
    Choice1,
    Choice2,
    Choice3
};

public class Parameter<ENUM_TYPE>
{
    public ENUM_TYPE OptionalValues {get;} // WRONG USING
    public ENUM_TYPE Value {get; set;}
}

Requested behavior of the code: 请求的代码行为:

Parameter<eUserChoice> myParam = new Parameter<eUserChoice>()
myParam.Value = myParam.OptionalValues.Choice1;
// OR:
// myParam.Value = myParam.OptionalValues.Choice1 | myParam.OptionalValues.Choice2;

When I'm writing "myParam.OptionalValues." 当我写“ myParam.OptionalValues”时。 I'm want that this will be equal to write "eUserChoice." 我希望这等于写“ eUserChoice”。 ( intellisense is important). 智能感知很重要)。

I'm not sure if it's possible... What do you think? 我不确定是否可能...您如何看待?

Generics are not (that) magic. 泛型不是魔术。 With the following line 与以下行

public ENUM_TYPE OptionalValues {get;}

You are creating a property that can have a value of type ENUM_TYPE. 您正在创建一个属性,该属性的值可以为ENUM_TYPE类型。 However what you want is to let the OptionalValues property link to the type itself. 但是,您要让OptionalValues属性链接到类型本身。 This is impossible. 这是不可能的。

Generics simply allow you to create some sort of class template (c++ people: not that kind of template) which generates a new class for each type of ENUM_TYPE you supply. 泛型只是让你建立某种形式的类模板(C ++的人:不是那种模板)生成一个新的类为您提供各类型ENUM_TYPE的。 And nothing more. 仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM