저는 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한다.
어떤 아이디어?
값을 하나씩 복사하는 것은 실제로별로 좋지 않을 수 있습니다. 그런 식으로, 당신은 각각의 속성에 대해 생각했다는 것을 문서화하고'dest.Prop1 = src.Prop1'이 속성을 복제하는 올바른 방법이라는 것을 확신하게됩니다 (예를 들어,'dest.Prop1 = MakeDeepProp1Copy src.Prop1)'. – Heinzi
COM 개체가'IPersist' 인터페이스 (http://msdn.microsoft.com/en-us/library/ms688695%28VS.85%29.aspx)를 지원합니까? –
VB.NET을 모르지만'Source.GetType()'은'__COMObject'를 반환하지 않습니다? VB.NET에 해당하는'typeof '을 사용하면 어떻게됩니까? (Google에 따르면, (GetType (SourceType)')? –