Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#通用接口集合未编译_C#_Oop_Generics_Inheritance_Constraints - Fatal编程技术网

c#通用接口集合未编译

c#通用接口集合未编译,c#,oop,generics,inheritance,constraints,C#,Oop,Generics,Inheritance,Constraints,我在创建泛型接口集合时遇到问题 这是我的通用界面: public interface IPlayerInputHandler<out TDataStore, out TStateContext, in TPlayerInput, out TServerOutput> where TDataStore : DataStore where TStateContext : IStateContext where TPlayerInput : IPlayerInput

我在创建泛型接口集合时遇到问题

这是我的通用界面:

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);
}
公共接口IPlayerInputHandler
其中TDataStore:DataStore
其中TStateContext:IStateContext
其中TPlayerInput:IPlayerInput
其中TServerOutput:IServerOutputPacket
{
TDataStore数据存储{get;}
TStateContext上下文{get;}
TServerOutput HandleInput(ushort-serverTick、TPlayerInput-playerInput、Guid字符);
}
这是一个基类,我的` 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);
公共抽象类ServerPlayerInputHandler:IPlayerInputHandler
其中TPlayerInput:IPlayerInput
其中TServerOutput:IServerOutputPacket
{
公共数据存储数据存储{get;private set;}
public StateContext上下文{get;private set;}
受保护的只读ServerStateManager ServerStateManager;
受保护的只读服务器PlayerManager PlayerManager;
受保护的ServerPlayerInputHandler(数据存储数据存储、StateContext上下文、ServerStateManager ServerStateManager、ServerPlayerManager PlayerManager)
{
数据存储=数据存储;
上下文=上下文;
ServerStateManager=ServerStateManager;
PlayersManager=PlayersManager;
}
公共抽象TServerOutput HandleInput(ushort-serverTick、TPlayerInput-playerInput、Guid字符);
}

还有一个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;
    }
}
public类additeminputhHandler:ServerPlayerInputHandler
{
公共AddItemInputHandler(数据存储数据存储、StateContext上下文、ServerStateManager ServerStateManager、ServerPlayerManager ServerPlayerManager)
:base(数据存储、上下文、serverStateManager、ServerPlayerManager)
{
}
公共覆盖AddItemPacket HandleInput(USHost serverTick、AddItemInput playerInput、Guid playerId)
{
var instanceId=Extensions.generateSortId();
Context.Mutator.CharacterAddItem(playerId、playerInput.ItemId、instanceId);
var aip=新的AddItemPacket(playerInput.ItemId、instanceId、playerId、serverTick);
返回aip;
}
}
我希望能够创建一个集合,如下所示:

private Dictionary<PlayerInputType, IPlayerInputHandler<IPlayerInput, IServerOutputPacket, IPlayerInput, IServerOutputPacket>> _playerInputHandlers;
private Dictionary\u playerInputHandler;
并且能够将
AddItemInputHandler
添加到集合中

从我的通用界面可以看出,它公开的方法还依赖于
TPlayerInput
TServerOutput
,我不希望创建一个接受
IPlayerInput
TServerOutput
的方法,因为它可能导致装箱和取消装箱


你们有什么建议吗?

首先,命名一个类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);
>     } 
> 
> }
>公共类playerInputHandler:字典<
>PlayerInputType,
>IPlayerInputHandler<
>IPlayerInput,
>IServOutputPacket,
>IPlayerInput,
>IServOutputPacket>
>                         > {
> 
>public void additeminputhHandler(T PlayerInputType,U PlayerInputHandler)
>其中T:PlayerInputType
>其中U:IPlayerInputHandler
>     {
>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
公共类数据存储:IPlayerInput
公共接口IPlayerInput
公共接口IPlayerInputHandler
其中TDataStore:IPlayerInput
其中TStateContext:IStateContext
其中TPlayerInput:IPlayerInput
其中TServerOutput:IServerOutputPacket
公共接口IServOutputPacket:IStateContext
公共接口IServerPlayerInputHandler
其中TPlayerInput:IPlayerInput
其中TServerOutput:IServerOutputPacket
公共接口IStateContext
公共类PlayerInputType
公共抽象类ServerPlayerInputHandler:IPlayerInputHandler、IServerPlayerInputHandler
其中TPlayerInput:IPlayerInput
其中TServerOutput:IServerOutputPacket
公共类服务器PlayerManager
公共类ServerStateManager
公共类StateContext:IStateContext

我要问的第一个问题是:这真的需要是泛型类吗?为什么它不能只是一个接受IPlayerInput并返回IServerOutputPacket的类?@CallumBradbury如果该方法将接收IPlayerInput,则意味着我的IPlayerInput的结构实现将被装箱—我试图阻止这种行为。我还应该提到,代码的这一部分对性能至关重要,因此我选择使用结构(数据相对较小,它满足使用结构而不是类的要求),我从未想过将
PlayerInputHandlers
转换为从字典继承,这似乎很有趣,我将尝试并更新它。谢谢你的回复!我试图集成您想要的解决方案,但我收到了相同的错误,这是我的PoC:至于我试图实现的目标:我试图抽象命令处理程序,客户端可以发送到服务器,因此在这种情况下,
AddItemInputHandler
处理程序处理
AddItem
命令,因此,
AddItemHandler
应限制为该类型的命令抱歉,这里是关机。好几个星期都看不到你的问题。冠状病毒获胜。