Home All Groups Group Topic Archive Search About

Problem with "currentDomain.createInstance" and nested types.

Author
9 Feb 2006 1:31 PM
Giò
Hi.  I've a big problem with "currentDomain.createInstance" and nested
types.  I have an assembly that contains some types which I want to
create using reflection.  To Do so, I use The method
"System.AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap
(ASSEMBLYFILES AS STRING,bindingFlags ,... )";  all works fine and it
creates the object.  After, I serialize ( with binary serialization) a
specific group of properties of this object, and there are not
problems;  but when I deserialize properties, on another object created
in the same way, if the type are nested, the environment throw an
invalid cast exception!  This problem doesn't appear if I create an
instance of object using its constructor (in standard way).
I think that the problem is on the use of the bindingFlags in
"CreateInstanceFromAndUnwrap" method, but certainly the problem is only
on the nested types.


I attach code for better understanding


_ Create Instance


Public Overloads Shared Function CreateObject(ByVal
assemblyURIFileName As String, ByVal typeFullName As String) As Object
            'Dim typeNameComponents() As String =
typeFullName.Split(","c)

            ' Creazione istanza oggetto
            Dim control As Object
            Try

                control =
system.appDomain.CurrentDomain.CreateInstanceFromAndUnwrap(assemblyURIFileName,
                        GetNamespace(typeFullName), True,
Reflection.BindingFlags.Default Or _
                         Reflection.BindingFlags.Public Or_
                        Reflection.BindingFlags.NonPublic Or _
                        Reflection.BindingFlags.Instance Or _
                         Reflection.BindingFlags.CreateInstance Or _
                        Reflection.BindingFlags.IgnoreCase, _
                        Nothing, Nothing, Nothing, Nothing, Nothing)

                If control Is Nothing Then
                    MsgBox("Tipo Inesistente")
                    System.Environment.Exit(0)
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            Return control
End Function




Type definition in external assembly, loaded using
CreateInstanceFromAndUnwrap method
( used after to create instance, with appdomain method)


Serializable(), DiploCard.ComponentModel.AdToolBoxItem("Linea")> _
Public Class AdLine
    Inherits AdControlBase
    'Implements i

    Protected Overrides Sub AdInitializeOnControlAdded()
        ' Inizializzazioni da effettuare quando il controllo viene
aggiunto al container

        Me.BackColor = System.Drawing.SystemColors.ControlText
        Me.Size = New System.Drawing.Size(100, 1)
    End Sub

    Protected Overrides Sub AdInitializeOnAmbientCreationComplete()

    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
        If disposing Then
            'TODO: - IMPORTANTE - Rilascio di tutte le risorse
utilizzate nella classe
        End If
        MyBase.Dispose(disposing)
    End Sub

  Dim _orientation As AdOrientationType = AdOrientationType.Vertical

<DiploCard.ComponentModel.AdVisualizableProperty(ComponentModel.AdVisualizableProperty.Yes)>
_
    Public Property Orientation() As AdOrientationType
        Get
            Return _orientation
        End Get
        Set(ByVal Value As AdOrientationType)
            _orientation = Value
        End Set
    End Property


<Serializable()> _
Public Enum AdOrientationType As Integer
    Horizontal = 5
    Vertical = 7
End Enum

end Class


If I serialize the property "Orientation()", when I deserialize it's
value on another object with same type (created using  "CreateObject"
method), an invalidcatstException is thrown, even if the types are the
same!

The problem get out when the property has a type defined in the
assembly dynamically loaded, using the createInstance Method.


Can someone help me?

Thanks in advance.  Giò       

Ps:sorry for my bad english

AddThis Social Bookmark Button