2010年3月5日星期五

The problem involved by making structure as a property

I met a strange problem while I was evaluating the XamlServices that involved in .net 4.
I created 2 types for Xaml Serialization Test:

 public class Foo
    {
        public Foo() { }
        public Foo(int a)
        {
            Random RND = new Random();
            Value = RND.Next();
            MyProperty = RND.Next();
            Complex.Init();
        }
        
        [Ambient]
        public int Value { get; set; }

        [Ambient]
        public int MyProperty { get;  private set; }

        [Ambient]
        public Nested Complex;
    }

    public struct Nested
    {
        public void Init()
        {
            this.Temp = Guid.NewGuid();
        }

        [Ambient]
        public Guid Temp { get; set; }
    }

So I can create a test instance of Foo by calling Foo(int a), e.g:
Foo foo = new Foo(1);

But since Xaml doesn't support serialize fields, I cannot apply AmbientAttribute to field, so the compiling fails. So I changed the field Complex of Class Foo to a property:

public Nested Complex { get; set;}

Then the code compiles correct. But when I executed the code, I found the Temp property of Complex is empty guid. But I found I do called the Init method in the constructor of Foo. What's the problem?

I believe that the problem must be special behavior related to ValueType. So I omitted to call Init(), I try to assign the Complex in Foo's Constructor as following code

//Complex.Init();
this.Complex.Temp = Guid.NewGuid();

Compiles fails!  Visual Studio tells me "Cannot modify the return value of 'structTest.Foo.Complex' because it is not a variable"

Still now, everything is explainable:
1. It is because this.Complex is pointed to the variable itself  that Code Complex.Init() works when Complex is field.
2. It is because this.Complex is a return value, a copy of the original variable, instead of the original variable that the Code Complex.Init() doesn't work when Complex is a property. this.Complex.Init() do changed the value of the Temp, but it modified the new copy instead of the original one.

So we need to be careful while change a value type field into a property, which may modify the program behavior a lot, and really difficult to debug while the code is complicated.

Posted via email from 米良的实验室

没有评论:

发表评论