簡體   English   中英

c#通用接口集合不編譯

[英]c# generic interface collection does not compile

我在創建通用接口集合時遇到問題。

這是我的通用接口:

public interface IPlayerInputHandler<out TDataStore, out TStateContext, in TPlayerInput, out TServerOutput>
    where TDataStore : DataStore
    where TStateContext : IStateContext
    where TPlayerInput : IPlayerInput
    where TServerOutput : IServerOutputPacket
{
    TDataStore DataStore { get; }
    TStateContext Context { get; }

    TServerOutput HandleInput(ushort serverTick, TPlayerInput playerInput, Guid CharacterId);
}

這是一個基類,我的`PlayerInputHandler

public abstract class ServerPlayerInputHandler<TPlayerInput, TServerOutput> : IPlayerInputHandler<DataStore, StateContext, TPlayerInput, TServerOutput>
    where TPlayerInput : IPlayerInput
    where TServerOutput : IServerOutputPacket
{
    public DataStore DataStore { get; private set; }
    public StateContext Context { get; private set; }

    protected readonly ServerStateManager ServerStateManager;
    protected readonly ServerPlayersManager PlayersManager;

    protected ServerPlayerInputHandler(DataStore dataStore, StateContext context, ServerStateManager serverStateManager, ServerPlayersManager playersManager)
    {
        DataStore = dataStore;
        Context = context;
        ServerStateManager = serverStateManager;
        PlayersManager = playersManager;
    }
    public abstract TServerOutput HandleInput(ushort serverTick, TPlayerInput playerInput, Guid CharacterId);

}

以及 PlayerInputHandler 的示例:

public class AddItemInputHandler : ServerPlayerInputHandler<AddItemInput, AddItemPacket>
{
    public AddItemInputHandler(DataStore dataStore, StateContext context, ServerStateManager serverStateManager, ServerPlayersManager serverPlayersManager)
        : base(dataStore, context, serverStateManager, serverPlayersManager)
    {
    }

    public override AddItemPacket HandleInput(ushort serverTick, AddItemInput playerInput, Guid playerId)
    {
        var instanceId = Extensions.GenerateShortId();

        Context.Mutator.CharacterAddItem(playerId, playerInput.ItemId, instanceId);
        var aip = new AddItemPacket(playerInput.ItemId, instanceId, playerId, serverTick);

        return aip;
    }
}

我希望能夠創建一個集合,如下所示:

private Dictionary<PlayerInputType, IPlayerInputHandler<IPlayerInput, IServerOutputPacket, IPlayerInput, IServerOutputPacket>> _playerInputHandlers;

並且能夠將AddItemInputHandler添加到集合中。

從我的通用接口可以看出,它公開的方法也依賴於TPlayerInputTServerOutput ,我不希望創建一個接受IPlayerInputTServerOutput的方法,因為它可能會導致裝箱和拆箱。

你們有什么建議如何繼續嗎?

首先,命名一個類 Addsomething 是一種糟糕的方式。 我認為。

老實說,我不太明白你想做什么。 不過,讓我們嘗試一下。

我為集合創建了一個類

> public class PlayerInputHandlers : Dictionary<
>                             PlayerInputType,
>                             IPlayerInputHandler<
>                                 IPlayerInput,
>                                 IServerOutputPacket,
>                                 IPlayerInput,
>                                 IServerOutputPacket>
>                         > {
> 
>     public void AddItemInputHandler<T, U>(T PlayerInputType, U PlayerInputHandler) 
>         where T : PlayerInputType
>         where U: IPlayerInputHandler<IPlayerInput, IServerOutputPacket, IPlayerInput, IServerOutputPacket>
>     {
>         this.Add(PlayerInputType, PlayerInputHandler);
>     } 
> 
> }

這是我所有的界面

public class DataStore : IPlayerInput
public interface IPlayerInput
public interface IPlayerInputHandler<out TDataStore, out TStateContext, in TPlayerInput, out TServerOutput>
        where TDataStore : IPlayerInput
        where TStateContext : IStateContext
        where TPlayerInput : IPlayerInput
        where TServerOutput : IServerOutputPacket
public interface IServerOutputPacket : IStateContext
public interface IServerPlayerInputHandler<TPlayerInput, TServerOutput>
        where TPlayerInput : IPlayerInput
        where TServerOutput : IServerOutputPacket
public interface IStateContext
public class PlayerInputType
public abstract class ServerPlayerInputHandler<TPlayerInput, TServerOutput> : IPlayerInputHandler<DataStore, StateContext, TPlayerInput, TServerOutput>, IServerPlayerInputHandler<TPlayerInput, TServerOutput> 
        where TPlayerInput : IPlayerInput
        where TServerOutput : IServerOutputPacket
public class ServerPlayersManager
public class ServerStateManager
public class StateContext : IStateContext

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM