我有两个构造函数,它们将值提供给只读字段。

public class Sample
{
    public Sample(string theIntAsString)
    {
        int i = int.Parse(theIntAsString);
        _intField = i;
    }

    public Sample(int theInt) => _intField = theInt;
    public int IntProperty    => _intField;

    private readonly int _intField;
}

一个构造函数直接接收值,另一个构造函数进行一些计算并获得值,然后设置字段。

现在问题来了:

我不想复制 设置代码。在这种情况下,只有一个 场地已经设置好了,当然还有可能 要大于1。 要使字段为只读,我需要 从构造函数中设置它们,那么 我不能将共享代码“提取”到 效用函数。 我不知道怎么叫 另一个构造函数。

什么好主意吗?


当前回答

在我的例子中,我有一个主构造函数,它使用OracleDataReader作为参数,但我想使用不同的查询来创建实例:

我有这样的代码:

public Subscriber(OracleDataReader contractReader)
    {
        this.contract = Convert.ToString(contractReader["contract"]);
        this.customerGroup = Convert.ToString(contractReader["customerGroup"]);
        this.subGroup = Convert.ToString(contractReader["customerSubGroup"]);
        this.pricingPlan= Convert.ToString(contractReader["pricingPlan"]);
        this.items = new Dictionary<string, Member>();
        this.status = 0;
        
        
    }

所以我创建了以下构造函数:

public Subscriber(string contract, string customerGroup) : this(getSubReader(contract, customerGroup))
    { }

这个方法是:

 private static  OracleDataReader getSubReader(string contract, string customerGroup)
    { 
        cmdSubscriber.Parameters[":contract"].Value = contract + "%";
        cmdSubscriber.Parameters[":customerGroup"].Value = customerGroup+ "%";
        return  cmdSubscriber.ExecuteReader();
        
    }

注意:静态定义的cmdSubscriber在代码的其他地方定义;在这个示例中,我的主构造函数进行了简化。

其他回答

以防你需要在调用另一个构造函数之前而不是之后运行一些东西。

public class Sample
{
    static int preprocess(string theIntAsString)
    {
        return preprocess(int.Parse(theIntAsString));
    }

    static int preprocess(int theIntNeedRounding)
    {
        return theIntNeedRounding/100;
    }

    public Sample(string theIntAsString)
    {
        _intField = preprocess(theIntAsString)
    }

    public Sample(int theIntNeedRounding)
    {
        _intField = preprocess(theIntNeedRounding)
    }

    public int IntProperty  => _intField;

    private readonly int _intField;
}

如果需要设置多个字段,ValueTuple会非常有用。

拜托,拜托,亲爱的千万不要在家里,或者在工作场所,或者任何地方尝试这个。

这是解决一个非常非常具体的问题的方法,我希望你们不会遇到这种情况。

我发布这篇文章,因为从技术上讲,这是一个答案,也是另一个看待它的角度。

重复一遍,任何情况下都不要使用。代码是与LINQPad运行。

void Main()
{
    (new A(1)).Dump();
    (new B(2, -1)).Dump();
    
    var b2 = new B(2, -1);
    b2.Increment();
    
    b2.Dump();
}

class A 
{
    public readonly int I = 0;
    
    public A(int i)
    {
        I = i;
    }
}

class B: A
{
    public int J;
    public B(int i, int j): base(i)
    {
        J = j;
    }
    
    public B(int i, bool wtf): base(i)
    {
    }
    
    public void Increment()
    {
        int i = I + 1;

        var t = typeof(B).BaseType;
        var ctor = t.GetConstructors().First();
        
        ctor.Invoke(this, new object[] { i });
    }
}

因为构造函数是一个方法,你可以用反射来调用它。现在您可以用传送门来思考,或者想象一罐蠕虫的图片。很抱歉。

是这样的:

public Sample(string str) : this(int.Parse(str)) { }

错误处理和使代码可重用是关键。我将字符串添加到int验证,如果需要,可以添加其他类型。用一个更可重用的解决方案来解决这个问题:

public class Sample
{
    public Sample(object inputToInt)
    {
        _intField = objectToInt(inputToInt);
    }

    public int IntProperty => _intField;

    private readonly int _intField;
}

public static int objectToInt(object inputToInt)
{
    switch (inputToInt)
        {
            case int inputInt:
                return inputInt;
            break;
            case string inputString:
            if (!int.TryParse(inputString, out int parsedInt))
            {
                throw new InvalidParameterException($"The input {inputString} could not be parsed to int");
            }
            return parsedInt;

            default:
                throw new InvalidParameterException($"Constructor do not support {inputToInt.GetType().Name}");
            break;
        }
}

在构造函数体之前,使用以下任意一个:

: base (parameters)

: this (parameters)

例子:

public class People: User
{
   public People (int EmpID) : base (EmpID)
   {
      // Add more statements here.
   }
}