避免在C#中重复执行方法

时间:2017-01-20 01:21:51

标签: c# generics design-patterns delegates json.net

我有几个班,其中我需要解析json对象。我看到这个json对象的初始循环在除子方法之外的所有类中都是相同的。

例如,在Class1.cs

private static void FindObject(JToken token)
{
  switch (token.Type)
  {
      case JTokenType.Array:          
         JArray array = token as JArray;
         array.ForEach(a => FindObject(a));
         break;           
      case JTokenType.String:
         token.Replace(GetNewImgTag(token.ToString()));
         break;
      case JTokenType.Object:
         token.Children().ForEach(t => FindObject(t));
         break;
       case JTokenType.Property:
         JProperty prop = token as JProperty;

         if (prop.Value.Type == JTokenType.Array)
         {
             FindObject(prop.Value);
             return;
         }

         prop.Value = GetNewImgTag(prop.Value.ToString());
         break;
      default:
         throw new NotImplementedException(token.Type + " is not defined");
  }    
}

private static JToken GetNewImgTag(string text)
{
   ...
}

和2.cs类是

private static void FindObject(JToken token)
{
  switch (token.Type)
  {
      case JTokenType.Array:          
         JArray array = token as JArray;
         array.ForEach(a => FindObject(a));
         break;           
      case JTokenType.String:
         token.Replace(ReplaceLinks(token.ToString()));
         break;
      case JTokenType.Object:
         token.Children().ForEach(t => FindObject(t));
         break;
       case JTokenType.Property:
         JProperty prop = token as JProperty;

         if (prop.Value.Type == JTokenType.Array)
         {
             FindObject(prop.Value);
             return;
         }

         prop.Value = ReplaceLinks(prop.Value.ToString());
         break;
      default:
         throw new NotImplementedException(token.Type + " is not defined");
  }    
}

private static JToken ReplaceLinks(string text)
{
   ...
}

如果比较两个类,除了子方法调用之外,FindObject()几乎相同。我需要在几个类中实现它。我试图避免这种多重复制方法的创建。

有人能建议更好的方法来设计吗?

我在这里看到了类似的帖子,但我无法将此代表应用于我的方案。

Avoiding repetitive code in multiple similar methods (C#)

1 个答案:

答案 0 :(得分:2)

一种直截了当的方法是识别不同的部分,并使你传递给一个单独的函数的委托。

这是一个有效的例子。

space

现在,在第1课:

public static class MyTokenReaderUtilities
{
     public static void ConvertEachProperty(JToken token, Func<string, JToken> convertString)
     {
        switch (token.Type)
        {
            case JTokenType.Array:          
               JArray array = token as JArray;
              array.ForEach(a => ConvertEachProperty(a, convertString));
              break;           
           case JTokenType.String:
             token.Replace(convertString(token.ToString()));
           break;
          case JTokenType.Object:
              token.Children().ForEach(t => ConvertEachProperty(t, convertString));
              break;
          case JTokenType.Property:
              JProperty prop = token as JProperty;

              if (prop.Value.Type == JTokenType.Array)
              {
                ConvertEachProperty(prop.Value, convertString);
                return;
              }
              prop.Value = convertString(prop.Value.ToString());
              break;
          default:
             throw new NotImplementedException(token.Type + " is not defined");
       }  
     }
}

在第2课:

private static void FindObject(JToken token)
{
   MyTokenReaderUtilities.ConvertEachProperty(token, GetNewImgTag);
}
相关问题