C# 避免C语言中重复方法的实现#
我有几个类需要解析json对象。我看到这个json对象的初始循环在除子方法之外的所有类中几乎相同 例如,在Class1.cs中C# 避免C语言中重复方法的实现#,c#,generics,design-patterns,delegates,json.net,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 => Fi
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()除了子方法调用外几乎相同。我需要在几个类中实现这一点。我试图避免创建这种多重重复的方法
有人能提出更好的设计方法吗
我在这里看到了类似的帖子,但我无法将此代理应用到我的场景中
一种简单的方法是识别不同的部分,并将其作为委托传递到单独的函数中 这里有一个有效的例子
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");
}
}
}
第二类:
private static void FindObject(JToken token)
{
MyTokenReaderUtilities.ConvertEachProperty(token, ReplaceLinks);
}
一种简单的方法是识别不同的部分,并将其作为委托传递到单独的函数中 这里有一个有效的例子
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");
}
}
}
第二类:
private static void FindObject(JToken token)
{
MyTokenReaderUtilities.ConvertEachProperty(token, ReplaceLinks);
}
为什么您不能应用该委托。方法将更改为
FindObject(JToken令牌,Func getValue)
,并应用prop.Value=getValue(prop.Value.ToString())
对于案例JTokenType.Property:
,为什么不检查该值是否为字符串?它可以是整数、嵌套对象、日期或其他。为什么不能应用委托。方法将更改为FindObject(JToken令牌,Func getValue)
,并应用prop.Value=getValue(prop.Value.ToString())
对于案例JTokenType.Property:
,为什么不检查该值是否为字符串?它可以是一个整数或嵌套对象或日期或其他任何东西。看起来像重构主题,因此我建议Martin Fowlerlooks像重构主题,因此我建议Martin Fowler