Store a reference to a value type?

前端 未结 5 1195
情书的邮戳
情书的邮戳 2020-11-29 04:51

I am writing a \"Monitor\" object to facilitate debugging of my app. This Monitor object can be accessed at run time from an IronPython interpreter. My question is, is it po

5条回答
  •  离开以前
    2020-11-29 05:35

    You can literally take a pointer to a value type using usafe code

    public class Foo
    {
        public int a;
    }
    
    unsafe static class Program
    {
        static void Main(string[] args)
        {
            var f=new Foo() { a=1 };
            // f.a = 1
    
            fixed(int* ptr=&f.a)
            {
                *ptr=2;
            }
            // f.a = 2
        }
    }
    

提交回复
热议问题