c# - 将对象转换为它的类

标签 c# class variables casting

我正在尝试创建一些带有两个变量的类。其中一个变量是“名称”,另一个是“值”。对于每个类值可以是不同类型的变量(int、double 或 string)。

我想将这些类的实例存储在列表中,因此我将这些类放置在抽象类下。

然后在 foreach 循环中,我想使用这些实例的值,但我需要将它们转换为原始类型,以便 param.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;
            }
        }
    }
}

还有更好的选择吗?任何建议都会有很大帮助。 谢谢。

最佳答案

我更喜欢使用接口(interface)来实现这一点,但你可以这样做:

// ...
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/case 或 if/else 链的情况下工作。

但是,请注意,使用此模式的冲动可能暗示了潜在的设计问题。这就是它有时被视为“代码味道”的原因。

关于c# - 将对象转换为它的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72257480/

相关文章:

javascript - var t={}; 的含义变量等于花括号

javascript - AngularJS:没有 $scope 的 ng-repeat 中的变量在其本地范围内?

c# - 如何使用try-catch获取代码的错误行号

c# - 错误代码 :XamlC error XFC0000 : Cannot resolve type "MediaElement"

java - 在 Java 中声明 "static"变量时 "global"到底是什么意思?

c++ - 如何使用 C++ 和 SFML 创建自己的类

c# - HttpClient 和套接字耗尽 - 澄清?

c# - 从电子邮件地址获取域名

PHP:如何使用另一个类中的参数实例化一个类

javascript - 是否可以在javascript函数中,从具有Google map 中位置的纬度和经度的var创建具有位置名称的var?