字典中的接口元组键

时间:2018-02-09 08:29:26

标签: c# dictionary interface

对于我项目中的所有实体,我有一个基本实体,并从该接口另一个接口,然后是一个类(这是它是如何,我不能改变它)。

给定任何2个对象,我想调用一个方法。 我创建了一个带有元组键的字典,以便能够检索正确的方法。

这是代码:

Pool

不确定如何声明字典的键,因为调用失败:"字典中没有给定的键。"

public interface IAnimal
{
    string Name { get; set; }
}

public interface IDog : IAnimal
{

}

public interface ICat : IAnimal
{

}

public interface IMouse : IAnimal
{

}

public class Cat : ICat
{
    public string Name { get; set; }
}

public class Dog : IDog
{
    public string Name { get; set; }
}

public class Mouse : IMouse
{
    public string Name { get; set; }
}

public class Linker
{
    private static Dictionary<Tuple<Type, Type>, object> _linkMethodsDictionary = new Dictionary<Tuple<Type, Type>, object>();

    private static bool _linkDictionaryWasInitialized = false;

    public void InitializeLinkMethods()
    {
        if (_linkDictionaryWasInitialized) return;

        _linkMethodsDictionary.Add(Tuple.Create(typeof(IDog), typeof(ICat)), (Action<IDog, ICat>)LinkDogToCat);
        _linkMethodsDictionary.Add(Tuple.Create(typeof(ICat), typeof(Mouse)), (Action<ICat, IMouse>)LinkCatToMouse);
        _linkDictionaryWasInitialized = true;
    }

    public void Link<T, TU>(T entity1, TU entity2) where T : class, IAnimal
        where TU : class, IAnimal
    {
        Action<T, TU> linkMethod = _linkMethodsDictionary[Tuple.Create(typeof(T), typeof(TU))] as Action<T, TU>;
        if (linkMethod == null)
            throw new NotImplementedException($"Could not find link method for {entity1.Name} and {entity2.Name}");
        linkMethod(entity1, entity2);
    }

    public void LinkDogToCat(IDog dog, ICat cat)
    {
        Console.WriteLine($"Dog: {dog.Name} - Cat:{cat.Name}");
    }

    public void LinkCatToMouse(ICat cat, IMouse mouse)
    {
        Console.WriteLine($"Cat: {cat.Name} - Mouse:{mouse.Name}");
    }

1 个答案:

答案 0 :(得分:0)

我使用结构作为词典的关键

 internal struct EntityLinkKey
 {
    private readonly Type _first;

    private readonly Type _second;

    public EntityLinkKey(Type first, Type second)
    {
        this._first = first;
        this._second = second;
    }

    public override int GetHashCode()
    {
        return this._first.GetHashCode() * 17 + this._second.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (GetType() != obj.GetType()) return false;

        EntityLinkKey p = (EntityLinkKey)obj;
        return p._first == this._first && p._second == this._second;
    }
}