简体   繁体   中英

How to manage an object reference in c#

Consider this code:

    private static void Main(string[] args)
    {
        var person = new Person { Manager = new Manager { Name = "Shahrooz" }, Name = "Sina" };
        Console.WriteLine("I am {0} my manager is {1}", person.Name, person.Manager.Name);
        //Outpu:I am Sina my manager is Shahrooz
        var newManager = person.Manager;
        person.Manager = null;

        new Thread(() => File.WriteAllText( Path.Combine("C:\\", string.Format("{0}.txt", person.Name)), new JavaScriptSerializer().Serialize(person))).Start();
        Console.WriteLine("I am {0} my manager is", person.Name);
        person.Manager = newManager;
        Console.ReadLine();
    }
}

public class Person
{
    public string Name { get; set; }
    public Manager Manager { get; set; }
}

public class Manager
{
    public string Name { get; set; }
}

I am trying to serialize an object with JavaScriptSerializer . Before serializing I set person's manager to null but I get wonderful results in text file:

{"Name":"Sina","Manager":{"Name":"Shahrooz"}}

I mixed up. Please help me.

Update:

I change my code.that is ok:

  internal class Program
    {
        private static  void Main(string[] args)
        {
            var person = new Person { Manager = new Manager { Name = "Shahrooz" }, Name = "Sina" };
            Console.WriteLine("I am {0} my manager is {1}", person.Name, person.Manager.Name);
            //Outpu:I am Sina my manager is Shahrooz
            var x = person.Manager;
            person.Manager = null;
            add(person);
            Console.WriteLine("I am {0} my manager is", person.Name);
            person.Manager = x;
            Console.ReadLine();
        }

        public static async void add(Person person)
        {
            await AddToFile(person);

        }

        private async static Task AddToFile(Person person)
        {
            File.WriteAllText(Path.Combine("C:\\", string.Format("{0}.txt", person.Name)), new JavaScriptSerializer().Serialize(person));
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public Manager Manager { get; set; }
    }

    public class Manager
    {
        public string Name { get; set; }
    }

The problem is that the thread you create to serialize the object races with the main thread. Sometimes it will serialize the object before the main thread resets the Manager reference, sometimes after.

Here are some solution ideas:

  • Get rid of the thread altogether. In this reduced example, it's useless. In the original code, it's probably not.
  • Synchronize access to the object, ie use some form of synchronization to make sure that the thread has finished its serialization before you continue on the main thread. Of course, if the main thread just starts the worker and then immediately waits for it to complete, there was no point in using threads in the first place, so you'll want to make the synchronization more fine-grained.
  • Give the thread its private copy of the object to work with. That is, instead of modifying the existing Person, create a new one that has all the same properties except for Manager being null, and give that to the thread.

因为您的经理对newManager的引用相同,并且经理实际上不是null。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM