2009-05-22 4 views
1

사용자가 특정 사용자 지정 목록 컨트롤에서 다른 응용 프로그램의 다른 인스턴스에있는 두 번째 목록 컨트롤에있는 데이터 행을 끌어다 놓을 수 있도록 시도하고 있습니다. parameterTypedListView.SelectedObjects는 T 만 포함하는 사용자 정의 클래스입니다 일반은 IList가인스턴스 간 드래그 앤 드롭 데이터

DoDragDrop(parameterTypedListView.SelectedObjects, DragDropEffects.Copy); 

는 필드/속성으로 value 형.

OnDragDrop 이벤트에서이 데이터를 추출하려고 시도하지만 System.MarshalByRefObject에서 상속 된 것으로 보이는 System.__ComObject 개체 만 가져옵니다.

요약하면 : 실제로 사용할 수있는 객체 지향 형식으로 데이터를 추출하려면 어떻게합니까?

편집 : 내 맞춤 클래스를 직렬화 가능으로 설정하면 아무런 효과가 없습니다. 나는 __ComObject 열거 할 수 있습니다 :

foreach (var dataObject in (IEnumerable) e.Data.GetData("System.Collections.ArrayList")) 
{ 
    // this actually enumerates the correct number of times, i.e. as many times as there are items in the list. 
} 

을하지만, 모든 데이터 객체 내가 아무것도 유용에 캐스트 할 수없는 시스템 .__하여 ComObject, 그 자체입니다.

+0

... 인스턴스에서 작동? – Konstantinos

+0

@ Konstantinos, 네, 같은 응용 프로그램의 여러 인스턴스. –

답변

3

초기 문제를 복제 할 수 있었지만 배열 목록의 클래스에 [Serializable] 특성을 추가하자마자 개체를 올바른 형식으로 볼 수있었습니다.

다음은 작은 예제 코드를 보여주는 몇 가지 예제 코드입니다.

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop); 
     this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter); 
    } 

    [Serializable] 
    class DragClass 
    { 
     public string Prop1 { get; set; } 
     public int Prop2 { get; set; } 
    } 

    private void label1_MouseDown(object sender, MouseEventArgs e) 
    { 
     System.Collections.ArrayList aDragClasses = new System.Collections.ArrayList(); 
     aDragClasses.Add(new DragClass() { Prop1 = "Test1", Prop2 = 2 }); 
     aDragClasses.Add(new DragClass() { Prop1 = "Test2", Prop2 = 3 }); 
     aDragClasses.Add(new DragClass() { Prop1 = "Test3", Prop2 = 4 }); 

     DoDragDrop(aDragClasses, DragDropEffects.Copy); 
    } 

    private void Form1_DragEnter(object sender, DragEventArgs e) 
    { 
     e.Effect = DragDropEffects.Copy; 
    } 

    private void Form1_DragDrop(object sender, DragEventArgs e) 
    { 
     foreach (var aData in (System.Collections.IEnumerable)e.Data.GetData(typeof(System.Collections.ArrayList))) 
     { 
      System.Diagnostics.Debug.WriteLine(((DragClass)aData).Prop1); 
     } 
    } 



} 
0

나는 데이터를 전달하기 위해 목록을 직접 사용하고 있다고 생각합니다. 나는 그것을 여러 가지 방법으로 시도하여 실패하게 만들었고 작동하지 않는 몇 가지 방법을 찾아 냈습니다.

사용자 지정 클래스에 [Serializable] 특성이 없으면 클래스가 프로세스간에 마샬링되는 방식이므로 올바르게 작동하지 않습니다. 또한, 데이터를 전달하기 위해 List를 직접 사용하면 null 참조 예외가 발생합니다.

간단한 전송 클래스를 사용하여 데이터를 전달하면 (모든 유형이 직렬화 가능) 모든 것이 제대로 작동합니다.

[Serializable] 
class Test 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
} 

[Serializable] 
class Transport 
{ 
    public Transport() 
    { 
     this.Items = new List<Test>(); 
    } 
    public IList<Test> Items { get; private set; } 
} 

그럼이 문제 할 수 없다 그리고 우리가 2 개 개의 다른 경우에 두 번 실행되는 동일한 프로그램에 대해 얘기하는

private void Form1_DragDrop(object sender, DragEventArgs e) 
{ 
    foreach (var item in ((Transport)e.Data.GetData(typeof(Transport))).Items) 
    { 
     System.Diagnostics.Debug.WriteLine(item.Name + " " + item.Description); 
    } 
}