首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这是构建这个类和方法的常用方法吗?

这是构建这个类和方法的常用方法吗?
EN

Code Review用户
提问于 2012-12-07 13:29:59
回答 1查看 133关注 0票数 3

我是一个新开发人员,所以我有很多问题:

在这个类中,我获得磁盘上的文件路径(需要转换为pcap扩展名的wireshark文件),并使用方法convertFileToPcap()、调用convertFileToPcap()的构造函数和创建Editcap对象后的主函数将其转换为文件路径(pcap ),属性getNewFileName()返回_newFileName (类成员),我想知道是否有更好的方法或适当的方法来实现它。

代码语言:javascript
复制
public class Editcap
{
    #region members
    private string _editpcap;
    private ProcessStartInfo _editpcapProcess;
    private FileInfo _fileInfo;
    private string _newFileName;
    #endregion

    #region c'tor
    public Editcap(FileInfo fileinfo)
    {
        _fileInfo = fileinfo;
        _newFileName = "";
        convertFileToPcap();
    }
    #endregion

    public void convertFileToPcap()
    {
        string oldFileExtension = _fileInfo.Extension;
        _newFileName = _fileInfo.FullName.Replace(oldFileExtension, "_new") + ".pcap";
        _editpcapProcess = new ProcessStartInfo(string.Format("\"{0}\"", _editpcap));
        _editpcapProcess.Arguments = (string.Format("{2}{0}{2} -F libpcap {2}{1}{2}", _fileInfo.FullName, _newFileName, "\""));
        _editpcapProcess.WindowStyle = ProcessWindowStyle.Hidden;
        _editpcapProcess.RedirectStandardOutput = true;
        _editpcapProcess.RedirectStandardError = true;
        _editpcapProcess.CreateNoWindow = true;
        _editpcapProcess.UseShellExecute = false;
        _editpcapProcess.ErrorDialog = false;
        Process capinfosProcess = Process.Start(_editpcapProcess);
        capinfosProcess.WaitForExit();
    }

    public string getNewFileName()
    {
        return _newFileName;
    }
}
EN

回答 1

Code Review用户

发布于 2012-12-07 15:07:32

你好,欢迎来到代码评论!

  • 首先,我想向您介绍微软官方的命名指南 C#开发。您的代码违背了其中的一些,如果这是您公司或团队的指导方针,那就太棒了!如果你没有正式的风格,那就从我的链接开始吧。
  • 我肯定不会从构造函数中调用转换方法。这可能引发会阻止创建对象的异常。将处理方法分开,让调用者调用它。毕竟是public
  • 在某些情况下,您的成员变量似乎也有点全局。在多个方法中只使用_fileInfo_newFileName。让其他人保持本土化的方法。
    • 作为其中的一部分,请注意,_editpcap从来不被分配,并且始终是null。我不认为这是你想要的。

  • getNewFileName方法看起来非常像Java,因为Java没有属性的概念。这是被授予财产的主要候选人。
  • Process类是IDisposable,因此将它封装在using块中是惯用的。
  • 现在我只是开始稍微吹毛求疵,构造函数中设置的字段在构造后是不变的,所以通过使用readonly关键字来说明它的意图。此外,您还可以使用现代的C#设施作为对象初始化器和var关键字。

尽管如此,以下是我提议的代码重构:

代码语言:javascript
复制
public class Editcap
{
    #region members
    private readonly FileInfo fileInfo;
    private string newFileName = string.Empty;
    #endregion

    #region c'tor
    public Editcap(FileInfo fileinfo)
    {
        if (fileinfo == null)
        {
            throw new ArgumentNullException("fileInfo");
        }

        this.fileInfo = fileinfo;
    }
    #endregion

    public void ConvertFileToPcap()
    {
        this.newFileName = this.fileInfo.FullName.Replace(this.fileInfo.Extension, "_new") + ".pcap";

        string editpcap = null; // still not set, need to fix!
        var editpcapProcess = new ProcessStartInfo(string.Format("\"{0}\"", editpcap))
        {
            Arguments = string.Format("{2}{0}{2} -F libpcap {2}{1}{2}", this.fileInfo.FullName, this.newFileName, "\""),
            WindowStyle = ProcessWindowStyle.Hidden,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true,
            UseShellExecute = false,
            ErrorDialog = false
        };

        using (var capinfosProcess = Process.Start(editpcapProcess))
        {
            capinfosProcess.WaitForExit();
        }
    }

    public string NewFileName
    {
        get
        {
            return this.newFileName;
        }
    }
}
票数 6
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/19385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档