Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将简单的.cs文件合并到一个.cs文件中,并在顶部使用所有用法,以便编译_C#_Powershell - Fatal编程技术网

C# 如何将简单的.cs文件合并到一个.cs文件中,并在顶部使用所有用法,以便编译

C# 如何将简单的.cs文件合并到一个.cs文件中,并在顶部使用所有用法,以便编译,c#,powershell,C#,Powershell,所以我知道,总的来说,这是不可能的,因为 但是我的.cs文件是简单的类,顶部有一个或两个using。我需要一个包含所有类的文件,这样我就可以将它作为单个文件粘贴到web浏览器IDE中进行编译和运行 我尝试使用PowerShell,只需将所有文件复制到一个文件中,如下所示: get-content *.cs | out-file bigBadFile.cs 但是这个文件不会编译,因为它在中间使用,这是不允许的: CS1529:using子句必须位于命名空间中定义的所有其他元素之前,外部别名声明除

所以我知道,总的来说,这是不可能的,因为

但是我的.cs文件是简单的类,顶部有一个或两个using。我需要一个包含所有类的文件,这样我就可以将它作为单个文件粘贴到web浏览器IDE中进行编译和运行

我尝试使用PowerShell,只需将所有文件复制到一个文件中,如下所示:

get-content *.cs | out-file bigBadFile.cs
但是这个文件不会编译,因为它在中间使用,这是不允许的:

CS1529:using子句必须位于命名空间中定义的所有其他元素之前,外部别名声明除外

如果你想知道我为什么需要它,那是为了CodinGame平台,我讨厌把我所有的代码都保存在一个文件中

要合并的示例文件:

GameData.cs:

using System.Collections.Generic;

public class GameData
{
    public GameData(int width, int height)
    {
       ...
    }

    public int Width { get; set; }
    public int Height { get; set; }
    public List<int> List { get; set; }
    ...
}

我终于做到了。那些PowerShell命令对我很有用:

get-content *.cs | where { $_ -match "^using" } | Select-Object -Unique | out-file bigBadFile.txt
get-content *.cs | where { $_ -notmatch "^using" } | out-file -append bigBadFile.txt
所以我在这里做的是从所有文件中获取所有用法,并将它们放入bigBadFile.txt。然后我从所有文件中获取所有不使用的代码,并将其附加到bigBadFile.txt

结果对我来说是有效的,尽管它使用了重复的语句。我按照西奥在评论中的建议添加了
| Select Object-Unique
,以避免重复使用


-match
之后,大括号内的代码
“^using”
只是一个正则表达式,因此如果您的使用在.cs文件中前面有空格(这是不寻常的,您可以使用“

将其更改为
”^[]*最后我成功地做到了这一点。那些PowerShell命令对我来说很有用:

get-content *.cs | where { $_ -match "^using" } | Select-Object -Unique | out-file bigBadFile.txt
get-content *.cs | where { $_ -notmatch "^using" } | out-file -append bigBadFile.txt
所以我在这里做的是从所有文件中获取所有using并将它们放入bigBadFile.txt中。然后从所有文件中获取所有不使用using的代码,并将其附加到bigBadFile.txt中

结果对我来说是有效的,尽管它使用语句进行了复制。正如西奥在评论中建议的那样,我添加了
| Select Object-Unique
,以避免使用重复


-match
之后,大括号内的代码
“^using”
只是一个正则表达式,因此如果您的使用在.cs文件中前面有空格(这是不寻常的,您可以将其更改为
”^[]*using“

只是为了提供一个更简洁、更快速的PSv4+替代方案


只是为了提供一个更简洁、更快速的PSv4+替代方案:

$usings, $rest = (Get-Content *.cs).Where({ $_ -match '^\s*using\s' }, 'Split')

# Encoding note: Creates a BOM-less UTF-8 file in PowerShell [Core] 6+,
#                and an ANSI file in Windows PowerShell. Use -Encoding as needed.
Set-Content bigBadFile.txt -Value (@($usings | Select-Object -Unique) + $rest)