简体   繁体   English

将动作类型作为参数传递给C#

[英]Passing an action type as a parameter c#

Is there any way to combine the following two methods into one new method by passing the ActionType as a parameter? 通过将ActionType作为参数传递,是否可以将以下两种方法组合为一个新方法? I would like to call the new single method into two other different methods (Withdraw and Deposit). 我想将新的单一方法称为另外两个不同的方法(提现和存款)。

class Account
{

    enum ActionType
    {
        Withdraw,
        Deposit,
    }

    private void WriteDeposit()
    {
        StreamWriter outputFile;
        outputFile = File.AppendText("account.log");

        outputFile.WriteLine("{0},{1}", DateTime.Now, ActionType.Deposit);
        outputFile.Close();
    }

    private void WriteWithdraw()
    {
        StreamWriter outputFile;
        outputFile = File.AppendText("account.log");

        outputFile.WriteLine("{0},{1}", DateTime.Now, ActionType.Withdraw);
        outputFile.Close();
    }

Based on your question the following should work. 根据您的问题,以下应该起作用。

class Account
{

    enum ActionType
    {
        Withdraw,
        Deposit,
    }

    public void Withdraw()
    {
        WriteAction(ActionType.Withdraw);
    }

    private void WriteAction(ActionType action)
    {
        StreamWriter outputFile;
        outputFile = File.AppendText("account.log");

        outputFile.WriteLine("{0},{1}", DateTime.Now, action);
        outputFile.Close();
    }
}

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

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