【问题标题】:Get Interfaces but not the base one获取接口但不是基础接口
【发布时间】:2017-02-02 23:43:06
【问题描述】:

这是一个小问题。

我有界面“ISuperAbility”。 而且我有一些从 ISuperAbility 继承的 superAbilities(也是接口)。

所以,我有一个字典,其中 Type 作为键,double(points) 作为值。 在那个字典里我有下一个数据:

        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);

所有这些“能力”都是继承自 ISuperAbility 的接口。

然后,我有实现两个接口的英雄,例如“Mysterio”: IMindsReadble 不可见

所以,当我想获得某个英雄的所有分数时,我会做下一件事:

        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;
        }

在此之后我有一个异常告诉我问题原因字典没有这样的键。一个关键是“ISuperAbility”。

因此,方法中的搜索也返回一个基本接口。正常吗?我认为,不止于此。

那么,除了base 1,我还能做些什么来获取接口呢?

编辑 1

我必须说,我在某些时候错了。 我从ISuperAbility继承的能力没有达到这个条件

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

编辑 2

解决方案,我更喜欢下一个:

                if (typeof(ISuperAbility).IsAssignableFrom(humanInterface) && humanInterface != typeof(ISuperAbility))
                {
                    force += XMenLaboratory.abilityPoints[humanInterface];
                }

谢谢大家。

【问题讨论】:

  • 您的IsAssignableFrom 正确吗?您不是要寻找typeof(ISuperAbility).IsAssignableFrom( humanInterface) 的接口吗?
  • @IanMercer 对不起,我不明白你的意思。我的代码不适合我的目的吗?是不是逻辑有问题?
  • @VladislavHromyh Ian Mercer 的意思是,为了检查humanInterface 是否继承ISuperAbility,您应该使用typeof(ISuperAbility).IsAssignableFrom( humanInterface)。你的条件不对,所以达不到。
  • 你的情况倒退了。 IFlyable 不能从 ISuperAbility 分配,它可以分配给 ISuperAbility
  • 哦,谢谢你们。我是第一次使用,所以使用方法IsAssignableFrom()的方式不对。

标签: c# inheritance reflection interface


【解决方案1】:

您可以从foreach 语句中排除接口ISuperAbility

foreach (Type humanInterface in humanInterfaces.Where(i => i != typeof(ISuperAbility)))

这样你就可以获得除了ISuperAbility之外的所有能力接口。

【讨论】:

    【解决方案2】:

    你可以按照@dotnetom 所说的那样做。这是一个简单(并且可能是最好的)可行的解决方案。

    或者 - 如果您想要一些通用解决方案 - 您可以在所有类型(接口类型)上调用“GetInterfaces()”。如果结果是空数组/null,你就会知道,它是“根接口”并跳过它。

    编辑: 示例代码:

    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.GetInterfaces().Length < 1)
            {
                // the interface is "base interface", so we ignore it
                continue;
            }
    
            // we are suming the ability point from "derived interface"
            force += XMenLaboratory.abilityPoints[humanInterface];
        }
    
        return force;
    }
    

    【讨论】:

    • Hmgh... 你能重述一下主要思想吗? “你可以在所有类型上调用“GetInterfaces()”是什么意思
    • 哦...我明白了。举个例子,谢谢,我试试看。
    猜你喜欢
    • 2018-03-29
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    相关资源
    最近更新 更多