C#中的#include指令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2134017/
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
#include directive in C#
提问by Rom C.
Is there a replacement? If there is, how would the directive look for a file named "class.cs"? I just want to split the code into a file for each class.
有替代品吗?如果存在,该指令将如何查找名为“class.cs”的文件?我只想将代码拆分为每个类的文件。
回答by hunter
check out the usingstatement
查看using语句
回答by David M
No, there is no replacement for an #include statement. C# is an object-oriented language where code is organised into classes. You can use code from one class in another class depending on its visibility, and you can split the code from a single class across multiple source files using partial classes. These are essentially the way you use code from one "file" in another. But it's not the same thing at all.
不,#include 语句没有替代品。C# 是一种面向对象的语言,其中代码被组织成类。您可以根据其可见性在另一个类中使用来自一个类的代码,并且您可以使用分部类将来自单个类的代码拆分到多个源文件中。这些本质上是您在另一个“文件”中使用代码的方式。但这根本不是一回事。
回答by Klaus Byskov Pedersen
It's a little unclear what you mean. But are you thinking about:
有点不清楚你的意思。但是你有没有想过:
using MyNamespace;
回答by JaredPar
Unlike C or C++ in C# there is no need to have a #include to use types defined in other files. C# instead does type resolution based on containers such as classes or namespaces. As long as both files are included in the compilation and the namespace of the second type is used in is available then your class will be accessible.
与 C# 中的 C 或 C++ 不同,无需 #include 即可使用其他文件中定义的类型。C# 而是基于容器(如类或命名空间)进行类型解析。只要这两个文件都包含在编译中并且使用了第二种类型的命名空间,那么您的类就可以访问了。
Example:
例子:
Class1.cs
Class1.cs
namespace Project1 {
class Class1 { ... }
}
Class2.cs
Class2.cs
namespace Project1 {
class Class2 {
private Class1 m_field1;
..
}
}
回答by krohrbaugh
It's not exactly like C's #include directive, but C#'s using statementis what you're after:
它并不完全像 C 的 #include 指令,但 C# 的using 语句是您所追求的:
using Assembly.Name;
It works at a namespace level, rather than a file level. So, if class.cs
includes a public class named SomeClass
in the Application.Core
namespace, this would look like:
它在命名空间级别而不是文件级别工作。因此,如果class.cs
包含SomeClass
在Application.Core
命名空间中命名的公共类,则如下所示:
using Application.Core;
This is typically placed at the top of the file you are working in and would allow that class to use SomeClass
as an object (along with all other public classes in the Application.Core
namespace).
这通常放置在您正在处理的文件的顶部,并允许该类SomeClass
用作对象(以及Application.Core
命名空间中的所有其他公共类)。
Of course, if the classes are all in the same namespace (e.g. Application.Core
) there's no reason to employ the using
statement at all. Classes within the same namespace can resolve each other without any declarations.
当然,如果类都在同一个命名空间中(例如Application.Core
),则根本没有理由使用该using
语句。同一命名空间内的类可以在没有任何声明的情况下相互解析。
回答by tkyle
You would need to place the contents of class.cs into a namespace. And then put a using statement at the top of the file that needs to see class.cs.
您需要将 class.cs 的内容放入命名空间。然后在需要查看class.cs的文件顶部放一个using语句。
class.cs
namespace Class {
//class.cs stuff
}
Then do the following in the file that needs class.
然后在需要类的文件中执行以下操作。
using Class;
回答by Gabriel Magana
Also don't forget C# partial classeshave some functionality that you might otherwise get with #include statements.
另外不要忘记 C#部分类有一些功能,否则您可能会通过 #include 语句获得这些功能。
Partial classes allow you to split a class definition over several files.
部分类允许您将类定义拆分为多个文件。
回答by Alex LE
You include each file in the *.csproj if you are using msbuild or in the csc (C# Compiler) command line:
如果您使用 msbuild 或在 csc(C# 编译器)命令行中,则将每个文件包含在 *.csproj 中:
csc File1.cs File2.cs
http://msdn.microsoft.com/en-us/library/78f4aasd%28VS.80%29.aspx
http://msdn.microsoft.com/en-us/library/78f4aasd%28VS.80%29.aspx
回答by Dai
The idiomatic way to achieve metaprogramming in C# (beyond Generics) is with T4 templates - Visual Studio and MSBuild supports T4 built-in, however VS does not come with T4 syntax coloring - you'll need a third-party add-in for that.
在 C# 中实现元编程(超越泛型)的惯用方法是使用 T4 模板——Visual Studio 和 MSBuild 支持内置的 T4,但是 VS 没有 T4 语法着色——你需要一个第三方加载项.
In order to demonstrate T4's include
functionality, I'll use the scenario of wanting to add an ==
operator overload to multiple classes simultaneously without using inheritance.
为了演示 T4 的include
功能,我将使用想要在==
不使用继承的情况下同时向多个类添加运算符重载的场景。
For comparison, in C++ it would be like this:
为了比较,在 C++ 中它会是这样的:
OperatorEquals.inc
OperatorEquals.inc
bool operator==(const TYPE* lhs, const TYPE* rhs) { if( lhs == nullptr && rhs != nullptr ) return false; return lhs.Equals(rhs); }
Code.h
代码.h
class Foo {
public:
#define TYPE Foo
#include "OperatorEquals.inc"
}
class Bar {
public:
#define TYPE Bar
#include "OperatorEquals.inc"
}
In C#, you would do this:
在 C# 中,你会这样做:
- Use
partial
classes so that all of your non-metaprogramming logic (i.e. normal C# code) is in a file, e.g.Foo.cs
andBar.cs
- Create a new T4 template in your project, change the output file extension to
.cs
- Create a second
partial class
definition of the same type within that T4 (*.tt
) file, though you won't have C# syntax highlighting. - Define the included file:
- 使用
partial
类,以便所有非元编程逻辑(即普通的 C# 代码)都在一个文件中,例如Foo.cs
和Bar.cs
- 在您的项目中创建一个新的 T4 模板,将输出文件扩展名更改为
.cs
partial class
在该 T4 (*.tt
) 文件中创建相同类型的第二个定义,但不会突出显示 C# 语法。- 定义包含的文件:
Operators.inc.cs.t4
Operators.inc.cs.t4
public static operator==(<#= typeName #> x, <#= typeName #> y) {
if( x == null && y != null ) return false;
return x.Equals( y );
}
- Add it to your T4 template:
- 将其添加到您的 T4 模板中:
Metaprogramming.tt
元编程.tt
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ import namespace="System" #>
<#@ output extension=".cs" #>
<# String typeName = null; #>
public partial class Foo {
<# typeName = "Foo"; #>
<#@ include file="Operators.inc.cs.t4" #>
}
public partial class Bar {
<# typeName = "Bar"; #>
<#@ include file="Operators.inc.cs.t4" #>
}
Whenever you "Save" the .tt
file (even if you make no changes) VS will regenerate the output .cs
file which will look like this:
每当您“保存”.tt
文件时(即使您没有进行任何更改),VS 都会重新生成如下所示的输出.cs
文件:
public partial class Foo {
public static operator==(Foo x, Foo y) {
if( x == null && y != null ) return false;
return x.Equals( y );
}
}
public partial class Bar {
public static operator==(Bar x, Bar y) {
if( x == null && y != null ) return false;
return x.Equals( y );
}
}
Note that this scenario is contrived - if you really did want to add the operator==
(and all the others: IEquatable<T>
, operator!=
, IComparable<T>
, etc) then you would probably use a T4 render function instead of an include, because that makes parameterization more straightforward and keeps everything self-contained in a single file:
请注意,这个场景很做作-如果你真的想要添加的operator==
(和所有其他人:IEquatable<T>
,operator!=
,IComparable<T>
,等),那么你可能会使用一个T4渲染功能而非包括,因为这使得参数更简单,使一切自我- 包含在单个文件中:
T4RenderFunction.tt
T4RenderFunction.tt
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ import namespace="System" #>
<#@ output extension=".cs" #>
<# String typeName = null; #>
public partial class Foo {
<# RenderBoilerplateOperators("Foo"); #>
}
public partial class Bar {
<# RenderBoilerplateOperators("Bar"); #>
}
<#+
// Functions are declared at the bottom
void RenderBoilerplateOperators(String typeName) {
#>
public static operator==(<#= typeName #> lhs, <#= typeName #> rhs) {
return <#= typeName #>.Equals( lhs, rhs );
}
public override Boolean Equals(<#= typeName #> other) {
return <#= typeName #>.Equals( this, other );
}
public static Boolean Equals(<#= typeName #> lhs, <#= typeName #> rhs) {
// T4 can use VS DTE to enumerate members of `typeName`, but you're probably better-off implementing this method manually
}
public static operator!=(<#= typeName #> lhs, <#= typeName #> rhs) {
return !<#= typeName #>.Equals( lhs, rhs );
}
// and so on...
<#
} // void RenderBoilerplateOperators
#>
回答by ewwink
an example using Partial Class
.
一个使用Partial Class
.
Main.cs
Main.cs
partial class Program
{
private static void Main()
{
A();
B();
}
}
fileA.cs
fileA.cs
partial class Program
{
private static void A() => Console.WriteLine("A");
}
fileB.cs
fileB.cs
partial class Program
{
private static void B() => Console.WriteLine("B");
}