为无关联的C#类实现类型约束

时间:2017-05-26 16:13:26

标签: c# generics interface generic-type-argument

我正在编写一个C#应用程序,它将生成Dungeons&批量中的龙字符。然而,当我遇到一个"奖金"对于一个角色参数我先跑到墙上。正确地限制奖金方法的通用类型逃脱了我。这是我的一个例子:

{{1}}

我面临的问题是,从用户的角度来看,我希望用户能够添加"此比赛为智能提供+2加值"。不幸的是,能力得分"智能"在与角色关联之前,它不会拥有Value属性,从而使IBonusable约束成为一个问题。

我希望能够将IBonus的value属性约束为与IBonusable项的value属性相同的类型,而不必将奖金直接绑定到特定字符(这将在我的上述实现中发生, ICharacterAbilityScore inherting IBonusable)。

是否存在实现此目的的仅接口方法,或者在定义具体实现时是否必须添加此约束?

另外,我计划首先使用代码EF构建数据库,如果这会影响答案。

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解你的问题,但这符合我的理解。

public interface IAbilityScore : IBaseEntity
{
    string Name { get; set; }
}

public interface IBonus<T>
{
    T Value { get; set; }
}

public interface IBonusable<T>
{
    T Value { get; set; }
}

public interface ICharacterAbilityScore<T> : IBaseEntity, IBonusable<T>,IBonus<T>
{
    ICharacter Character { get; set; }
    IAbilityScore AbilityScore { get; set; }
    bool IsSaveProficient { get; set; }
}