2013-03-13 2 views
2

저는 VB.Net 2010을 사용하고 있습니다. 외부 (자체가 아닌) 응용 프로그램에서 입력 한 (COM) 객체의 내용을 내 응용 프로그램에 복제하려고합니다. 필드하나씩 (필드/속성 추가 또는 미래의 응용 프로그램 빌드에서 제거 될 수 있습니다) 복사 할 싶지 않습니다.비 직렬화 (COM) 객체 복제

개체 유형이 serialize되지 않습니다. 다음과 같이

나는 (VB 코드는 스레드 copy one object to another에 제안) 반사을 시도했다 :

Imports System.Reflection 

Public Class ObjectHelper 

    ' Creates a copy of an object 
    Public Shared Function GetCopy(Of SourceType As {Class, New})(ByVal Source As SourceType) As SourceType 

     Dim ReturnValue As New SourceType 
     Dim sourceProperties() As PropertyInfo = Source.GetType().GetProperties() 

     For Each sourceProp As PropertyInfo In sourceProperties 
      sourceProp.SetValue(
       ReturnValue, 
       sourceProp.GetValue(Source, Nothing), 
       Nothing) 
     Next 

     Return ReturnValue 

    End Function 

End Class 

작동하지 않습니다, 배열이 비어() 반환 sourceProperties한다.

어떤 아이디어?

+0

값을 하나씩 복사하는 것은 실제로별로 좋지 않을 수 있습니다. 그런 식으로, 당신은 각각의 속성에 대해 생각했다는 것을 문서화하고'dest.Prop1 = src.Prop1'이 속성을 복제하는 올바른 방법이라는 것을 확신하게됩니다 (예를 들어,'dest.Prop1 = MakeDeepProp1Copy src.Prop1)'. – Heinzi

+1

COM 개체가'IPersist' 인터페이스 (http://msdn.microsoft.com/en-us/library/ms688695%28VS.85%29.aspx)를 지원합니까? –

+1

VB.NET을 모르지만'Source.GetType()'은'__COMObject'를 반환하지 않습니다? VB.NET에 해당하는'typeof '을 사용하면 어떻게됩니까? (Google에 따르면, (GetType (SourceType)')? –

답변

0

당신이 정말로 당신의 인터페이스를 검사 할 때 그 개체의 실제 유형으로

Dim sourceProperties() As PropertyInfo = Source.GetType().GetProperties() 

Dim sourceProperties() As PropertyInfo = GetType(SourceType).GetProperties() 

Source.GetType() 반환 _COMObject 타입이어야 구현에 버그가,가 우선 객체에 의해 구현되는 타입.

다른 해결책은 개체가 IPersist interfaces 중 하나를 구현하는지 확인하고이를 실제 COM 직렬화에 사용하는 것입니다. 개체를 예를 들어로 전송하면됩니다. System.Runtime.InteropServices.ComTypes.IPersistFile. 불행히도 다른 IPersist * 인터페이스는 네임 스페이스에 정의되어 있지 않으므로 어딘가에서 가져와야합니다.

관련 문제