question about cloning and prism events

79 views Asked by At

So I have an event right...

 public class SetLineItemEvent : CompositePresentationEvent<SetLineItemEventPayload> { }

No that SetLineItemEventPayLoad contains a LineItem object right? When I fire that event and create a new instance of SetLineitemEventPayLoad and set the line item object does it make a copy of that object? or am I left with a reference to the original object? It looks like it uses "Deep Cloning" (meaning I have a completely new copy) but I would like someone to confirm that if they are able.

See this link for a better idea of what I mean by deep cloning.. http://www.csharp411.com/c-object-clone-wars/

Thanks

2

There are 2 answers

0
Paul Ruane On

Whether an object is copied or merely referenced depends upon whether than object is based upon a class or struct. Class objects are always referenced whereas struct objects are always copied whenever they are assigned or passed as parameters to methods.

struct A {}

A a1 = new A();
A a2 = a1; // copied
a1 == a2; // false

class B {}

B b1 = new B();
B b2 = bl; // both reference same object
b1 == b2; // true

So, the behaviour depends upon whether your 'line item' is a class or struct.

0
deepee1 On

As Paul Ruane states, so long as "SetLineItemEventPayload" is a class it will be passed by reference. No cloning.

Here a small sample program in it's entirety that proves just that...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static normalclass c;
        static normalstruct s;
        static void Main(string[] args)
        {
            c = new normalclass() { classArg = 1 };
            s = new normalstruct() { structArg = 1 };
            EventVarPasser.BasicEvent += new EventHandler<FancyEventArgs>(EventVarPasser_BasicEvent);
            EventVarPasser.RaiseEvent(c,s);


        }

        static void EventVarPasser_BasicEvent(object sender, FancyEventArgs e)
        {
            Console.WriteLine("Before Messing with Eventargs");
            Console.WriteLine("Class:" + c.classArg.ToString() + "  Struct: " + s.structArg.ToString());
            e.normclass.classArg += 1;
            e.normstruct.structArg += 1;
            Console.WriteLine("After Messing with Eventargs");
            Console.WriteLine("Class :" + c.classArg.ToString() + "  Struct: " + s.structArg.ToString());

        }

    }

    static class EventVarPasser
    {
        public static event EventHandler<FancyEventArgs> BasicEvent;

        public static void RaiseEvent(normalclass nc, normalstruct ns)
        {
            FancyEventArgs e = new FancyEventArgs() { normclass = nc, normstruct = ns };        
             BasicEvent(null, e);
        }

    }

    class normalclass
    {
        public int classArg;
    }

    struct normalstruct
    {
        public int structArg;
    }

    class FancyEventArgs : EventArgs
    {
        public normalstruct normstruct;
        public normalclass normclass;
    }

}

The output to this is:

Before Messing with Eventargs
Class:1  Struct: 1
After Messing with Eventargs
Class :2  Struct: 1