我在引用命名空间时遇到问题

I'm having a problem when referencing a namespace

提问人:Artemis 提问时间:12/7/2022 最后编辑:phoArtemis 更新时间:12/8/2022 访问量:90

问:

我将三个脚本从同一个文件夹中分离出来,因为一个脚本干扰了另一个脚本,当引用其中一个脚本中的命名空间时,我收到“缺少使用指令/程序集引用”错误

我尝试运行该命令,但它最终没有做任何事情dotnet add reference

不知道把代码放在哪里,所以...

它不必要地复杂化,但忽略了错误与使用 错误是 Vector3 和使用 thingys

脚本 1

namespace StateCalculator
{
    using System;
    using System.Configuration;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Linq;
    using CustomVariablesAndFunctionsEngine.Vectors; <- Error
    using CustomVariablesAndFunctionsEngine.Math; <- Error

    public class Calculator
    {
        #region Variables

        public CalculatorStateMachine StateMachine { get; private set; }
    
        public Add Add { get; private set; }
        public Subtract Subtract { get; private set; }
        public Multiply Multiply { get; private set; }

        public Divide Divide { get; private set; }
        public Square Square { get; private set; }
        public Sqrt Sqrt { get; private set; }
        public Cube Cube { get; private set; }
        public Cbrt Cbrt { get; private set; }

        #endregion

        #region Functions

        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            c.Awake();
            c.Round1();
        }

        private void Awake()
        {
            StateMachine = new CalculatorStateMachine();

            Add = new Add(this, StateMachine);
            Subtract = new Subtract(this, StateMachine);
            Multiply = new Multiply(this, StateMachine);
            Divide = new Divide(this, StateMachine);
            Square = new Square(this, StateMachine);
            Sqrt = new Sqrt(this, StateMachine);
            Cube = new Cube(this, StateMachine);
            Cbrt = new Cbrt(this, StateMachine);
        }

        private void Round1()
        {
            string YesOrNo;
            int operation;

            Console.WriteLine("This is a simple calculator");
            Console.WriteLine("Would you like to Continue Yes/No");
            YesOrNo = Console.ReadLine().ToLower();

            if(YesOrNo == "yes")
            {
                Console.WriteLine("What operation do you want to use");
                Console.WriteLine("1 : Add");
                Console.WriteLine("2 : Subtract");
                Console.WriteLine("3 : Multiply");
                Console.WriteLine("4 : Divide");
                Console.WriteLine("5 : Square");
                Console.WriteLine("6 : Sqrt");
                Console.WriteLine("7 : Cube");
                Console.WriteLine("8 : Cbrt");
                operation = Convert.ToInt32(Console.ReadLine());

                Which(operation);
            }
            
            else
            {
                Console.WriteLine("Ok, sorry to see you go :(");
            }
        }

        private void Which(int operation)
        {
            if(operation == 1)
                {
                    StateMachine.ChangeOperation(Add);
                }

            else if (operation == 2)
            {
                StateMachine.ChangeOperation(Subtract);
            }

            else if (operation == 3)
            {
                StateMachine.ChangeOperation(Multiply);
            }

            else if (operation == 4)
            {
                StateMachine.ChangeOperation(Divide);
            }

            else if(operation == 5)
            {
                StateMachine.ChangeOperation(Square);
            }

            else if(operation == 6)
            {
                StateMachine.ChangeOperation(Sqrt);
            }

            else if(operation == 7)
            {
                StateMachine.ChangeOperation(Cube);
            }

            else if(operation == 8)
            {
                StateMachine.ChangeOperation(Cbrt);
            }

            else
            {
                Console.WriteLine("Sorry I cant do that");
                Which(operation);
            }
        }

