简体   繁体   English

如何在 c# 中使方法通用?

[英]How to make a method generic in c#?

I have couple of methods and it keeps on expanding.我有几种方法,而且它还在不断扩展。 So, I am planning to make it generic.所以,我打算让它通用。 Can anyone please help me with that.谁能帮我解决这个问题。 Atleast the method definition.至少方法定义。

private static Dictionary<string, class1> PToDictionary(MapField<string, class1Proto> keyValuePairs)
{
    Dictionary<string, class1> keyValues = new();
    foreach (var pair in keyValuePairs)
    {
       **keyValues[pair.Key] = pToR(pair.Value);**
    }
    return keyValues;
}

My another method is:我的另一种方法是:

private static Dictionary<Uri, class2> PToDictionary1(MapField<string, class2Proto> keyValuePairs)
{
        Dictionary<string, class2> keyValues = new();
        foreach (var pair in keyValuePairs)
        {
           **keyValues[new Uri(pair.Key)] = pToR1(pair.Value);**
        }
        return keyValues;
}

How can I make this generic so that when more methods are added, I dont need to add code.我怎样才能使这个通用,以便在添加更多方法时,我不需要添加代码。 I was thinking something like this, but errors are:我在想这样的事情,但错误是:

//   Not sure how to call this method after Func is there
    private static Dictionary<TKey, TValue> PToDictionary<TKey, TValue, TKeyProto, TValueProto>(MapField<TKeyProto, TValueProto> keyValuePairs, Func<TKeyProto, TKey> keyFunc, Func<TValueProto, TValue> valueFunc)
{
   //How can I generalize my above method ?
}

Can someone help me complete the method?有人可以帮我完成这个方法吗?

You don't need an extra method at all.您根本不需要额外的方法。 LINQ already provides everything you need, combined with the fact that MapField implements IDictionary<TKey, TValue> (and therefore IEnumerable<KeyValuePair<TKey, TValue>> . LINQ 已经提供了你需要的一切,结合MapField实现IDictionary<TKey, TValue> (因此IEnumerable<KeyValuePair<TKey, TValue>>的事实。

You'd just call:你只要打电话:

var dictionary = repeatedField.ToDictionary(
    pair => ConvertKey(pair.Key), pair => ConvertValue(pair.Value));

(where ConvertKey would be whatever code you want to convert the repeated field key into the dictionary key, and likewise for ConvertValue ). (其中ConvertKey是您想要将重复字段键转换为字典键的任何代码,对于ConvertValue也是如此)。

Sample calls:示例调用:

var d1 = repeatedField1.ToDictionary(pair => pair.Key, pair => pToR(pair.Value));

var d2 = repeatedField2.ToDictionary(
    pair => new Uri(pair.Key), pair => pToR1(pair.Value));

... but you may be able to remove the pToR and pToR1 methods anyway. ...但是无论如何您都可以删除pToRpToR1方法。 (It's hard to tell without information about what they're doing...) (如果没有关于他们在做什么的信息,很难说...)

You can use the following method to convert MapField<TKeyProto, TValueProto> to Dictionary<TKey, TValue> :您可以使用以下方法将MapField<TKeyProto, TValueProto>转换为Dictionary<TKey, TValue>

public static Dictionary<TKey, TValue> PToDictionary<TKey, TValue, TKeyProto, TValueProto>(
    MapField<TKeyProto, TValueProto> keyValuePairs,
    Func<TKeyProto, TKey> mapKey,
    Func<TValueProto, TValue> mapValue
)
{
    Dictionary<TKey, TValue> keyValues = new();
    foreach (var pair in keyValuePairs)
    {
        keyValues[mapKey(pair.Key)] = mapValue(pair.Value);
    }
    return keyValues;
}

Here, mapKey is a function that converts MapField 's key to a dictionary key.在这里, mapKey是一个 function, MapField的键转换为字典键。 Similarly, mapValue converts MapField 's value to a dictionary value.同样, mapValueMapField的值转换为字典值。

Another way is to make usage of LINQ ToDictionary extension method:另一种方法是使用 LINQ ToDictionary扩展方法:

public static Dictionary<TKey, TValue> PToDictionary<TKey, TValue, TKeyProto, TValueProto>(
    MapField<TKeyProto, TValueProto> keyValuePairs,
    Func<TKeyProto, TKey> mapKey,
    Func<TValueProto, TValue> mapValue
)
{
    // this is possible because MapField<TKey, TValue> implements IEnumerable<KeyValuePair<TKey, TValue>>
    return keyValuePairs.ToDictionary(
        (KeyValuePair<TKeyProto, TValueProto> kvp) => mapKey(kvp.Key),
        (KeyValuePair<TKeyProto, TValueProto> kvp) => mapValue(kvp.Value));
}

For example, if you want to convert MapField<string, string> to Dictionary<Uri, int> you can use the following code:例如,如果要将MapField<string, string>转换为Dictionary<Uri, int>可以使用以下代码:

Dictionary<Uri, int> dictionary = PToDictionary(
    map,
    key => new Uri(key),
    val => int.Parse(val));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM