我试图创建一些带有两个变量的类.其中一个变量是Name,另一个变量是Value.对于每个类,值可以是不同类型的变量(int、double或string).

我想把这些类的实例存储在一个列表中,所以我把这些类放在一个抽象类下.

然后在foreach循环中,我想使用这些实例的值,但我需要将它们转换为原始类型,以便参数.Set函数将接受它.

我的代码是这样的:

List<ElementProperty> parameters = new List<ElementProperty>();
//I add my parameters to the list.
parameters.Add(new ElementProperty.String("TestName", "TestVariable"));
parameters.Add(new ElementProperty.Integer("TestName", 10));

//I want to make this foreach loop shorter and more proper
foreach (var parameter in parameters)
{
    Parameter param = el.LookupParameter(parameter.Name);
    if (parameter is ElementProperty.Boolean)
    {
        param.Set(((ElementProperty.Boolean)parameter).Value);
        //param.Set only accepts int double and string
    }
    else if (parameter is ElementProperty.Double)
    {
        param.Set(((ElementProperty.Double)parameter).Value);
    }
    else if (parameter is ElementProperty.Integer)
    {
        param.Set(((ElementProperty.Integer)parameter).Value);
    }
    else if (parameter is ElementProperty.String)
    {
        param.Set(((ElementProperty.String)parameter).Value);
    }
}

public abstract class ElementProperty
{
    public string Name;
    public object Value;

    public class Integer : ElementProperty
    {
        public new int Value;
        public Integer(string Name, int Value)
        {
            this.Name = Name;
            this.Value = Value;
        }
    }

    public class Double : ElementProperty
    {
        public new double Value;
        public Double(string Name, double Value)
        {
            this.Name = Name;
            this.Value = Value;
        }
    }

    public class String : ElementProperty
    {
        public new string Value;
        public String(string Name, string Value)
        {
            this.Name = Name;
            this.Value = Value;
        }
    }

    public class Boolean : ElementProperty
    {
        public new int Value;
        public Boolean(string Name, bool Value)
        {
            this.Name = Name;

            if (Value is false)
            {
                this.Value = 0;
            }
            else
            {
                this.Value = 1;
            }
        }
    }
}

有更好的 Select 吗?任何建议都会有很大帮助.

推荐答案

我更喜欢使用接口,但你可以这样做:

// ...
foreach (var parameter in parameters)
{
    parameter.SetTo(param); // Call same interface
}
// ...

在每一个具体类别中:

public class Integer : ElementProperty
{
    public new int Value;
    public Integer(string Name, int Value)
    {
        this.Name = Name;
        this.Value = Value;
    }
    public void SetTo(Parameter p)
    {
         p.Set(this.Value); // calls correct overload
    }
}

这在没有switch /shell 或if/else链的情况下完全有效.

However,请注意,使用这种模式的冲动may be暗示了潜在的设计问题.这就是为什么它有时被视为"代码气味".

Csharp相关问答推荐

C#中的包版本控制

为什么使用DXGI输出复制和Direct 3D时捕获的图像数据全为零?

Regex在c#中完全匹配

当打印一行x个项目时,如何打印最后一行项目?

为什么我不能更改尚未设置的模拟对象属性的值?

如果属性名为xyz,我需要使用System.Text.Json修改字符串类型的值""<>

在C#中,有没有一种方法可以集中定义跨多个方法使用的XML参数描述符?

无法创建';';类型的';DbContext';.异常';无法解析类型';Microsoft.EntityFrameworkCore.DbContextOptions`1[Comm的服务

如何从ASP.NET核心MVC视图和Blazor传递数据

在实体框架中处理通用实体&S变更跟踪器

当使用Dapper映射DBNull时,我可以抛出异常吗?

如何使用用于VS代码的.NET Maui扩展在我的iOS/Android设备或模拟器上进行调试?

当空判断结果赋给变量时,为什么会出现可能空异常警告的解引用?

如何返回具有泛型的类?

单元测试类型为HttpClient with Microsoft.Extensions.Http.Resilience

数据库操作预计影响1行,但实际影响0行; after _dbContext.SaveChanges();

为什么连接到Google OAuth2后,结果.Credential为空?

使用ITfoxtec.Identity.Saml2解析相同键多值SAML 2声明

将文本从剪贴板粘贴到RichTextBox时,新文本不会在RichTextBox ForeColor中着色

分别切换用于读取和写入的EF核心日志(log)