简体   繁体   English

避免使用开关盒

[英]Avoiding use of switch case

I've a following method "Test" which accepts a params list: 我有以下方法“ Test”,它接受一个参数列表:

Public void method Test(params double[] list]
{
 Vols vol= new Vols();
//Fill the object based on the input list and
//pass this vol to other private method for further processing
}

Inside this method, am using a custom business object called Vols defined as follows: 在此方法内部,我使用一个名为Vols的自定义业务对象,定义如下:

public class Vols
{
    private double _vol09;
    private double _vol05;
    private double _vol01;

    public Vols()
    {
    }


    public double Vol09
    {
        get { return _vol09; }
        set { _vol09 = value; }
    }


    public double Vol01
    {
        get { return _vol01; }
        set { _vol01 = value; }
    }


    public double Vol05
    {
        get { return _vol05; }
        set { _vol05 = value; }
    }
} 

The user of method "test" can pass in values as: test(0.1,0.9); 方法“ test”的用户可以将值传递为:test(0.1,0.9);

So, depending on the input passed, I want to set only the corresponding property in the business object "Vols"....ie. 因此,根据传递的输入,我只想在业务对象“ Vols”中设置相应的属性。 in this case , properties Vol01 and Vol09 would be set in the method "test". 在这种情况下,将在方法“ test”中设置属性Vol01和Vol09。 Is there any way to do this so that I can avoid a switch case inside the method? 有什么办法可以避免在方法内部出现开关情况?

This would be possible using reflection...but since reflection is expensive, is there any other approach I can make use of?Also, shall I use switch-case statement or reflection here wrt good coding practices? 使用反射是可能的...但是由于反射很昂贵,还有其他方法可以使用吗?此外,在这种情况下,我应该使用switch-case语句还是反射吗?

Thanks. 谢谢。

It would be more readable if you used an object initializer... 如果使用对象初始化程序,它将更具可读性...

var test = new Test() { Vol01 = 0.1, Vol09 = ... }

Your constructor can construct the default business object and your property setters can call the corresponding property on Vols. 您的构造函数可以构造默认业务对象,而属性设置者可以调用Vols上的相应属性。

It also might make sense to use a fluent interface which is always a good choice when there is complex, variable, constructor setup... 使用流利的接口也很有意义,当存在复杂的,可变的,构造函数的设置时,这总是一个不错的选择。

var test = new Test().WithVol01(0.1).AndIntentRevealingName();

or, just inject the business object... 或者,只需注入业务对象...

var test = new Test(new Vols(...setup how I want it tested...));

And in C# 4.0, you can use named parameters, all the cool kids are doing it... 在C#4.0中,您可以使用命名参数,所有很酷的孩子都在做...

var test = new Test(vol01: 0.1, ...);

Reflection is overkill, and I wouldn't be worrying about performance. 反思是矫kill过正,我不会担心性能。 I would be worrying if my test cases clearly revealed intent. 如果我的测试用例清楚地表明了我的意图,我将感到担心。

应该可以解决使用反射

You can't switch on a variable of type double in C#, and I would avoid reflection if possible. 您无法在C#中打开double类型的变量,如果可能的话,我会避免反射。

Your code doesn't make it clear what value you are actually going to store in the VolXX properties. 您的代码并不清楚在VolXX属性中实际要存储什么值。 They are of type double, would the Vol09 property only ever store the value 0.9? 它们是double类型的,Vol09属性是否只能存储值0.9?

If possible I'd rework the Vols class. 如果可能的话,我会重做Vols类。 Here's one idea... 这是一个主意...

public class Vols
{ 
    private List<double> _vols = new List<double>();

    public void AddVolume( double volume )
    {
        _vols.Add( volume );
    }

    public void GetVolume( int index )
    {
        return _vols.ElementAtOrDefault( index );
    }
}

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

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