无法投射类型''在类型''

时间:2015-03-23 14:45:18

标签: c# casting

我正在为游戏的服务器端工作,它几乎已经完成,但今天我遇到了问题...... 这个游戏基于2种形式,触地得分和死亡竞赛,在游戏结束时服务器必须知道结果。这是一款在线游戏。 当我结束比赛或玩家离开时,服务器崩溃,它不应该,它只是回到房间。 它给了我这个错误:

[ERROR]: Unable to cast object of type 'FagNet.Core.Data.GameScore' to type 'FagNet.Core.Data.TDGameScore'.
   in FagNet.Core.Data.Player.SetNewTDStats(Boolean win)
   in FagNet.Core.Data.Room.BroadcastBriefing(Boolean result)
   in FagNet.Core.Data.Room.<BeginResult>d__2b.MoveNext()
--- End of stack trace ---
   in System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__1(Object state)
   in System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   in System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   in System.Threading.ThreadPoolWorkQueue.Dispatch()
   in System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

任何人都可以帮助我!? 我需要帮助:(

这是FagNet.Core.Data.GameScore的来源:

public class GameScore
    {
        public DateTime JoinTime { get; set; }
        public uint TotalPoints { get; set; }

        public virtual uint CalculateEXP()
        {
            return 0;
        }
    }

这是FagNet.Core.Data.TDGameScore:

的来源
public class TDGameScore : GameScore
{
    public uint TDScore { get; set; }
    public uint TDAssists { get; set; }

    public uint Kills { get; set; }
    public uint KillAssists { get; set; }
    public uint Offense { get; set; }
    public uint OffenseAssists { get; set; }
    public uint Defense { get; set; }
    public uint DefenseAssists { get; set; }
    public uint Recovery { get; set; }

    public override uint CalculateEXP()
    {
        var ts = DateTime.Now - JoinTime;
        if (ts.TotalSeconds < 0 || TotalPoints == 0)
            return 0;
        return (uint)ts.TotalSeconds / 4 + (TDScore * 15) + (100 * TotalPoints / (500 + 2 * TotalPoints) * 14);
    }
}

2 个答案:

答案 0 :(得分:0)

看到FagNet.Core.Data.Player.SetNewTDStats(布尔胜利)的代码会很有趣(我不能发表评论,所以不得不写一个答案)。

我的猜测是你试图将GameScore类型的实例转换为TDGameStore,除非你将一个TDGameStore实例分配给GameScore类型的变量然后尝试强制转换,否则这个实例是无效的。 (对不起,这是一个不好的解释!)

我会在TDGameScore中添加一个复制构造函数,例如:

public TDGameStore(GameStore gs) {
  JoinTime = gs.JoinTime;
  TotalPoints = gs.TotalPoints;
}

希望有所帮助

答案 1 :(得分:0)

这是FagNet.Core.Data.Player.SetNewTDStats(布尔胜利)的代码:

public void SetNewTDStats(bool win = false)
    {
        var score = (TDGameScore) GameScore;
        TDStats.TotalMatches++;
        if (win)
            TDStats.Won++;
        else
            TDStats.Lost++;

        TDStats.TotalKills += score.Kills;
        TDStats.TotalKillAssists += score.KillAssists;
        TDStats.TotalOffense += score.Offense;
        TDStats.TotalOffenseAssists += score.OffenseAssists;
        TDStats.TotalDefense += score.Defense;
        TDStats.TotalDefenseAssists += score.DefenseAssists;
        TDStats.TotalRecovery += score.Recovery;
        TDStats.TotalTouchdowns += score.TDScore;
        TDStats.TotalTouchdownAssists += score.TDAssists;
        GameDatabase.Instance.UpdateTDStats(AccountID, TDStats);
    }