2010-02-02 7 views
0

클래스 객체를 가져 와서 최소한의 코드로 웹 서비스 객체로 변환 할 수있는 전송 메커니즘을 만들려고합니다.1 유형의 객체를 다른 유형의 객체로 전송

나는이 접근법으로 꽤 좋은 성공을 거두었지만 내 소스 객체의 속성으로 사용자 정의 클래스가 반환 될 때이를 위해 기술을 수정해야합니다. 내가 필요로 무엇

Private Sub Transfer(ByVal src As Object, ByVal dst As Object) 
    Dim theSourceProperties() As Reflection.PropertyInfo 

    theSourceProperties = src.GetType.GetProperties(Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance) 

    For Each s As Reflection.PropertyInfo In theSourceProperties 
     If s.CanRead AndAlso (Not s.PropertyType.IsGenericType) Then 
      Dim d As Reflection.PropertyInfo 
      d = dst.GetType.GetProperty(s.Name, Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance) 
      If d IsNot Nothing AndAlso d.CanWrite Then 
       d.SetValue(dst, s.GetValue(src, Nothing), Nothing) 
      End If 
     End If 
    Next 
End Sub 

은 어떤 소스 속성은 (문자열, INT16, INT32 등, 그리고 복잡한 형태의) 기본 유형 인 경우 결정의 일부입니다.

s.PropertyType.Attributes를보고 마스크를 확인했지만 기본 유형임을 나타내는 아무것도 찾을 수 없습니다.

확인할 사항이 있습니까?

+0

파생 클래스에서 재정의 될 때 IsPrimitive 속성을 구현 타입은 기본 형식 중 하나인지의 여부를 판정 Type.IsPrimitiveImpl 방법. http://msdn.microsoft.com/en-us/library/system.type.isprimitiveimpl%28VS.71%29.aspx – abmv

답변

0

abmv의 팁 덕분에, 이것은 내가 사용하는 최종 결과입니다. 여전히 몇 가지 특정 속성을 코딩해야했지만 대부분이 메커니즘을 통해 자동으로 처리되었습니다.

Private Sub Transfer(ByVal src As Object, ByVal dst As Object) 
    Dim theSourceProperties() As Reflection.PropertyInfo 

    theSourceProperties = src.GetType.GetProperties(Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance) 

    For Each s As Reflection.PropertyInfo In theSourceProperties 
     If s.CanRead AndAlso (Not s.PropertyType.IsGenericType) And (s.PropertyType.IsPrimitive Or s.PropertyType.UnderlyingSystemType Is GetType(String)) Then 
      Dim d As Reflection.PropertyInfo 
      d = dst.GetType.GetProperty(s.Name, Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance) 
      If d IsNot Nothing AndAlso d.CanWrite Then 
       d.SetValue(dst, s.GetValue(src, Nothing), Nothing) 
      End If 
     End If 
    Next 
End Sub 
관련 문제