我有一个很奇怪的问题。我尝试了所有修复方法,但到目前为止没有任何效果。
如您在第一张图片中所见,当我尝试从“UnnamedGameServer”外部的 namespace 引用静态类“SharedConstants”时,编译器将返回以下错误:
我发现使用UnnamedGameServer.SharedConstants而不是SharedConstants并使用UnnamedGameServer引用此类时,此问题已得到解决。在.cs文件顶部。但是我更喜欢避免在使用该类的每一行上都引用它。
这是我的代码的一些屏幕截图:
第一张截图:
第二张截图:
编辑: using语句的屏幕截图:
编辑2:代码,不带屏幕截图:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections;
using ProtoBuf;
using UnnamedGameServer;
using System.Threading;
namespace Unnamed_game
{
class Connection
{
public struct UserInformation
{
public string Username;
public int UserID;
}
static private Connection instance;
private Connection()
{
client = new TcpClient();
header = new PacketHeader();
_isLoggedCharacter = false;
_isLoggedUser = false;
magicByte = UnnamedGameServer.SharedConstants.ServerInfo.MagicByte;
}
// properties
TcpClient client;
PacketHeader header;
Thread serverCommThread;
byte[] magicByte;
bool _isLoggedCharacter;
CharacterInformation chInformation
{
get
{
return Player.Instance().Information;
}
}
bool _isLoggedUser;
public UserInformation Information;
// methods
static public Connection Instance()
{
if(instance == null)
{
instance = new Connection();
return instance;
}
else
{
return instance;
}
}
/// <summary>
/// Should never be used. Use the parameterless one to get the server address and information. It doesn't use try-catch, the exception handling should be done on Game1.Initialize()
/// </summary>
public void ConnectServer(IPEndPoint endPoint)
{
if (client.Connected)
{
return;
}
else
{
client.Connect(endPoint);
serverCommThread = new Thread(HandleServerCommunication);
serverCommThread.Start();
}
}
public void ConnectServer()
{
this.ConnectServer(new IPEndPoint(UnnamedGameServer.SharedConstants.ServerInfo.ServerAddress, UnnamedGameServer.SharedConstants.ServerInfo.Port));
}
private void HandleServerCommunication()
{
if (client == null)
{
throw new Exception("The TcpClient is null");
}
else
{
// this doesn't work
byte[] magicByte = SharedConstants.ServerInfo.MagicByte;
// this works
magicByte = UnnamedGameServer.SharedConstants.ServerInfo.MagicByte;
}
}
private void SendPacket(ActionType actionType, object packetStruct)
{
try
{
header.ActionTypeNumber = (short)actionType;
using (NetworkStream stream = client.GetStream())
{
stream.Write(magicByte, 0, magicByte.Length);
Serializer.SerializeWithLengthPrefix<PacketHeader>(stream, header, PrefixStyle.Base128);
switch (actionType)
{
case ActionType.Connect:
Serializer.SerializeWithLengthPrefix<PacketConnect>(stream, (PacketConnect)packetStruct, PrefixStyle.Base128);
break;
}
stream.Write(magicByte, 0, magicByte.Length);
}
}
catch (Exception)
{
// error
return;
}
}
public void CreateNewCharacter(string characterName, CharacterClass chClass, CharacterRace chRace)
{
var info = new NewCharacterInfo();
info.Name = characterName;
info.OwnerUsername = Information.Username;
info.Class = chClass;
info.Race = chRace;
CreateNewCharacter(info);
}
public void CreateNewCharacter(NewCharacterInfo info)
{
var packet = new PacketCreateNewCharacter();
packet.chInfo = info;
SendPacket(ActionType.CreateNewCharacter, packet);
}
public void ConnectUser(string username, string unhashedPassword)
{
var packet = new PacketConnect();
packet.Username = username;
packet.UnhashedPassword = unhashedPassword;
ConnectUser(packet);
}
public void ConnectUser(PacketConnect packet)
{
if (_isLoggedCharacter || _isLoggedUser)
{
return;
}
else
{
SendPacket(ActionType.Connect, packet);
}
}
public void ConnectCharacter(string characterName, short serverID)
{
var packet = new PacketLoginCharacter();
packet.CharacterName = characterName;
packet.ServerID = serverID;
ConnectCharacter(packet);
}
public void ConnectCharacter(PacketLoginCharacter packet)
{
if (_isLoggedCharacter || !_isLoggedUser)
{
return;
}
else
{
SendPacket(ActionType.LoginCharacter, packet);
}
}
public void Disconnect(PacketDisconnect packet)
{
if (!_isLoggedUser)
{
return;
}
else
{
SendPacket(ActionType.Disconnect, packet);
}
}
}
}
编辑3::存储SharedConstants的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace UnnamedGameServer
{
/// <summary>
/// ALL CONSTANTS THAT ARE SHARED BY THE CLIENT AND THE SERVER ARE STORED HERE. DONT ADD MORE CLASSES THAT STORE CONSTANTS. Use GameConstants for game-only constants.
/// </summary>
public static class SharedConstants
{
/// <summary>
/// Server information with port, IP and MagicByte
/// </summary>
public static class ServerInfo
{
public const short Port = 6483;
public static readonly IPAddress ServerAddress = IPAddress.Loopback;
public static byte[] MagicByte
{
get
{
return new byte[4] { 0xF1, 0x64, 0x83, 0xC4 };
}
}
}
/// <summary>
/// Character constants shared by client/server
/// </summary>
public static class CharacterConstants
{
public static class Movement
{
/// <summary>
/// Coordinates per update
/// </summary>
public const float DefaultCoordinatePerUpdate = 8;
public const float ModifierNormal = 1f;
public const float ModifierFrozen = 0.8f;
public const float ModifierSpeedBuff = 1.2f;
public const float ModifierStop = 0f;
}
}
/// <summary>
/// Networking constants
/// </summary>
public static class Networking
{
public const int MilisecondsPerMovementUpdate = 100;
public const ushort ActionTypeNonMetaActionStart = 0x0FFF + 1;
/// <summary>
/// What is the number of actions a non-logged user can perform
/// </summary>
public const ushort ActionTypeNonLoggedUser = 0x000F;
}
}
enum CharacterDirection
{
NoMovement = 0x00,
Top = 0x01,
TopRight = 0x02,
TopLeft = 0x03,
Right = 0x04,
BottomRight = 0x05,
Bottom = 0x06,
BottomLeft = 0x07,
Left = 0x08
}
enum CharacterStatus
{
Alive = 0x01,
Dead = 0x02
}
enum CharacterClass
{
Mage = 0x01,
Knight = 0x02
}
enum CharacterRace
{
Human = 0x01
}
enum CharacterType
{
Player = 0x01,
GameMaster = 0x02
}
enum CharacterFaction
{
Newbie = 0x01,
Army = 0x02,
Chaos = 0x03
}
enum CharacterMovementStatus
{
Normal = 0x01,
Frozen = 0x02,
SpeedBuff = 0x03,
Stop = 0x04
}
struct CharacterExperience
{
public CharacterExperience(short level, int experience)
{
Level = level;
Experience = experience;
}
public short Level;
public int Experience;
}
}
最佳答案
拥有using语句应该起作用。如果using语句之前有任何代码,则可能会导致问题。
您可以尝试将using语句放在 namespace 之后,例如:
namespace Unnamed_game
{
using UnamedGameServer;
class Connection
关于c# - C#编译器将静态类解释为 namespace ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11444383/