【发布时间】:2021-08-28 22:01:08
【问题描述】:
我正在尝试修改一个纸牌游戏项目,其中有一个函数可以确定玩家创建的套牌是否是基本套牌。
我正在考虑修改,添加一个类似的函数来判断卡组是否是特殊卡组。
作为一个 C# 初学者,我的想法很简单:我看到一个类有一个成员函数 A(),所以在 A() 的代码下我只是复制并粘贴它来制作一个类似的函数 B()。而且我只将 B() 与 A() 一起使用,这样就不会有命名空间问题:如果 A() 工作良好,那么我的 B() 也应该工作,因为它们来自同一个类和同一个文件。
但是,结果是在另一个 .cs 文件中,原点 A() 运行良好,但我的 B() 显示错误 CS1061:
“DeckModel”不包含“IsSpecialDeck”的定义,并且找不到接受“DeckModel”类型的第一个参数的可访问扩展方法(您是否缺少 using 指令或程序集引用?)
怎么会这样?我尝试过重建/重新编译之类的方法。
src\Cynthia.Card\src\Cynthia.Card.Common\GwentGame\GwentDeck.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using Alsein.Extensions.Extensions;
namespace Cynthia.Card
{
public static class GwentDeck
{
public static DeckModel CreateBasicDeck(int defaultDeckIndex)
{
//some other codes......
}
public static bool IsBasicDeck(this DeckModel deck)//A(),the original one
{
var decks = deck.Deck.Select(x => GwentMap.CardMap[x]);
var deckFaction = GwentMap.CardMap[deck.Leader].Faction;
if (decks.Any(x => x.Faction != Faction.Neutral && x.Faction != deckFaction))
return false;
if (decks.Count() < 25 || decks.Count() > 40)
return false;
if (decks.Where(x => x.Group == Group.Gold).Count() > 4 ||
decks.Where(x => x.Group == Group.Silver).Count() > 6 ||
decks.Any(x => x.Group == Group.Leader))
return false;
if (decks.Where(x => x.Group == Group.Gold).Distinct().Count() != decks.Where(x => x.Group == Group.Gold).Count() ||
decks.Where(x => x.Group == Group.Silver).Distinct().Count() != decks.Where(x => x.Group == Group.Silver).Count())
return false;
if (decks.Where(x => x.Group == Group.Copper).GroupBy(x => x.CardId).Select(x => x.Count()).Any(x => x > 3))
return false;
return true;
}
public static bool IsSpecialDeck(this DeckModel deck)//B(), my work
{
var decks = deck.Deck.Select(x => GwentMap.CardMap[x]);
var deckFaction = GwentMap.CardMap[deck.Leader].Faction;
if (decks.Any(x => x.Faction != Faction.Neutral && x.Faction != deckFaction))
return false;
if (decks.Count() < 25 || decks.Count() > 40)
return false;
if (decks.Where(x => x.Group == Group.Gold).Count() > 8 ||
decks.Where(x => x.Group == Group.Silver).Count() > 12 ||
decks.Any(x => x.Group == Group.Leader))
return false;
if (decks.Where(x => x.Group == Group.Gold).GroupBy(x => x.CardId).Select(x => x.Count()).Any(x => x > 3) ||
decks.Where(x => x.Group == Group.Silver).GroupBy(x => x.CardId).Select(x => x.Count()).Any(x => x > 3))
return false;
if (decks.Where(x => x.Group == Group.Copper).Distinct().Count() != decks.Where(x => x.Group == Group.Copper).Count())
return false;
return true;
}
}
}
客户端代码
(src\Cynthia.Card.Unity\src\Cynthia.Unity.Card\Assets\Script\MathMenu\MatchInfo.cs):
using System.Collections;
using System.Collections.Generic;
using Cynthia.Card.Client;
using Cynthia.Card;
using UnityEngine;
using Alsein.Extensions;
using System.Linq;
using Autofac;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
using Assets.Script.Localization;
using Alsein.Extensions.LifetimeAnnotations;
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
using Alsein.Extensions.IO;
using System.Collections.Concurrent;
public class MatchInfo : MonoBehaviour
{
//other attributes and methods here.....
public async void MatchButtonClick()/////pressing the match button
{
try
{
//already in qneue
if (IsDoingMatch)
{
await _client.StopMatch();
return;
}
//if the deck is special
//error CS1061: 'DeckModel' does not contain a definition for 'IsSpecialDeck' accepting a first argument of type 'DeckModel' could be found
if(_client.User.Decks.Single(x => x.Id == CurrentDeckId).IsSpecialDeck())
MatchButtonText.text="special"+MatchButtonText.text;
//if the deck is not special and not basic,stop matching
else if (!_client.User.Decks.Single(x => x.Id == CurrentDeckId).IsBasicDeck())
{
await _UIService.YNMessageBox("PopupWindow_IncompleteDeckTitle", "PopupWindow_IncompleteDeckDesc", "PopupWindow_OkButton", isOnlyYes: true);
return;
}
//otherwise start matching
_ = _client.MatchOfPassword(CurrentDeckId, MatchPassword.text);
//some other codes....
}
catch
{
SceneManager.LoadScene("LoginScene");
_client.ClientState = ClientState.Standby;
}
}
//other methods...
}
DeckModel 类
(src\Cynthia.Card\src\Cynthia.Card.Common\Models\DeckModel.cs):
using System.Collections.Generic;
namespace Cynthia.Card
{
public class DeckModel : ModelBase
{
public string Name { get; set; } = "";
public List<string> Deck { get; set; } = new List<string>();
public string Leader { get; set; } = "";
}
}
【问题讨论】:
-
格式化您的代码。它看起来像 IsSpecialDeck 我在 IsBasicDeck 内
-
现在格式化。我在 vscode 中使用了彩色括号扩展,所以我认为这里没有问题
-
一个扩展方法(比如你的
IsSpecialDeck)需要一个using Cynthia.Card;(它的类所在的命名空间)。为什么不让它成为一个普通的实例方法呢?
标签: c# .net unity3d visual-studio-code