Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将两个方法重构为具有相同字段但名称不同的方法_C#_.net_Refactoring - Fatal编程技术网

C# 将两个方法重构为具有相同字段但名称不同的方法

C# 将两个方法重构为具有相同字段但名称不同的方法,c#,.net,refactoring,C#,.net,Refactoring,我有两个事件处理程序,除了处理不同的事件外,它们的代码完全相同。我怎样才能在不必重复自己的情况下,将这些代码组合到同一个代码中呢 static void SharePointEventHandler(object sender, SharePointEventArgs e) { switch (e.ExceptionType) { case SharePointEventArgs.ExceptionLevel.Debug: CLog(Log

我有两个事件处理程序,除了处理不同的事件外,它们的代码完全相同。我怎样才能在不必重复自己的情况下,将这些代码组合到同一个代码中呢

static void SharePointEventHandler(object sender, SharePointEventArgs e)
{
    switch (e.ExceptionType)
    {
        case SharePointEventArgs.ExceptionLevel.Debug:
            CLog(LogType.Debug, e.Message);
            break;

        case SharePointEventArgs.ExceptionLevel.Info:
            CLog(LogType.Info, e.Message);
            break;

        case SharePointEventArgs.ExceptionLevel.Error:
            CLog(LogType.Error, e.Message, e.Exception);
            break;
    }
}

static void FTPEventHandler(object sender, FTPEventArgs e)
{
    switch (e.ExceptionType)
    {
        case FTPEventArgs.ExceptionLevel.Debug:
            CLog(LogType.Debug, e.Message);
            break;

        case FTPEventArgs.ExceptionLevel.Info:
            CLog(LogType.Info, e.Message);
            break;

        case FTPEventArgs.ExceptionLevel.Error:
            CLog(LogType.Error, e.Message, e.Exception);
            break;
    }
}

根据代码的当前状态,有多种方法可以重构它。我假设目前这两个
EventArgs
子类根本不相关。我想最好的办法就是改变这种状况。具体而言,创建一个基类,现有的两个子类都从该基类派生:

class ExceptionEventArgs : EventArgs
{
    public enum ExceptionLevel
    {
        Debug,
        Info,
        Error
    }

    public ExceptionLevel ExceptionType { get; set; }
    public string Message { get; set; }
    public Exception Exception { get; set; }
}

class SharePointEventArgs : ExceptionEventArgs { ... }
class FTPEventArgs : ExceptionEventArgs { ... }
然后,您可以对两个事件使用相同的事件处理程序:

static void SharePointEventHandler(object sender, ExceptionEventArgs e)
{
    switch (e.ExceptionType)
    {
        case ExceptionEventArgs.ExceptionLevel.Debug:
            CLog(LogType.Debug, e.Message);
            break;

        case ExceptionEventArgs.ExceptionLevel.Info:
            CLog(LogType.Info, e.Message);
            break;

        case ExceptionEventArgs.ExceptionLevel.Error:
            CLog(LogType.Error, e.Message, e.Exception);
            break;
    }
}

这利用了对.NET委托类型的差异支持。委托实例可以将任何方法作为目标,该方法接收为委托类型指定的确切类型的参数,或者可以从为委托类型指定的参数中分配哪些参数。

根据代码的当前状态,有多种方法可以重构此方法。我假设目前这两个
EventArgs
子类根本不相关。我想最好的办法就是改变这种状况。具体而言,创建一个基类,现有的两个子类都从该基类派生:

class ExceptionEventArgs : EventArgs
{
    public enum ExceptionLevel
    {
        Debug,
        Info,
        Error
    }

    public ExceptionLevel ExceptionType { get; set; }
    public string Message { get; set; }
    public Exception Exception { get; set; }
}

class SharePointEventArgs : ExceptionEventArgs { ... }
class FTPEventArgs : ExceptionEventArgs { ... }
然后,您可以对两个事件使用相同的事件处理程序:

static void SharePointEventHandler(object sender, ExceptionEventArgs e)
{
    switch (e.ExceptionType)
    {
        case ExceptionEventArgs.ExceptionLevel.Debug:
            CLog(LogType.Debug, e.Message);
            break;

        case ExceptionEventArgs.ExceptionLevel.Info:
            CLog(LogType.Info, e.Message);
            break;

        case ExceptionEventArgs.ExceptionLevel.Error:
            CLog(LogType.Error, e.Message, e.Exception);
            break;
    }
}

这利用了对.NET委托类型的差异支持。委托实例可以将任何方法作为目标,该方法接收为委托类型指定的确切类型的参数,或者可以从为委托类型指定的参数中分配哪些参数。

根据代码的当前状态,有多种方法可以重构此方法。我假设目前这两个
EventArgs
子类根本不相关。我想最好的办法就是改变这种状况。具体而言,创建一个基类,现有的两个子类都从该基类派生:

