dimanche 25 septembre 2016

C# Get Interfaces but not the base one

Here is little problem.

I have interface "ISuperAbility". And I have some superAbilities(also interfaces) which inherited from ISuperAbility.

So, I have a dictionary with a Type as a key, and double(points) as a Value. In that dictionary I have next data:

        abilityPoints = new Dictionary<Type, double>();
        abilityPoints.Add(typeof(IFlyable), 2.0f);
        abilityPoints.Add(typeof(IInvisible), 4.0f);
        abilityPoints.Add(typeof(IMindsReadable), 6.0f);
        abilityPoints.Add(typeof(IWallWalkable), 1.0f);

All these "abilities" is a interfaces that inheried from ISuperAbility.

Then, I have hero, for example "Mysterio", that implements two interfaces: IMindsReadble IInvisible

SO, when I want to get all points on certain hero, I do the next thing:

        public static double ForceOfHuman(Human hero)
        {
            double force = 0;

            Console.WriteLine(hero.GetType());
            Type[] humanInterfaces = hero.GetType().GetInterfaces();

            foreach (Type humanInterface in humanInterfaces)
            {
                if (humanInterface.IsAssignableFrom(typeof(ISuperAbility)))
                {
                    force += XMenLaboratory.abilityPoints[humanInterface];
                }
            }

            return force;
        }

And after this I have an exception that tell me about problem cause dictionary does not have such a key. And a key is "ISuperAbility".

So that search in method return also a base interface. Is it normal ? I think, more than that.

So, what can I do for getting interfaces except base one ?





Aucun commentaire:

Enregistrer un commentaire