C# 字典C的聚合扩展#

C# 字典C的聚合扩展#,c#,generics,dictionary,C#,Generics,Dictionary,我正在尝试在字典上创建通用聚合扩展。像这样的 void Main(){ var foo = new Dictionary<string, Metric>(); foo["first"] = new Metric(5); foo["sec"] = new Metric(10); foo.Aggregate<string, Metric, int>("first", new Metric(5)); } public class Metri

我正在尝试在
字典
上创建通用聚合扩展。像这样的

void Main(){
    var foo = new Dictionary<string, Metric>();
    foo["first"] = new Metric(5);
    foo["sec"]  = new Metric(10);

    foo.Aggregate<string, Metric, int>("first", new Metric(5));
}

public class Metric : IAggregatable<int> {
    public int Total { get; set; }

    public Metric(int total) {
        Total = total;
    }

    public void Aggregate(int value) {
        Total += value;
    }
}

public static class DictionaryExtensions {
    public static void Aggregate<TKey, TValue, T>(this Dictionary<TKey, TValue> dic, TKey key, TValue value) where TValue : IAggregatable<T> {
        TValue result;
        if (dic.TryGetValue(key, out result))
            dic[key].Aggregate(value.Total);
        else
            dic[key] = value;
    }
}

public interface IAggregatable<T> {
    T Total { get; set; }
    void Aggregate(T value);
}
void Main(){
var foo=新字典();
foo[“第一”]=新指标(5);
foo[“sec”]=新指标(10);
foo.合计(“第一”,新指标(5));
}
公共类度量:IAggregatable{
公共整数总计{get;set;}
公共度量(整数合计){
总计=总计;
}
公共空隙骨料(int值){
总+=价值;
}
}
公共静态类字典扩展{
公共静态无效聚合(此字典dic、TKey、TValue值),其中TValue:IAggregatable{
t价值结果;
if(dic.TryGetValue(键,输出结果))
dic[key]。聚合(value.Total);
其他的
dic[键]=值;
}
}
公共接口可升级{
T总计{get;set;}
空隙集料(T值);
}

这很有效,但每次调用
Aggregate(…)
时,我都必须指定泛型类型参数。这可以在
main()
中看到,作为
foo.Aggregate(“第一”,新的度量(5))。有没有更干净的方法来获得此功能,因为我不希望每次都指定泛型类型参数。

我认为您的界面有点笨重。您不需要了解指标的内部结构。要聚合,您只需要知道可以聚合什么,而不需要知道如何聚合。实施如何处理这些问题:

using System.Collections.Generic;

namespace ConsoleApplication3
{
    public class Metric : IAggregatable<Metric>
    {
        public int Total { get; set; }

        public Metric(int total)
        {
            Total = total;
        }

        public void Aggregate(Metric other)
        {
            Total += other.Total;
        }
    }

    public static class DictionaryExtensions
    {
        public static void Aggregate<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue value) where TValue : IAggregatable<TValue>
        {
            TValue result;
            if (dic.TryGetValue(key, out result))
                dic[key].Aggregate(value);
            else
                dic[key] = value;
        }
    }

    public interface IAggregatable<T>
    {
        void Aggregate(T other);
    }

    class Program
    {
        void Main()
        {
            var foo = new Dictionary<string, Metric>();
            foo["first"] = new Metric(5);
            foo["sec"] = new Metric(10);

            foo.Aggregate("first", new Metric(5));
        }        
    }
}
使用System.Collections.Generic;
命名空间控制台应用程序3
{
公共类度量:IAggregatable
{
公共整数总计{get;set;}
公共度量(整数合计)
{
总计=总计;
}
公共空隙骨料(公制其他)
{
总计+=其他。总计;
}
}
公共静态类字典扩展
{
公共静态无效聚合(此字典dic、TKey、TValue值),其中TValue:IAggregatable
{
t价值结果;
if(dic.TryGetValue(键,输出结果))
dic[键]。聚合(值);
其他的
dic[键]=值;
}
}
公共接口可升级
{
空隙骨料(T其他);
}
班级计划
{
void Main()
{
var foo=新字典();
foo[“第一”]=新指标(5);
foo[“sec”]=新指标(10);
foo.合计(“第一”,新指标(5));
}        
}
}

我觉得你的界面有点笨重。您不需要了解指标的内部结构。要聚合,您只需要知道可以聚合什么,而不需要知道如何聚合。实施如何处理这些问题:

using System.Collections.Generic;

namespace ConsoleApplication3
{
    public class Metric : IAggregatable<Metric>
    {
        public int Total { get; set; }

        public Metric(int total)
        {
            Total = total;
        }

        public void Aggregate(Metric other)
        {
            Total += other.Total;
        }
    }

    public static class DictionaryExtensions
    {
        public static void Aggregate<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue value) where TValue : IAggregatable<TValue>
        {
            TValue result;
            if (dic.TryGetValue(key, out result))
                dic[key].Aggregate(value);
            else
                dic[key] = value;
        }
    }

    public interface IAggregatable<T>
    {
        void Aggregate(T other);
    }

    class Program
    {
        void Main()
        {
            var foo = new Dictionary<string, Metric>();
            foo["first"] = new Metric(5);
            foo["sec"] = new Metric(10);

            foo.Aggregate("first", new Metric(5));
        }        
    }
}
使用System.Collections.Generic;
命名空间控制台应用程序3
{
公共类度量:IAggregatable
{
公共整数总计{get;set;}
公共度量(整数合计)
{
总计=总计;
}
公共空隙骨料(公制其他)
{
总计+=其他。总计;
}
}
公共静态类字典扩展
{
公共静态无效聚合(此字典dic、TKey、TValue值),其中TValue:IAggregatable
{
t价值结果;
if(dic.TryGetValue(键,输出结果))
dic[键]。聚合(值);
其他的
dic[键]=值;
}
}
公共接口可升级
{
空隙骨料(T其他);
}
班级计划
{
void Main()
{
var foo=新字典();
foo[“第一”]=新指标(5);
foo[“sec”]=新指标(10);
foo.合计(“第一”,新指标(5));
}        
}
}

如果你不打算使用
结果,为什么要使用
TryGetValue
?对,它应该是
result.Aggregate(value)
在if语句之后,这样你就不会做两次我不喜欢的查找了。如果你不打算使用
结果,为什么要使用
TryGetValue
?对,它应该是
result.Aggregate(value)
在if语句之后,这样您就不会进行两次查找了,这是我的错