C#编译器将静态类解释为命名空间

时间:2012-07-12 02:48:07

标签: c# visual-studio-2010 compilation compiler-errors

我有一个非常奇怪的问题。我尝试了一切来解决它,到目前为止没有任何作用。

正如您在第一个图像中看到的,当我尝试从“UnnamedGameServer”之外的命名空间引用静态类“SharedConstants”时,编译器返回以下错误:

  

类型或命名空间名称'ServerInfo'不存在于   命名空间'SharedConstants'

我发现使用UnnamedGameServer.SharedConstants而不是SharedConstants并使用UnnamedGameServer引用此类的问题出现了转机;在.cs文件的顶部。但我更喜欢避免在我使用该类的每一行上引用它。

以下是我的代码的一些截图:

第一个截图:

Bug in Visual Studio

第二个截图:

Bug in Visual Studio

编辑:使用声明的屏幕截图:

Bug in Visual Studio

编辑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;
    }
}

2 个答案:

答案 0 :(得分:2)

使用using语句应该有效。如果在using语句之前有任何代码,那么这可能会导致问题。

你可以尝试在命名空间之后放置using语句,例如:

namespace Unnamed_game
{
    using UnamedGameServer;
    class Connection

答案 1 :(得分:0)

这应该有效:

using UnnamedGameServer;

class MyClass {
byte[] b = SharedConstants.ServerInfo.MagicByte;
}

如果这不能解决您的问题,请发布一个显示问题的简短但完整示例的代码(文字,而不是屏幕截图)。