How to mutate a boxed struct using IL

后端 未结 5 1067
执笔经年
执笔经年 2020-12-24 07:41

Imagine we have a mutable struct (yes, don\'t start):

public struct MutableStruct
{
    public int Foo { get; set; }
    public override string          


        
5条回答
  •  不知归路
    2020-12-24 08:39

    You can do this even easier. Try this under .NET 4.5 where we have dynamic.

    struct Test
    {
        public Int32 Number { get; set; }
    
    
        public override string ToString()
        {
            return this.Number.ToString();
        }
    }
    
    
    class Program
    {
        static void Main( string[] args )
        {
            Object test = new Test();
    
            dynamic proxy = test;
    
            proxy.Number = 1;
    
            Console.WriteLine( test );
            Console.ReadLine();
        }
    }
    

    I know it's not reflection but still fun though.

提交回复
热议问题