简体   繁体   English

如何创建具有变量值的参数类

[英]How to create a parameter class with variant value

I want to create a params collection. 我想创建一个params集合。 Create a collection with generics is very simple: 使用泛型创建集合非常简单:

List<Param> Params = new List<Param>();

And I can simply add params like this: 我可以简单地添加如下参数:

List<Param> Params = new List<Param>() {
    new Param() { Label = "Param 1", Type = Param.ParamType.Text },
    new Param() { Label = "Param 2", Type = Param.ParamType.Select }
}

Now, how do I add a typed value property for each param? 现在,如何为每个参数添加类型化的value属性?

Like: 喜欢:

  • a string for the text 文字字符串
  • an options list for the select 选择的选项列表
  • a date, a boolean… 日期,布尔值…

I think there are a better solution like that: 我认为有这样一个更好的解决方案:

new Param() { Label = "Param 1", Type = Param.ParamType.Text, StringValue = "text" },
new Param() { Label = "Param 1", Type = Param.ParamType.Text, StringValue = "text" },
new Param() { Label = "Param 1", Type = Param.ParamType.CheckBox, BoolValue = true }

I'd recommend making a generic subclass: 我建议制作一个通用子类:

public Param<T> : Param
{
    public T Value { get; set; }
}

And then create them like this: 然后像这样创建它们:

new Param<string>() { Label = "Param 1", Type = Param.ParamType.Text, Value = "text" },
new Param<string>() { Label = "Param 1", Type = Param.ParamType.Text, Value = "text" },
new Param<bool>() { Label = "Param 1", Type = Param.ParamType.CheckBox, Value = true }

You can take advantage of type inference to create a more convenient static method on Param : 您可以利用类型推断在Param上创建一个更方便的静态方法:

public class Param 
{
    ...
    public static Param<T> From<T>(string label, ParamType type, T value)
    {
        return new Param<T>() 
        {
            Label = label, 
            Type = type, 
            Value = value 
        }
    }
}

And then use it like this: 然后像这样使用它:

Param.From("Param 1", Param.ParamType.Text, "text"),
Param.From("Param 1", Param.ParamType.Text, "text"),
Param.From("Param 1", Param.ParamType.CheckBox, true)

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

相关问题 是否可以创建变量值类型? - Is it possible to create a variant value type? 如何创建一个将泛型类作为参数的方法 - How to create a method that takes a generic class as a parameter 根据应用程序生命周期为每个构造函数参数值创建一个类的实例 - Create an instance of a class per constructor parameter value with application lifetime 如何为SQL CLR存储过程提供sql_variant参数? - How to have a sql_variant parameter for a SQL CLR stored procedure? 如何从类型参数创建类实例? - How can I create class instance from type parameter? 如何创建自定义结构(或类)类型的应用程序设置参数? - How to create an application settings parameter of type a custom struct (or class)? 如何创建不具有依赖项注入的构造函数参数的类 - How to create a class without constructor parameter which has dependency injection 如何在 C# 中发送参数类并从中创建新对象 - How to send parameter class and create new object from it in C# 如何使用已知类型作为参数使用泛型创建泛型类? - How to create a generic class with generic using a known type as parameter? 如何使用实现类的类型的参数创建抽象方法 - How to create an abstract method with a parameter of type of implementing class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM