如何重载通用IEnumerable

时间:2019-07-06 00:36:36

标签: c# generics ienumerable

我知道这是尝试使单个泛型和数组泛型过载的常见问题。所有这些答案都讨论了为什么以这种方式起作用,以及如何有更好的方法,但是没有一个能证明这一点。我正在寻找有关如何实现自己所追求的目标的建议,我愿意重构并以不同的方式进行操作

以下是代表问题的代码: 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

1 个答案:

答案 0 :(得分:0)

我认为您在那里拥有所需的所有信息,但是您无法将所有工作委托给编译器,您必须自己完成工作:

http://www.example.com/test_program/files/homescript.js

输出:

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"}
        });
    }
}

*更新* 如果有人发现此问题有用,请在注释的答案中添加代码的演变:

  1. https://dotnetfiddle.net/Zkx7UJ
  2. https://dotnetfiddle.net/aeCG3z
  3. https://dotnetfiddle.net/6Z49ta
  4. https://dotnetfiddle.net/u5HAFP