C# 关于克隆和棱镜事件的问题

C# 关于克隆和棱镜事件的问题,c#,mvvm,prism-4,C#,Mvvm,Prism 4,所以我有一件事是对的 public class SetLineItemEvent : CompositePresentationEvent<SetLineItemEventPayload> { } 公共类SetLineItemEvent:CompositePresentationEvent{} 否SetLineItemEventPayLoad包含LineItem对象,对吗?当我触发该事件并创建SetLineitemEventPayLoad的新实例并设置LineItem对象时,它

所以我有一件事是对的

 public class SetLineItemEvent : CompositePresentationEvent<SetLineItemEventPayload> { }
公共类SetLineItemEvent:CompositePresentationEvent{}
否SetLineItemEventPayLoad包含LineItem对象,对吗?当我触发该事件并创建SetLineitemEventPayLoad的新实例并设置LineItem对象时,它是否会复制该对象?还是我留下了对原始对象的引用?它看起来像是使用了“深度克隆”(意思是我有一个全新的拷贝),但我希望有人能证实这一点

请参阅此链接,以更好地了解我所说的深度克隆。。


感谢

对象是被复制还是仅仅被引用取决于对象是基于类还是基于结构。类对象总是被引用,而结构对象总是在作为参数分配或传递给方法时被复制

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

因此,行为取决于“行项目”是类还是结构。

正如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