如何将 cs 文件添加到现有的 C# 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/901621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 01:43:51  来源:igfitidea点击:

How do I add a cs file to an existing C# project?

c#visual-studio-2008file

提问by adeena

A while back I wrote a little program in Microsoft Visual C# 2008 Express Edition. In it included a file called "ProblemReport.cs" with it's own form and ProblemReport class.

不久前,我在 Microsoft Visual C# 2008 Express Edition 中编写了一个小程序。其中包含一个名为“ProblemReport.cs”的文件,它有自己的表单和 ProblemReport 类。

I'm writing a new program and want to reuse this code. (still working in MS Vis C# 2008 express)

我正在编写一个新程序并希望重用此代码。(仍在 MS Vis C# 2008 express 中工作)

In my new program, in the C# Solution Explorer, I right clicked on my project and chose "Add existing item..." I then added ProblemReport.cs, ProblemReport.designer.cs and ProblemReport.resx.

在我的新程序中,在 C# 解决方案资源管理器中,我右键单击我的项目并选择“添加现有项目...”,然后我添加了 ProblemReport.cs、ProblemReport.designer.cs 和 ProblemReport.resx。

What step(s) am I missing here? When I go back to the main form in my new program, and try to instantiate a new instance of "ProblemReport", I get the error message:

我在这里遗漏了什么步骤?当我回到新程序中的主窗体并尝试实例化“ProblemReport”的新实例时,我收到错误消息:

"The type of namespace name 'ProblemReport' could not be found (are you missing a using directive or an assembly reference?)"

“找不到命名空间名称‘ProblemReport’的类型(您是否缺少 using 指令或程序集引用?)”

Obviously - I'm supposed to do something else to get this to work... just not sure what it is!

显然 - 我应该做一些其他的事情来让它工作......只是不确定它是什么!

-Adeena

-阿迪娜

采纳答案by p.campbell

Ensure that all your CS files are within the same namespace. Sounds like the imported one(s) aren't living with your new project.

确保所有 CS 文件都在同一个命名空间中。听起来像导入的(S)没有与您的新项目一起生活。

Ensure that:

确保这件事:

namespace MyProgram //same as the rest of your project
{

    public class ProblemReport
    {
    }

}

meanwhile in your other .cs files....

同时在您的其他 .cs 文件中....

namespace MyProgram 
{

     public class SomeClass
     {
           public void DoSomething()
           {
               ProblemReport. //you should get intellisense here.
           }
      }

}

回答by blowdart

When you created it it's likely it used a different namespace, as those are generated from the project name by default.

创建它时,它可能使用了不同的命名空间,因为默认情况下这些命名空间是从项目名称生成的。

In the .CS file does the namespace match that of the rest of your project?

在 .CS 文件中,命名空间是否与项目其余部分的命名空间匹配?

回答by jkottnauer

You have to add a "using" directive to your new project -

您必须在新项目中添加“使用”指令 -

using ProblemReport;

Then you should be able to access the ProblemReport class. Of course this depends on the name of namespace of the ProblemReport.cs file.

然后您应该能够访问 ProblemReport 类。当然,这取决于 ProblemReport.cs 文件的命名空间名称。

回答by ChrisF

You might be better off extracting ProblemReport into it's own class library (separate dll) so you only have one copy of the code.

您最好将 ProblemReport 提取到它自己的类库(单独的 dll)中,这样您就只有一份代码副本。

Create a new class library project, copy ProblemReport.cs, ProblemReport.designer.cs and ProblemReport.resx into that and update the namespace as required. Build it and put the dll somewhere central. Then add a reference to this class library from your two programs.

创建一个新的类库项目,将 ProblemReport.cs、ProblemReport.designer.cs 和 ProblemReport.resx 复制到其中,并根据需要更新命名空间。构建它并将 dll 放在中央的某个地方。然后从您的两个程序中添加对此类库的引用。