C# 我如何消除只在类型上不同但使用非默认构造函数的方法之间的重复?
您好,我得到了许多看起来几乎完全相同的方法(以下两个示例)。区别在于处理的JSON breanch的名称、返回列表的类型和添加到列表的对象的类型。我知道这些示例方法仍然需要在其主体中进行一些优化,但实际情况是传递返回值的类型和方法当前需要的类的类型,并使其全部工作。如果可能的话,我希望避免使用强制转换来代替调用方法 方法1C# 我如何消除只在类型上不同但使用非默认构造函数的方法之间的重复?,c#,.net,optimization,methods,C#,.net,Optimization,Methods,您好,我得到了许多看起来几乎完全相同的方法(以下两个示例)。区别在于处理的JSON breanch的名称、返回列表的类型和添加到列表的对象的类型。我知道这些示例方法仍然需要在其主体中进行一些优化,但实际情况是传递返回值的类型和方法当前需要的类的类型,并使其全部工作。如果可能的话,我希望避免使用强制转换来代替调用方法 方法1 public static List<Box> JsonToListOfBoxes(string data) { List<
public static List<Box> JsonToListOfBoxes(string data)
{
List<Box> ListOfBoxes = new List<Box>();
if(!string.IsNullOrEmpty(data))
{
JObject productsJson = JObject.Parse(data);
JToken jtkProduct;
jtkProduct = productsJson["boxes"];
if(jtkProduct != null)
if(jtkProduct.HasValues)
{
int childrenCount = productsJson["boxes"].Count();
for(int x = 0;x < childrenCount;x++)
ListOfBoxes.Add(new Box(productsJson["boxes"][x]));
}
}
return ListOfBoxes;
}
公共静态列表JSONTOListofBox(字符串数据)
{
List LISTOFBOKS=新列表();
如果(!string.IsNullOrEmpty(数据))
{
JObject productsJson=JObject.Parse(数据);
jtokenjtkproduct;
jtkProduct=productsJson[“盒子”];
if(jtkProduct!=null)
if(jtkProduct.HasValues)
{
int childrenCount=productsJson[“box”].Count();
对于(int x=0;x
方法2
public static List<Envelope> JsonToListOfEnvelopes(string data)
{
List<Envelope> ListOfEnvelopes = new List<Envelope>();
if(!string.IsNullOrEmpty(data))
{
JObject productsJson = JObject.Parse(data);
JToken jtkProduct;
jtkProduct = productsJson["envelopes"];
if(jtkProduct != null)
if(jtkProduct.HasValues)
{
int childrenCount = productsJson["envelopes"].Count();
for(int x = 0;x < childrenCount;x++)
ListOfEnvelopes.Add(new Envelope(productsJson["envelopes"][x]));
}
}
return ListOfEnvelopes;
}
公共静态列表JSONTOLISTOFEVELOPES(字符串数据)
{
List LISTOFEVELOPES=新列表();
如果(!string.IsNullOrEmpty(数据))
{
JObject productsJson=JObject.Parse(数据);
jtokenjtkproduct;
jtkProduct=productsJson[“信封”];
if(jtkProduct!=null)
if(jtkProduct.HasValues)
{
int childrenCount=productsJson[“信封”].Count();
对于(int x=0;x
您可以使用通用方法,其中dataName
应该是“盒子”或“信封”:
公共静态列表JSONTOListofBox(字符串数据、字符串数据名)
{
List ListOfItems=新列表();
如果(!string.IsNullOrEmpty(数据))
{
JObject productsJson=JObject.Parse(数据);
jtokenjtkproduct;
jtkProduct=productsJson[dataName];
if(jtkProduct!=null)
if(jtkProduct.HasValues)
{
int childrenCount=productsJson[dataName].Count();
对于(int x=0;x
使用示例:
var list1=jsontoListofBox(“数据字符串”、“框”);
var list2=JSONtoListofBox(“数据字符串”、“信封”);
使用泛型,您可以进行如下更改:(无参数化泛型构造函数)
我只是稍微改变了@msmolcic的逻辑
public static List<T> JsonToListOfBoxes<T>(string data)
{
List<T> ListOfItems = new List<T>();
string dataName = typeof(T) == typeof(Box) ? "boxes" : "envelopes";
//if there are many types one can try in below way..
// if (typeof(T) == typeof(Box))
// {
// dataName = "Box";
// }
// else if (typeof(T) == typeof(Envelope))
// {
// dataName = "envelopes";
// }
if (!string.IsNullOrEmpty(data))
{
JObject productsJson = JObject.Parse(data);
JToken jtkProduct;
jtkProduct = productsJson[dataName];
if (jtkProduct != null)
if (jtkProduct.HasValues)
{
int childrenCount = productsJson[dataName].Count();
for (int x = 0; x < childrenCount; x++)
ListOfItems.Add((T)Activator.CreateInstance(typeof(T), productsJson[dataName][x]));
}
}
return ListOfItems;
}
公共静态列表JSONTOListofBox(字符串数据)
{
List ListOfItems=新列表();
字符串dataName=typeof(T)==typeof(Box)?“Box”:“信封”;
//如果有很多种类型,你可以用下面的方法试试。。
//if(类型(T)=类型(方框))
// {
//dataName=“Box”;
// }
//否则如果(类型(T)=类型(信封))
// {
//dataName=“信封”;
// }
如果(!string.IsNullOrEmpty(数据))
{
JObject productsJson=JObject.Parse(数据);
jtokenjtkproduct;
jtkProduct=productsJson[dataName];
if(jtkProduct!=null)
if(jtkProduct.HasValues)
{
int childrenCount=productsJson[dataName].Count();
对于(int x=0;x
@LuisLavieri这对我来说是显而易见的,我被类类型卡住了,返回了类型我没有看到框和信封对象。当然,你必须使用泛型,我认为你得到了一些可行的解决方案,但我认为从API的角度来看,保持公共表面不变,然后将多余的部分提取到一个私有的helper方法中,你可能会得到很好的服务。这允许您将T
限制为您知道自己有能力处理并具有商业意义的类型。new T(productsJson[dataName][x])
不会编译,你不能在泛型中使用构造函数参数。@ScottChamberlain这个怎么样?使用Activator.CreateInstanc
对我来说仍然感觉像是代码味道,所以它不会为你赢得+1,但我会去掉-1,因为代码完成了它的工作。另外Activator.CreateInstance不应该在一个紧密的循环中使用,因为它的速度非常慢。送一辆lambda会更有意义。@Aron这只是我当时脑子里的一个建议,没有必要去恨;)这个问题引用了许多方法,但他举了2个例子。您的解决方案只能烘焙显示的2个变体,不能处理其他变体。@AnthonyPegram我刚刚添加了注释代码段。看看吧。如果用户不想传递额外的参数和有限的类型,此解决方案会有所帮助。
var list1 = JsonToListOfBoxes<Box>("dataString", "boxes");
var list2 = JsonToListOfBoxes<Envelope>("dataString", "envelopes");
public static List<T> JsonToListOfEnvelopes<T>(string data, string searchString, Func<string, T> creator)
{
List<T> ListOfEnvelopes = new List<T>();
if (!string.IsNullOrEmpty(data))
{
JObject productsJson = JObject.Parse(data);
JToken jtkProduct;
jtkProduct = productsJson[searchString];
if (jtkProduct != null)
if (jtkProduct.HasValues)
{
int childrenCount = productsJson[searchString].Count();
for (int x = 0; x < childrenCount; x++)
ListOfEnvelopes.Add(creator(productsJson[searchString][x]));
}
}
return ListOfEnvelopes;
}
var result = JsonToListOfEnvelopes("data", "boxes", c => { return new Box(c); });
var result = JsonToListOfEnvelopes("data", "envelopes", c => { return new Envelope(c); });
public static List<T> JsonToListOfBoxes<T>(string data)
{
List<T> ListOfItems = new List<T>();
string dataName = typeof(T) == typeof(Box) ? "boxes" : "envelopes";
//if there are many types one can try in below way..
// if (typeof(T) == typeof(Box))
// {
// dataName = "Box";
// }
// else if (typeof(T) == typeof(Envelope))
// {
// dataName = "envelopes";
// }
if (!string.IsNullOrEmpty(data))
{
JObject productsJson = JObject.Parse(data);
JToken jtkProduct;
jtkProduct = productsJson[dataName];
if (jtkProduct != null)
if (jtkProduct.HasValues)
{
int childrenCount = productsJson[dataName].Count();
for (int x = 0; x < childrenCount; x++)
ListOfItems.Add((T)Activator.CreateInstance(typeof(T), productsJson[dataName][x]));
}
}
return ListOfItems;
}