질문

2011-02-07 4 views
0

그래서 나는 SetLineItemEventPayLoad는 LineItem을 객체 권리를 포함 바로 이벤트 ...질문

public class SetLineItemEvent : CompositePresentationEvent<SetLineItemEventPayload> { } 

아니가? 해당 이벤트를 발생시키고 SetLineitemEventPayLoad의 새 인스턴스를 만들고 광고 항목 개체를 설정하면 해당 개체의 복사본이 만들어 집니까? 아니면 원본 개체에 대한 참조를 남겼습니까? "딥 클론 (Deep Cloning)"(완전히 새로운 사본이 있음을 의미)을 사용하는 것처럼 보입니다.하지만 누군가가 가능하면이를 확인해주기를 바랍니다.

나는 깊은 복제 무엇을 의미하는 더 나은 아이디어를위한이 링크를 참조하십시오 .. http://www.csharp411.com/c-object-clone-wars/

감사 대상 복사하거나 단순히 참조 여부

답변

0

이 객체가 클래스를 기반으로 이상 여부에 따라 또는 구조체. 클래스 개체는 항상 참조되는 반면 구조체 개체는 매개 변수로 할당되거나 메서드에 전달 될 때마다 항상 복사됩니다.

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 

따라서 행동은 '광고 항목'이 클래스 또는 구조체인지 여부에 따라 달라집니다.

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