所以我有一个正确的活动......
public class SetLineItemEvent : CompositePresentationEvent<SetLineItemEventPayload> { }
No SetLineItemEventPayLoad是否包含LineItem对象?当我触发该事件并创建SetLineitemEventPayLoad的新实例并设置行项目对象时,它是否复制了该对象?还是我留下了对原始物体的引用?看起来它使用“深度克隆”(意思是我有一个全新的副本),但我希望有人确认,如果他们能够。
请参阅此链接,以便更好地了解深度克隆的含义。 http://www.csharp411.com/c-object-clone-wars/
由于
答案 0 :(得分:0)
是复制对象还是仅仅复制对象取决于对象是基于类还是结构。始终引用类对象,而只要将struct对象分配或作为参数传递给方法,就始终复制它们。
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
因此,行为取决于您的“订单项”是类还是结构。
答案 1 :(得分:0)
正如Paul Ruane所说,只要“SetLineItemEventPayload”是一个类,它就会通过引用传递。没有克隆。
这里有一个小样本程序,证明了......
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;
}
}
输出到:
Before Messing with Eventargs
Class:1 Struct: 1
After Messing with Eventargs
Class :2 Struct: 1