class ExceptionEventArgs : EventArgs
{
    public enum ExceptionLevel
    {
        Debug,
        Info,
        Error
    }

    public ExceptionLevel ExceptionType { get; set; }
    public string Message { get; set; }
    public Exception Exception { get; set; }
}

class SharePointEventArgs : ExceptionEventArgs { ... }
class FTPEventArgs : ExceptionEventArgs { ... }
然后,您可以对两个事件使用相同的事件处理程序:

static void SharePointEventHandler(object sender, ExceptionEventArgs e)
{
    switch (e.ExceptionType)
    {
        case ExceptionEventArgs.ExceptionLevel.Debug:
            CLog(LogType.Debug, e.Message);
            break;

        case ExceptionEventArgs.ExceptionLevel.Info:
            CLog(LogType.Info, e.Message);
            break;

        case ExceptionEventArgs.ExceptionLevel.Error:
            CLog(LogType.Error, e.Message, e.Exception);
            break;
    }
}

这利用了对.NET委托类型的差异支持。委托实例可以将任何方法作为目标,该方法接收为委托类型指定的确切类型的参数,或者可以从为委托类型指定的参数中分配哪些参数。

根据代码的当前状态,有多种方法可以重构此方法。我假设目前这两个
EventArgs
子类根本不相关。我想最好的办法就是改变这种状况。具体而言,创建一个基类,现有的两个子类都从该基类派生:

class ExceptionEventArgs : EventArgs
{
    public enum ExceptionLevel
    {
        Debug,
        Info,
        Error
    }

    public ExceptionLevel ExceptionType { get; set; }
    public string Message { get; set; }
    public Exception Exception { get; set; }
}

class SharePointEventArgs : ExceptionEventArgs { ... }
class FTPEventArgs : ExceptionEventArgs { ... }
然后,您可以对两个事件使用相同的事件处理程序:

static void SharePointEventHandler(object sender, ExceptionEventArgs e)
{
    switch (e.ExceptionType)
    {
        case ExceptionEventArgs.ExceptionLevel.Debug:
            CLog(LogType.Debug, e.Message);
            break;

        case ExceptionEventArgs.ExceptionLevel.Info:
            CLog(LogType.Info, e.Message);
            break;

        case ExceptionEventArgs.ExceptionLevel.Error:
            CLog(LogType.Error, e.Message, e.Exception);
            break;
    }
}

这利用了对.NET委托类型的差异支持。委托实例可以将任何方法作为目标,该方法接收为委托类型指定的确切类型的参数,或者从为委托类型指定的参数中可以分配哪些参数。

这些是自定义事件参数类型吗?你能更改事件签名吗?正如布拉德利所问的那样,
SharePointEventArgs
FTPEventArgs
之间的关系是什么。有什么关系吗?这里最大的问题是方法体实际上并不完全相同。每个类都使用一个特定于
EventArgs
子类的
enum
。如果这两个值以某种方式共享(例如,
enum
是在
EventArgs
子类之外声明的),就更容易了。如果两个eventArg类型之间没有关系,一个简单的方法是使用共享逻辑创建第三个方法,并且两个现有方法都有一行调用它。但这不是您要求的唯一方法解决方案。这些是自定义事件Args类型吗?你能更改事件签名吗?正如布拉德利所问的那样,
SharePointEventArgs
FTPEventArgs
之间的关系是什么。有什么关系吗?这里最大的问题是方法体实际上并不完全相同。每个类都使用一个特定于
EventArgs
子类的
enum
。如果这两个值以某种方式共享(例如,
enum
是在
EventArgs
子类之外声明的),就更容易了。如果两个eventArg类型之间没有关系,一个简单的方法是使用共享逻辑创建第三个方法,并且两个现有方法都有一行调用它。但这不是您要求的唯一方法解决方案。这些是自定义事件Args类型吗?你能更改事件签名吗?正如布拉德利所问的那样,
SharePointEventArgs
FTPEventArgs
之间的关系是什么。有什么关系吗?这里最大的问题是方法体实际上并不完全相同。每个类都使用一个特定于
EventArgs
子类的
enum
。如果这两个值以某种方式共享(例如,
enum
是在
EventArgs
子类之外声明的),就更容易了。如果两个eventArg类型之间没有关系,一个简单的方法是使用共享逻辑创建第三个方法,并且两个现有方法都有一行调用它。但这不是您要求的唯一方法解决方案。这些是自定义事件Args类型吗?你能更改事件签名吗?正如布拉德利所问的那样,
SharePointEventArgs
FTPEventArgs
之间的关系是什么。有什么关系吗?这里最大的问题是方法体实际上并不完全相同。每个类都使用一个特定于
EventArgs
子类的
enum
。如果这两个值以某种方式共享(例如,
enum
是在t之外声明的),则会更容易