- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我注意到在部分类的类级别使用 unsafe
修饰符时会出现某种行为,我希望得到一些澄清。
我一直在研究一个相当大的包装器,为了理智起见,我使用 partial
修饰符拆分多个文件。包装器大量使用 unsafe
指针,因此我选择简单地在类级别声明它以覆盖其中的所有内容。
public static unsafe partial class Ruby
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static VALUE CLASS_OF(VALUE obj) => ((RBasic*) obj)->klass;
}
在另一个文件中:
public static unsafe partial class Ruby
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void* DATA_PTR(VALUE obj) => ((RData*) obj)->data;
}
每个部分声明都需要 unsafe
修饰符,以便“允许”和编译不安全代码,这是可以理解的,我希望部分类的类声明需要完全匹配。
但是使用这个逻辑,我也可以拥有另一个不是不安全
的文件:
[SuppressUnmanagedCodeSecurity]
public static partial class Ruby
{
[DllImport(RUBY_LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern VALUE rb_ivar_get(VALUE obj, ID name);
}
在这里,我没有使用 unsafe
修饰符,这是完全可以接受的(显然这个文件中没有不安全的代码)。
我希望得到有关允许这样做的原因的澄清。每个部分类的类声明不应该完全匹配吗?不允许更改/排除任何其他类修饰符,例如 private
、public
、abstract
等,那么为什么可以使用 不安全
。在我看来,这种行为似乎不一致。我的猜测是编译器以某种方式在不同的上下文中运行,但这只是我的猜测,希望比我更了解这方面的人能给出一些启示。
最佳答案
您关于需要一致性的修饰符的断言有点不对。例如,这是完全有效的:
public abstract partial class Foo { }
partial class Foo { }
Foo
将代表一个抽象类,因为它的一部分被声明为抽象类。有关部分类的类修饰符的规则可以在规范的第 10.2.2 节中找到。以下是与 unsafe
分部类相关的文本:
When the unsafe modifier is used on a partial type declaration, only that particular part is considered an unsafe context (§18.1).
当 sealed
或 abstract
应用于分部类时,该类的所有部分都被视为sealed
或 abstract
。但是,它不能两者兼而有之。
事实上,可访问性是部分的唯一类修饰符,必须在所有部分之间保持一致,但不需要在所有部分中设置。
When a partial type declaration includes an accessibility specification (the public, protected, internal, and private modifiers) it must agree with all other parts that include an accessibility specification. If no part of a partial type includes an accessibility specification, the type is given the appropriate default accessibility (§3.5.1).
这基本上归结为不允许同时声明 public partial class Bar
和 internal partial class Bar
,因为可访问性在各部分之间变得不一致。声明为没有可访问性的其他部分将默认为已声明的可访问性,或规范第 3.5.1 节中概述的规则指定的默认值。
注意:我引用的规范部分来自 Visual Studio 2017 附带的规范版本。
ECMA-334引用资料如下:
23.2:
When the unsafe modifier is used on a partial type declaration (§15.2.7), only that particular part is considered an unsafe context.
15.2.2.1:
When a partial type declaration (§15.2.7) includes an accessibility specification (via the public, protected, internal, and private modifiers), that specification shall agree with all other parts that include an accessibility specification. If no part of a partial type includes an accessibility specification, the type is given the appropriate default accessibility (§8.5.2).
15.2.2.2 和 15.2.2.3 有关于 abstract
和 sealed
修饰符的规则。
关于c# - 为什么类级 "unsafe"修饰符在使用 "partial"时不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51939000/
SQL FMDB 的修饰符在哪里描述?特别是,如何在要插入的字符串中放置引号。比如我想将名称“Mac's Place”插入数据库?谢谢 最佳答案 如果在 SQL 中使用 executeUpdate 和
我读过 article在 MSDN 上。它解释了为什么“in”应该只与自定义只读结构一起使用,否则会有性能损失。但是,我不太明白如何对原始类型使用“in”。由于 C# 中的所有内置值类型都是不可变的,
“__printflike__ 修饰符”到底是什么?这个词是什么意思? 最佳答案 据推测,它会告诉编译器您正在使用的函数接受格式为 [anything, ] format, ... 的参数,其中 fo
我在 VS2010 上使用 ReSharper 插件,我正在生成一个接口(interface)方法。ReSharper 在参数名称上放置一个 @。那是做什么用的? int Count(Func @wh
我在写小偷中用了大量的正则,所以在接下来的版本中要逐步减少正则的应用 在PHP正则表达式中需要转义的字符如下: $^*()+={}[]|/:<>.?'" 注意:perl风
http://msdn.microsoft.com/en-us/library/435f1dw2.aspx public class Base { public string Field; }
根据scala-wartremover静态分析工具我必须将“final”放在我创建的每个案例类前面:错误消息显示“案例类必须是最终的”。 根据scapegoat (Scala 的另一个静态分析工具)相
在 tcsh 中,我可以通过以下方式从路径末尾提取第二个路径元素 cd /some/long/directory/structure/path/ set x=`pwd` echo ${x:h:h:t}
我找到了很多 SwiftUI 教程,其中修饰符 .resizable() 用于图像。它似乎不再适用于带有 Swift 5 的 Xcode 13.3.1。 导入了 SwiftUI。如果您自己键入整个修饰
我已经设置好了音频播放器 除了步进器的当前功能外,我还想为 onIncrement 和 onDecrement 播放单独的声音。 这个项目使用核心数据来持久化。 $estimatorData.qty
来自 linux 内核脚本/mod/modpost.c: static int is_vmlinux(const char *modname) { const char *myname;
我刚刚遇到了一个BNF Grammar for JAVA 。其中,“修饰符”有一个称为“threadsafe”的终结符号。但是,我以前从未见过它,也无法在《Java 语言规范》Java SE 7 版
我创建了一个脚本,它将我组中的所有帖子嵌入到我的网站上。为此,我必须知道所有可能的修饰符“附件”类型。 现在我知道,那个类型可以是: 分享, video_share_youtube。 但我不知道在哪里
在 tcsh 中,我可以通过以下方式从路径末尾提取第二个路径元素 cd /some/long/directory/structure/path/ set x=`pwd` echo ${x:h:h:t}
我找到了很多 SwiftUI 教程,其中修饰符 .resizable() 用于图像。它似乎不再适用于带有 Swift 5 的 Xcode 13.3.1。 导入了 SwiftUI。如果您自己键入整个修饰
我正在编写一个代表一些简单几何形状的 Java 类。 最上面abstract类(它本身是包私有(private))我已经声明了需要从同一包中的子类访问的属性。 如果我将属性声明为 final在 Abs
我已经设置好了音频播放器 除了步进器的当前功能外,我还想为 onIncrement 和 onDecrement 播放单独的声音。 这个项目使用核心数据来持久化。 $estimatorData.qty
明确地说,我是 不是 询问如何使用 ViewModifier 协议(protocol)来创建一个带有 body 函数的结构,然后可以使用它来修改 View 。这个问题有点不同。 我正在尝试创建 Nav
我有一个内部类,用于存储我用于游戏的控件的信息,现在我想在其中存储一个静态 ArrayList,其中包含所有控件的名称。但我收到此错误:“修饰符 static 只允许在常量变量声明中” private
public class This_testing { int x,y; public This_testing(int x,int y){ //Why modifier void
我是一名优秀的程序员,十分优秀!