使用 Visual Studio 2017 构建 SSDT 项目时如何将 /unsafe 标志传递给 C# 编译器

雷切尔·安布勒

前缀:请不要纠缠在should I do this,也不要被支持。MicrosoftUnsafe正是为此目的提供了权限集

使用 PERMISSION_SET = UNSAFE 创建的 CLR 程序集可能能够访问外部系统资源、调用非托管代码并获取系统管理员权限。

https://docs.microsoft.com/en-us/sql/t-sql/statements/create-assembly-transact-sql

现在,进入问题:

因此,传统观点认为,当人们希望使用不安全的代码块时,例如

  unsafe {
     fixed (char* passwordChars = password) {
        var securePassword = new SecureString(passwordChars, password.Length);
        securePassword.MakeReadOnly();
        return securePassword;
     }
  }

只需选中Build选项中的复选框并关闭一个小跑:

CSProj 构建选项

然而,当一个人在构建一个 SSDTSqlProj项目时,我完全不明白人们是如何做到的这是该项目类型的构建 ( SQLCLR Build ) 选项:

SqlProj 构建选项

那么,除了通常的“危险,危险,罗宾逊”类型的警告之外,这甚至可以用于SqlProj项目类型吗?是的,我正在构建不安全的代码,是的,我正在签署代码,使用非对称密钥并使用Unsafe附加权限登录

抛开这些不谈,你到底是怎么把它传递到编译器命令行的?另外,sqlproj如果需要修改文件,我也不反对

想法

If I have to I suppose I might be able create another assembly, outside of the SqlProj format with this code in and load that to Sql Server as an assembly that I then reference. It just seems odd that the SqlProj project format specifically prohibits this flag being set.

Jeroen Mostert

The UI will not expose the option, but the project file still supports it (along with any other option you can pass to the compiler).

In VS, right-click the project and choose "Unload Project", then right-click again and choose "Edit project". In the main <PropertyGroup> block, add

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

Save, reload the project once more, and it should now build unsafe blocks without complaint.

请注意,PERMISSION_SET = UNSAFE这比 C# 所说的“不安全”要广泛得多。即使在经过验证的托管代码中做一些可能会损害引擎的稳定性或吞吐量(如线程)的PERMISSION_SET = SAFE事情也是被禁止的(在代码访问安全性被放弃并使事情复杂化之前)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何防止Visual Studio 2017构建JavaScript?

如何在Visual Studio 2017中使用CMake设置编译器选项

是否有与Visual Studio C ++编译器等效的-ffast-math标志

如何在Windows下使用cmake生成使用Intel编译器的Visual Studio项目

使用Visual C ++编译器在Windows上构建库时,如何正确设置目标OS版本

如何在Visual Studio 2015中使用roslyn C#编译器?

如何在TFS Visual Studio构建步骤中使用Visual Studio 2017

如何使用MSBuild构建Visual Studio 2017 VSIX

如何使用Visual Studio 2017在Windows上构建OpenSSL?

Visual Studio 2017找不到打字稿编译器?

使用Visual Studio编译器分析内联的C ++函数

使用MSBuild在VS2017中构建SSDT项目失败

如何使用Visual Studio编译器使用cgo构建库?

VS 2017使用交叉编译器构建x64项目

Roslyn是Visual Studio 2017的默认编译器吗

如何在Dymola 2019 FD01中为Visual Studio编译器利用编译器标志

使用Visual Studio 2017创建CS2PHP编译器

CMake:将条件编译器标志添加到Visual Studio项目中

在Bazel中,如何防止某些C ++编译器标志传递给外部依赖项?

使用g ++编译器在Visual Studio中编译C ++程序会在最后生成%

在Visual Studio / C ++中使用fbx SDK构建项目时编译错误

使用Visual Studio 2017的编译器在C ++ 17中使用Lambda编译Kevlin Henney的Fizzbuzz

C ++:使用Visual Studio编译器时的const指针

如何使用Visual c ++编译器为Boost 1.39.0构建库运行Cmake

编译器无法在使用cmake配置的Visual Studio项目中执行C ++ 11指令

在Visual Studio中构建解决方案时使用哪个编译器?

Visual Studio (SSDT) 2017 无法启动调试

如何通过批处理文件使用 MsBuild 和 VS2017 编译器构建 C#、C++、C 解决方案?

如何使用 Visual Studio 编译器在 Windows 上编译 cython 编译的 c 代码