[英]Why I can not use a struct that is field of a class in a fixed statement?
在這段代碼中,我想從MyStruct類型創建一個指針,但是編譯器向我顯示此錯誤:“您不能使用給定表達式的地址”。 這是我的代碼:
unsafe class Program
{
static void Main(string[] args)
{
Test test = new Test();
//Error: You cannot take the address of given expression
fixed (MyStruct* ms = &test.MyStruct)
{
}
fixed (int* a = &test.a) //Is OK
{
}
}
}
unsafe class Test
{
public int a;
public MyStruct MyStruct { get; set; } = new MyStruct();
}
unsafe struct MyStruct
{
public int A;
public fixed int Ids[5];
public int B;
}
我不明白為什么會顯示此錯誤。 一切看起來還好嗎? 誰能解釋這段代碼會發生什么?
您不能在固定語句的上下文中使用屬性。 如果將MyStruct
屬性變成一個字段,它將起作用。
unsafe class Test
{
public int a;
public MyStruct MyStruct = new MyStruct();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.