        #endregion
    }

    public class CalculatorStateMachine
    {
        #region Variables

        public Operation CurrentOperation { get; private set; }

        #endregion

        #region Functions

        public void Initialize(Operation startingOperation)
        {
            CurrentOperation = startingOperation;
            CurrentOperation.Enter();
        }

        public void ChangeOperation(Operation newOperation)
        {
            CurrentOperation = newOperation;
            CurrentOperation.Enter();
        }

        #endregion
    }

    public class Operation
    {
        #region  Variables

        public Calculator Calculator { get; protected set; }
        public CalculatorStateMachine StateMachine { get; protected set; }

        protected Vector3 Numbers;

        #endregion

        #region Functions

        public Operation(Calculator calculator, CalculatorStateMachine stateMachine)
        {
            this.Calculator = calculator;
            this.StateMachine = stateMachine;
        }
    
        public virtual void Enter()
        {
            Numbers = new Vector3(0f, 0f, 0f);
            SetVariables();
            Calculate(this.Numbers);
        }

        public virtual void SetVariables()
        {

        }

        public virtual void Calculate(Vector3 Numbers)
        {
            
        }

        #endregion
    }

    public class Add : Operation
    {
        public Add(Calculator calculator, CalculatorStateMachine stateMachine) : base(calculator, stateMachine)
        {

        }

        public override void Enter()
        {
            base.Enter();
        }

        public override void Calculate(Vector3 Numbers)
        {
            base.Calculate(Numbers);
            Numbers.z = Numbers.x + Numbers.y;
            Console.WriteLine("Your answer is " + Convert.ToString(Numbers.z));
        }

        public override void SetVariables()
        {
            base.SetVariables();
            
            Console.WriteLine("First Number");
            Numbers.x = Convert.ToSingle(Console.ReadLine());
            Console.WriteLine("Second Number");
            Numbers.y = Convert.ToSingle(Console.ReadLine());
        }
    }

    public class Subtract : Operation
    {
        public Subtract(Calculator calculator, CalculatorStateMachine stateMachine) : base(calculator, stateMachine)
        {

        }

        public override void Enter()
        {
            base.Enter();
        }

        public override void Calculate(Vector3 Numbers)
        {
            base.Calculate(Numbers);
            Numbers.z = Numbers.x - Numbers.y;
            Console.WriteLine("Your answer is " + Convert.ToString(Numbers.z));
        }

        public override void SetVariables()
        {
            base.SetVariables();
            
            Console.WriteLine("First Number");
            Numbers.x = Convert.ToSingle(Console.ReadLine());
            Console.WriteLine("Second Number");
            Numbers.y = Convert.ToSingle(Console.ReadLine());
        }
    }

    public class Multiply : Operation
    {
        public Multiply(Calculator calculator, CalculatorStateMachine stateMachine) : base(calculator, stateMachine)
        {

        }

        public override void Enter()
        {
            base.Enter();
        }

        public override void Calculate(Vector3 Numbers)
        {
            base.Calculate(Numbers);
            Numbers.z = Numbers.x * Numbers.y;
            Console.WriteLine("Your answer is " + Convert.ToString(Numbers.z));
        }

        public override void SetVariables()
        {
            base.SetVariables();
            
            Console.WriteLine("First Number");
            Numbers.x = Convert.ToSingle(Console.ReadLine());
            Console.WriteLine("Second Number");
            Numbers.y = Convert.ToSingle(Console.ReadLine());
        }
    }

    public class Divide : Operation
    {
        public Divide(Calculator calculator, CalculatorStateMachine stateMachine) : base(calculator, stateMachine)
        {

        }

        public override void Enter()
        {
            base.Enter();
        }

        public override void Calculate(Vector3 Numbers)
        {
            base.Calculate(Numbers);
            Numbers.z = Numbers.x / Numbers.y;
            Console.WriteLine("Your answer is " + Convert.ToString(Numbers.z));
        }

        public override void SetVariables()
        {
            base.SetVariables();
            
            Console.WriteLine("First Number");
            Numbers.x = Convert.ToSingle(Console.ReadLine());
            Console.WriteLine("Second Number");
            Numbers.y = Convert.ToSingle(Console.ReadLine());
        }
    }

    public class Square : Operation
    {
        public Square(Calculator calculator, CalculatorStateMachine stateMachine) : base(calculator, stateMachine)
        {

        }

        public override void Enter()
        {
            base.Enter();
        }

        public override void Calculate(Vector3 Numbers)
        {
            base.Calculate(Numbers);
            Numbers.z = MathF.Pow(Numbers.x, 2f);
            Console.WriteLine("Your answer is " + Convert.ToString(Numbers.z));
        }

        public override void SetVariables()
        {
            base.SetVariables();
            
            Console.WriteLine("Number");
            Numbers.x = Convert.ToSingle(Console.ReadLine());
        }
    }
    
    public class Sqrt : Operation
    {
        public Sqrt(Calculator calculator, CalculatorStateMachine stateMachine) : base(calculator, stateMachine)
        {

        }

        public override void Enter()
        {
            base.Enter();
        }

        public override void Calculate(Vector3 Numbers)
        {
            base.Calculate(Numbers);
            Numbers.z = MathF.Sqrt(Numbers.x);
            Console.WriteLine("Your answer is " + Convert.ToString(Numbers.z));
        }

        public override void SetVariables()
        {
            base.SetVariables();
        
            Console.WriteLine("Number");
            Numbers.x = Convert.ToSingle(Console.ReadLine());
        }
    }

    public class Cube : Operation
    {
        public Cube(Calculator calculator, CalculatorStateMachine stateMachine) : base(calculator, stateMachine)
        {

        }

        public override void Enter()
        {
            base.Enter();
        }

        public override void Calculate(Vector3 Numbers)
        {
            base.Calculate(Numbers);
            Numbers.z = MathF.Pow(Numbers.x, 3f);
            Console.WriteLine("Your answer is " + Convert.ToString(Numbers.z));
        }

        public override void SetVariables()
        {
            base.SetVariables();
        
            Console.WriteLine("Number");
            Numbers.x = Convert.ToSingle(Console.ReadLine());
        }
    }  

    public class Cbrt : Operation
    {
        public Cbrt(Calculator calculator, CalculatorStateMachine stateMachine) : base(calculator, stateMachine)
        {

        }

        public override void Enter()
        {
            base.Enter();
        }

        public override void Calculate(Vector3 Numbers)
        {
            base.Calculate(Numbers);
            Numbers.z = MathF.Cbrt(Numbers.x);
            Console.WriteLine("Your answer is " + Convert.ToString(Numbers.z));
        }

        public override void SetVariables()
        {
            base.SetVariables();
        
            Console.WriteLine("Number");
            Numbers.x = Convert.ToSingle(Console.ReadLine());
        }
    } 

}
C# 语法错误

评论

4赞 johnny 5 12/7/2022
usings 应该在命名空间之外,不是吗?
2赞 steven01804 12/7/2022
您是否正在尝试引用其他项目?
2赞 Pac0 12/7/2022
如果是同一解决方案中的另一个项目,请在 Visual Studio 中右键单击项目并选择“添加引用”,然后选择其他项目。
3赞 gunr2171 12/7/2022
您似乎发布了比您的问题合理的代码更多的代码。请阅读如何提问以及如何制作一个最小的可重复示例;提供 MRE 有助于用户回答您的问题,并帮助未来的用户与您的问题相关。
3赞 Guru Stron 12/7/2022
@johnny5不,实际上有些人更喜欢将它们放在命名空间中 - 请参阅此处的原因。

答:

1赞 Artemis 12/8/2022 #1

我通过将代码传输到 Visual Studio 控制台应用程序,然后在解决方案中添加 using 指令作为引用来修复它