C#重载键/值和非键/值类型,使用var而不指定类型

时间:2019-01-18 09:44:15

标签: c#

下面的示例中,explicitly specify type将调用Execute<TKey, TValue>(this IEnumerable<IEnumerable<KeyValuePair<TKey, TValue>>>var关键字call Execute<T>

我尝试解决此问题,因为用户始终使用var v = "";而不是string v = ""

EX:

void Main()
{
    IEnumerable<IEnumerable<KeyValuePair<string, string>>> data = new[] {
        new Dictionary<string, string> (){
            {"Name" , "ITWeiHan" }
        }
    };
    data.Execute(); //call Execute<TKey, TValue>(this IEnumerable<IEnumerable<KeyValuePair<TKey, TValue>>> enums){}

    var data2 = new[] {
        new Dictionary<string, string> (){
            {"Name" , "ITWeiHan" }
        }
    };
    data2.Execute(); //call Execute<T>
}

public static class Test
{
    public static void Execute<TKey, TValue>(this IEnumerable<IEnumerable<KeyValuePair<TKey, TValue>>> enums){}

    public static void Execute<T>(this IEnumerable<T> enums){
    }
}

我试图用is解决问题。

当键/值是<object,object>时,值enums is IEnumerable<IEnumerable<KeyValuePair<object, object>>>true,但是键/值是<string,string>将返回false

我希望无论键/值的类型是什么,enums is IEnumerable<IEnumerable<KeyValuePair<object, object>>>总是得到true

void Main()
{
    var dataBy_String_String = new[] {
        new Dictionary<string, string> (){
            {"Name" , "ITWeiHan" }
        }
    };
    dataBy_String_String.Execute(); //ExecuteByNonKeyValueType

    var dataBy_Object_Object = new[] {
        new Dictionary<object, object> (){
            {"Name" , "ITWeiHan" }
        }
    };
    dataBy_Object_Object.Execute(); //ExecuteByKeyValueType 
}

public static class Test
{
    private static void ExecuteByKeyValueType<TKey, TValue>(this IEnumerable<IEnumerable<KeyValuePair<TKey, TValue>>> enums) { }
    private static void ExecuteByNonKeyValueType<T>(this IEnumerable<T> enums) { }

    public static void Execute<T>(this IEnumerable<T> enums){
        if(enums is IEnumerable<IEnumerable<KeyValuePair<object, object>>>){
            Console.WriteLine("ExecuteByKeyValueType");
            ExecuteByKeyValueType(enums as IEnumerable<IEnumerable<KeyValuePair<object, object>>>);
        }else{
            Console.WriteLine("ExecuteByNonKeyValueType");
            ExecuteByNonKeyValueType(enums);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

好吧,要么强迫您的团队使用正确的类型,例如使用resharper,如果您使用var,它可能显示警告或错误,或者告诉他们至少应指定参数类型:

var data = new[] {
    new Dictionary<string, string> (){
        {"Name" , "ITWeiHan" }
    }
};

// calls wrong overload
data.Execute(); //calls IEnumerable<T>
// calls corrrect
data.Execute<string, string>(); //calls IEnumerable<IEnumerable<KeyValuePair<TKey, TValue>>>

public static void Execute<TKey, TValue>(this IEnumerable<IEnumerable<KeyValuePair<TKey, TValue>>> enums)
{
    Console.WriteLine("IEnumerable<IEnumerable<KeyValuePair<TKey, TValue>>>");
}

public static void Execute<T>(this IEnumerable<T> enums)
{
    Console.WriteLine("IEnumerable<T>");
}

另一种解决方案是为字典提供第三个更具体的重载,该重载进行强制转换并调用所需的重载:

public static void Execute<TKey, TValue>(this IEnumerable<Dictionary<TKey, TValue>> dictionaries)
{
    var enums = (IEnumerable<IEnumerable<KeyValuePair<TKey, TValue>>>) dictionaries;
    enums.Execute();
}

现在,您的团队成员可以使用var,而无需指定类型参数。