如何重载泛型 IEnumerable
How to overload generic IEnumerable
我知道这是尝试重载单个泛型和数组泛型的常见问题。所有这些答案都在谈论为什么它以这种方式工作以及如何有更好的方法,但是 none 显示了这一点。我正在寻找有关如何完成我所追求的目标的建议,我愿意接受重构并以不同的方式进行重构
这是代表问题的代码:
https://dotnetfiddle.net/sWrNj3
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
public static class MyParser
{
public static void Parse<T>(string name, T value)
{
Console.WriteLine($"{name}: single");
}
// Must take in IEnumerable<T>
public static void Parse<T>(string name, IEnumerable<T> collection)
{
Console.WriteLine($"{name}: IEnumerable");
}
public static void ParseObj<T>(T data)
{
foreach (var prop in data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
Parse(prop.Name, prop.GetValue(data, null));
}
}
}
public class Program
{
public static void Main()
{
MyParser.ParseObj(new
{
Str = "abc",
Num = 543,
Arr = new[]{1, 2, 3},
Chars = new char[]{'x', 'y', 'z'}}
);
}
}
结果:
Str: single
Num: single
Arr: single
Chars: single
期望:
Str: single
Num: single
Arr: IEnumerable
Chars: IEnumerable
我认为您拥有所需的所有信息,但您不能将所有工作委托给编译器,您必须自己完成工作:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using static System.Console;
public static class MyParser
{
public static void ParseObj<T>(T data)
{
foreach (var prop in data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if(prop.PropertyType.IsArray) WriteLine($"{prop.Name}:array");
else if(prop.PropertyType == (typeof(string))) WriteLine($"{prop.Name}:string");
else if(prop.PropertyType.IsValueType) WriteLine($"{prop.Name}:value type");
else if(typeof(IEnumerable).IsAssignableFrom(prop.PropertyType)) WriteLine($"{prop.Name}:IEnumerable");
else if(prop.PropertyType.IsEnum) WriteLine($"{prop.Name}:enum");
else if(prop.PropertyType.IsClass) WriteLine($"{prop.Name}:class");
else WriteLine($"{prop.Name}:something else");
}
}
}
public class Program
{
public static void Main()
{
MyParser.ParseObj(new
{
Str = "abc"
, Num = 543
, Arr = new[]{1, 2, 3}
, Chars = new char[]{'x', 'y', 'z'}
, SomeList = new List<string>(){"a","b","c"}
});
}
}
输出:
Str:string
Num:value type
Arr:array
Chars:array
SomeList:IEnumerable
* 更新 *
将代码的演变添加到评论的答案中,以防其他人发现此问题有用:
我知道这是尝试重载单个泛型和数组泛型的常见问题。所有这些答案都在谈论为什么它以这种方式工作以及如何有更好的方法,但是 none 显示了这一点。我正在寻找有关如何完成我所追求的目标的建议,我愿意接受重构并以不同的方式进行重构
这是代表问题的代码: https://dotnetfiddle.net/sWrNj3
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
public static class MyParser
{
public static void Parse<T>(string name, T value)
{
Console.WriteLine($"{name}: single");
}
// Must take in IEnumerable<T>
public static void Parse<T>(string name, IEnumerable<T> collection)
{
Console.WriteLine($"{name}: IEnumerable");
}
public static void ParseObj<T>(T data)
{
foreach (var prop in data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
Parse(prop.Name, prop.GetValue(data, null));
}
}
}
public class Program
{
public static void Main()
{
MyParser.ParseObj(new
{
Str = "abc",
Num = 543,
Arr = new[]{1, 2, 3},
Chars = new char[]{'x', 'y', 'z'}}
);
}
}
结果:
Str: single
Num: single
Arr: single
Chars: single
期望:
Str: single
Num: single
Arr: IEnumerable
Chars: IEnumerable
我认为您拥有所需的所有信息,但您不能将所有工作委托给编译器,您必须自己完成工作:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using static System.Console;
public static class MyParser
{
public static void ParseObj<T>(T data)
{
foreach (var prop in data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if(prop.PropertyType.IsArray) WriteLine($"{prop.Name}:array");
else if(prop.PropertyType == (typeof(string))) WriteLine($"{prop.Name}:string");
else if(prop.PropertyType.IsValueType) WriteLine($"{prop.Name}:value type");
else if(typeof(IEnumerable).IsAssignableFrom(prop.PropertyType)) WriteLine($"{prop.Name}:IEnumerable");
else if(prop.PropertyType.IsEnum) WriteLine($"{prop.Name}:enum");
else if(prop.PropertyType.IsClass) WriteLine($"{prop.Name}:class");
else WriteLine($"{prop.Name}:something else");
}
}
}
public class Program
{
public static void Main()
{
MyParser.ParseObj(new
{
Str = "abc"
, Num = 543
, Arr = new[]{1, 2, 3}
, Chars = new char[]{'x', 'y', 'z'}
, SomeList = new List<string>(){"a","b","c"}
});
}
}
输出:
Str:string
Num:value type
Arr:array
Chars:array
SomeList:IEnumerable
* 更新 * 将代码的演变添加到评论的答案中,以防其他人发现此问题有用: