- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码会导致编译器在属性 getter 的第一行抛出错误 CS1605(“无法将 'var' 作为 ref 或 out 参数传递,因为它是只读的”)。
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly struct MyStruct
{
public readonly int Field1;
public readonly int Field2;
public MyStruct(int field1, int field2) => (Field1, Field2) = (field1, field2);
public ReadOnlySpan<byte> Span
{
get
{
// This code only works when MyStruct is not read only
ReadOnlySpan<MyStruct> temp = MemoryMarshal.CreateReadOnlySpan(ref this, 1);
return MemoryMarshal.Cast<MyStruct, byte>(temp);
}
}
}
删除readonly
来自public readonly struct MyStruct
line 使代码可以工作,但出于性能原因,我真的希望该结构为 readonly
。它使代码比必须将结构传递为 ref
更加干净。一直以来。
有没有办法获得 ReadOnlySpan<byte>
来自只读结构?
编辑:...不创建结构的隐式或显式副本?
最佳答案
看起来这可行:
// The following code will work from C# 7.3 and up, no unsafe keyword required
Span<MyStruct> span = stackalloc MyStruct[1];
span[0] = new MyStruct(3, 4);
var bytes = MemoryMarshal.Cast<MyStruct, byte>(span);
如果我们想将其公开为属性,我们可以尝试以下操作:
// Will not work at runtime
public ReadOnlySpan<byte> Span
{
get
{
unsafe
{
fixed (MyStruct* ptr = &this)
{
return new ReadOnlySpan<byte>(ptr, sizeof(MyStruct)); // If on the heap, we're doomed as returning will unpin the memory.
}
}
}
}
并将该结构标记为只读引用结构
,这再次保护了我们堆上的结构。它可以编译,但不会运行,因为您在运行时收到 AccessViolationException
。我会做更多的挖掘,看看是否可能,这在逻辑上应该是安全的,但今天可能不可能。
另一个折衷的解决方案是将其保留为只读结构
(而不是ref struct
)并添加此静态方法:
public static unsafe ReadOnlySpan<byte> GetSpan(ref MyStruct myStruct)
{
return new ReadOnlySpan<byte>(Unsafe.AsPointer(ref myStruct), sizeof(MyStruct));
}
然后从调用代码:
var myStruct = new MyStruct(1, 2);
var span = MyStruct.GetSpan(ref myStruct);
我们可以通过将其移出到引用扩展方法中来改进它的使用(C# 7.2 功能):
class Program
{
static void Main()
{
var myStruct = new MyStruct(1, 2);
var span = myStruct.GetSpan();
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly struct MyStruct
{
public readonly int Field1;
public readonly int Field2;
public MyStruct(int field1, int field2) => (Field1, Field2) = (field1, field2);
}
public static class MyStructExtensions
{
public static unsafe ReadOnlySpan<byte> GetSpan(ref this MyStruct myStruct)
{
return new ReadOnlySpan<byte>(Unsafe.AsPointer(ref myStruct), sizeof(MyStruct));
}
}
关于c# - 如何从只读结构中获取 ReadOnlySpan<byte> ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59020436/
呃......微软已经做了一些事情。 过去几个小时(大约 10:00 GMT)启动的所有构建都因以下错误(来自日志)而因“构建解决方案步骤”而失败: ##[error]EXEC(0,0): Error
我尝试理解 .net core 3.0。据我所知,我不能将 Span 或 ReadonlySpan 用作属性或成员,因为它是基于堆栈的结构。我想知道以下定义之间的区别。我可以成功定义“part1”而不
好的,.NET Core 2.1 已经落地。有了它,我们就得到了一种处理字符串数据的新方法 ReadOnlySpan 。它非常适合分割字符串数据,但是如何将跨度重新组合在一起呢? var hello
以下代码会导致编译器在属性 getter 的第一行抛出错误 CS1605(“无法将 'var' 作为 ref 或 out 参数传递,因为它是只读的”)。 [StructLayout(LayoutKin
我有一个相当简单的字符串扩展方法,该方法在我拥有的系统中被频繁调用,该方法正在执行大量字符串操作。我读了这篇文章 ( String.Substring() seems to bottleneck th
我正在使用ReadOnlySpan,我想亲自看看它比使用字符串快得多,但是......到目前为止,情况并非如此。我知道我的代码中可能犯了一个错误,但我找不到它。 static int CountCha
ReadOnlySpan据说非常适合解析,所以我尝试使用它,但遇到了一个我不知道如何处理的用例。 我有一个命令行 string其中参数前缀 -和分隔符 (空格)被转义了(我知道我可以在这里引用它们但是
引入了 C# 7.2 reference semantics with value-types ,除此之外,微软还开发了类似 Span and ReadOnlySpan 的类型潜在地提高需要在连续内存
假设我有如下方法: unsafe void Convert(byte* ptr, int length) { var span = new Span(ptr, length); var
在解析 int 时数据来自 string , 有没有办法使用 ReadOnlySpan ?喜欢int.Parse(str.AsSpan().Slice(2,3)) 因为如果 int 数据在字符串的中间
我理解为什么 ReadOnlySpan 不能用作泛型类的类型参数。 ReadOnlySpan 只是堆栈,因此它不能用作字段类型,字段成员像它的容器对象一样存在于堆中。然而,返回值和参数始终只是堆栈,那
我的类(class)有一个属性 public byte[] Location{get;} = new byte[30];我希望能够从 ReadOnlySpan 填充它但我找不到任何允许这样做的 API
Microsoft 在 Write safe and efficient C# code 中推荐: Apply the in modifier to readonly struct parameter
如果我已经只有 ReadOnlySpan 切片,那么连接字符串的最有效方法是什么? 简化示例: public class Program { public string ConcatSpans
我正在尝试尽可能快地解析二进制文件。所以这是我第一次尝试做的: using (FileStream filestream = path.OpenRead()) { using (var d =
我想知道当两者都ReadOnlySpan时是否有任何界面、模式或其他什么东西和 IReadOnlyList (以及更通用的接口(interface)),并且您希望避免无用的分配 . 考虑使用 IEnu
我正在寻找一种高效、免分配(!)的实现 public static int IndexOf(this ReadOnlySpan utf8Bytes, char @char) { // Shoul
我正在 Fsharp 中创建 RSA pki 并尝试加载私钥: let getRsa () = let rsa = RSA.Create() let privateKey = ge
我正在 Fsharp 中创建 RSA pki 并尝试加载私钥: let getRsa () = let rsa = RSA.Create() let privateKey = ge
我使用的是 Visual Studio 15.5.6 版本。 当我创建一个简单的控制台应用程序项目时,我无法使用 ReadOnlySpan或 Span或 Memory . 我将项目设置为 .Net F
我是一名优秀的程序员,十分优秀!