TypeConverter.CreateInstance How does it works

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • João Santa Bárbara

    #1

    TypeConverter.CreateInstance How does it works

    Hi all
    i have a class that i have made and i made an Typeconverter as well, and my
    problem is
    the code genereated in my form is

    Dim ClassTest1 As TestApplication .ClassTest = New TestApplication .ClassTest
    Me.MyClassTest = ClassTest1

    Wich is quite good, but my problem is in my property browser i cannot change
    any of my properties
    ' need the "DesignerSerial izationVisibili ty " this in visible because this
    is a form that must be inherited and i don´t want this property tobe writen
    in the child form. unless it was changed in the child form .

    i have declare my property like this

    <LocalizableAtt ribute(True), _
    Browsable(True) , _
    DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Visible), _
    MergablePropert y(False)> _
    Public Property MyClassTest() As ClassTest
    Get
    Return xMyClassTest
    End Get
    Set(ByVal Value As ClassTest)
    xMyClassTest = Value
    xMyClassTest_Ch angedProperty()
    End Set
    End Property
    please see the code below

    <System.Compone ntModel.TypeCon verter(GetType( ClassTypeConver ter)), _
    EditorBrowsable (EditorBrowsabl eState.Always), _
    Serializable()> _
    Public Class ClassTest
    Inherits System.MarshalB yRefObject
    Implements System.IDisposa ble
    Private xText As String = "Texto"
    Private xColor As System.Drawing. Color = Color.Red
    Public Event ChangedProperty ()
    Event testesss As EventHandler
    Public Property Text() As String
    Get
    Return xText
    End Get
    Set(ByVal Value As String)
    xText = Value
    RaiseEvent ChangedProperty ()
    End Set
    End Property
    Public Property Color() As System.Drawing. Color
    Get
    Return xColor
    End Get
    Set(ByVal Value As System.Drawing. Color)
    xColor = Value
    RaiseEvent ChangedProperty ()
    End Set
    End Property
    <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
    Private Function ShouldSerialize Color() As Boolean
    Return Not Me.Color.Equals (Color.Red)
    End Function
    <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
    Private Sub ResetColor()
    Me.Color = Color.Red
    End Sub
    <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
    Private Function ShouldSerialize Text() As Boolean
    Return Me.Text <> "Texto"
    End Function
    <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
    Private Sub ResetText()
    xText = "Texto"
    End Sub

    Public Sub Dispose() Implements System.IDisposa ble.Dispose
    xColor = Nothing
    End Sub
    End Class


    ' Type Converter Class
    Imports System.Reflecti on
    Imports System.Componen tModel
    Imports System
    Imports System.Drawing
    Imports System.Drawing. Design
    Imports System.Componen tModel.Design.S erialization
    Public Class ClassTypeConver ter
    Inherits ExpandableObjec tConverter
    Public Overloads Overrides Function CanConvertTo(By Val context As
    System.Componen tModel.ITypeDes criptorContext, ByVal destinationType As
    System.Type) As Boolean
    Try
    If destinationType Is GetType(Instanc eDescriptor) Then
    Return True
    End If
    Return MyBase.CanConve rtTo(context, destinationType )
    Catch ex As Exception
    MessageBox.Show ("Erro CanConvertTo " & ex.StackTrace)
    End Try
    End Function
    Public Overloads Overrides Function ConvertTo(ByVal context As
    System.Componen tModel.ITypeDes criptorContext, ByVal culture As
    System.Globaliz ation.CultureIn fo, ByVal value As Object, ByVal
    destinationType As System.Type) As Object
    Try
    If destinationType Is Nothing Then
    Throw New ArgumentNullExc eption("Argumen tos Invalidos")
    End If
    If destinationType Is GetType(Instanc eDescriptor) Then
    Dim ci As ConstructorInfo =
    GetType(ClassTe st).GetConstruc tor(Type.EmptyT ypes)
    Return New InstanceDescrip tor(ci, Nothing, False)
    End If
    Return MyBase.ConvertT o(context, culture, value, destinationType )
    Catch ex As Exception
    MessageBox.Show ("Erro ConvertTo" & ex.StackTrace)
    End Try
    End Function


    Public Overloads Overrides Function CreateInstance( ByVal context As
    System.Componen tModel.ITypeDes criptorContext, ByVal propertyValues As
    System.Collecti ons.IDictionary ) As Object
    Return New ClassTest 'Activator.Crea teInstance(GetT ype(ClassTest))
    End Function
    Public Overloads Overrides Function GetCreateInstan ceSupported(ByV al context
    As System.Componen tModel.ITypeDes criptorContext) As Boolean
    Return True
    End Function

    End Class


  • João Santa Bárbara

    #2
    Re: TypeConverter.C reateInstance How does it works

    hi there ,,
    just try to put aonther property in your MyProperty structure but not add it
    to the constructor.
    and then try to change the vaule at design time. :(
    thsk
    JSB



    "joeycalisa y" <hcalisay@_spam killer_codex-systems.com> wrote in message
    news:uXL7jZfAFH A.2156@TK2MSFTN GP10.phx.gbl...[color=blue]
    > sorry but i cannot do a vb.net equivalent.
    >
    > try to browse my attached example to get some ideas...
    >
    > --
    > Joey Calisay
    > http://spaces.msn.com/members/joeycalisay/
    >
    >
    > "João Santa Bárbara" <joaosb@i24port ugal.com> wrote in message
    > news:eD3Agj9$EH A.3840@tk2msftn gp13.phx.gbl...[color=green]
    >> Hi all
    >> i have a class that i have made and i made an Typeconverter as well, and[/color]
    > my[color=green]
    >> problem is
    >> the code genereated in my form is
    >>
    >> Dim ClassTest1 As TestApplication .ClassTest = New[/color]
    > TestApplication .ClassTest[color=green]
    >> Me.MyClassTest = ClassTest1
    >>
    >> Wich is quite good, but my problem is in my property browser i cannot[/color]
    > change[color=green]
    >> any of my properties
    >> ' need the "DesignerSerial izationVisibili ty " this in visible because[/color]
    > this[color=green]
    >> is a form that must be inherited and i don´t want this property tobe[/color]
    > writen[color=green]
    >> in the child form. unless it was changed in the child form .
    >>
    >> i have declare my property like this
    >>
    >> <LocalizableAtt ribute(True), _
    >> Browsable(True) , _
    >> DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Visible),[/color]
    > _[color=green]
    >> MergablePropert y(False)> _
    >> Public Property MyClassTest() As ClassTest
    >> Get
    >> Return xMyClassTest
    >> End Get
    >> Set(ByVal Value As ClassTest)
    >> xMyClassTest = Value
    >> xMyClassTest_Ch angedProperty()
    >> End Set
    >> End Property
    >> please see the code below
    >>
    >> <System.Compone ntModel.TypeCon verter(GetType( ClassTypeConver ter)), _
    >> EditorBrowsable (EditorBrowsabl eState.Always), _
    >> Serializable()> _
    >> Public Class ClassTest
    >> Inherits System.MarshalB yRefObject
    >> Implements System.IDisposa ble
    >> Private xText As String = "Texto"
    >> Private xColor As System.Drawing. Color = Color.Red
    >> Public Event ChangedProperty ()
    >> Event testesss As EventHandler
    >> Public Property Text() As String
    >> Get
    >> Return xText
    >> End Get
    >> Set(ByVal Value As String)
    >> xText = Value
    >> RaiseEvent ChangedProperty ()
    >> End Set
    >> End Property
    >> Public Property Color() As System.Drawing. Color
    >> Get
    >> Return xColor
    >> End Get
    >> Set(ByVal Value As System.Drawing. Color)
    >> xColor = Value
    >> RaiseEvent ChangedProperty ()
    >> End Set
    >> End Property
    >> <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
    >> Private Function ShouldSerialize Color() As Boolean
    >> Return Not Me.Color.Equals (Color.Red)
    >> End Function
    >> <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
    >> Private Sub ResetColor()
    >> Me.Color = Color.Red
    >> End Sub
    >> <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
    >> Private Function ShouldSerialize Text() As Boolean
    >> Return Me.Text <> "Texto"
    >> End Function
    >> <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
    >> Private Sub ResetText()
    >> xText = "Texto"
    >> End Sub
    >>
    >> Public Sub Dispose() Implements System.IDisposa ble.Dispose
    >> xColor = Nothing
    >> End Sub
    >> End Class
    >>
    >>
    >> ' Type Converter Class
    >> Imports System.Reflecti on
    >> Imports System.Componen tModel
    >> Imports System
    >> Imports System.Drawing
    >> Imports System.Drawing. Design
    >> Imports System.Componen tModel.Design.S erialization
    >> Public Class ClassTypeConver ter
    >> Inherits ExpandableObjec tConverter
    >> Public Overloads Overrides Function CanConvertTo(By Val context As
    >> System.Componen tModel.ITypeDes criptorContext, ByVal destinationType As
    >> System.Type) As Boolean
    >> Try
    >> If destinationType Is GetType(Instanc eDescriptor) Then
    >> Return True
    >> End If
    >> Return MyBase.CanConve rtTo(context, destinationType )
    >> Catch ex As Exception
    >> MessageBox.Show ("Erro CanConvertTo " & ex.StackTrace)
    >> End Try
    >> End Function
    >> Public Overloads Overrides Function ConvertTo(ByVal context As
    >> System.Componen tModel.ITypeDes criptorContext, ByVal culture As
    >> System.Globaliz ation.CultureIn fo, ByVal value As Object, ByVal
    >> destinationType As System.Type) As Object
    >> Try
    >> If destinationType Is Nothing Then
    >> Throw New ArgumentNullExc eption("Argumen tos Invalidos")
    >> End If
    >> If destinationType Is GetType(Instanc eDescriptor) Then
    >> Dim ci As ConstructorInfo =
    >> GetType(ClassTe st).GetConstruc tor(Type.EmptyT ypes)
    >> Return New InstanceDescrip tor(ci, Nothing, False)
    >> End If
    >> Return MyBase.ConvertT o(context, culture, value, destinationType )
    >> Catch ex As Exception
    >> MessageBox.Show ("Erro ConvertTo" & ex.StackTrace)
    >> End Try
    >> End Function
    >>
    >>
    >> Public Overloads Overrides Function CreateInstance( ByVal context As
    >> System.Componen tModel.ITypeDes criptorContext, ByVal propertyValues As
    >> System.Collecti ons.IDictionary ) As Object
    >> Return New ClassTest 'Activator.Crea teInstance(GetT ype(ClassTest))
    >> End Function
    >> Public Overloads Overrides Function GetCreateInstan ceSupported(ByV al[/color]
    > context[color=green]
    >> As System.Componen tModel.ITypeDes criptorContext) As Boolean
    >> Return True
    >> End Function
    >>
    >> End Class
    >>
    >>[/color]
    >
    >
    >[/color]


    Comment

    • joeycalisay

      #3
      Re: TypeConverter.C reateInstance How does it works

      and why would you not add it to the constructor of the struct? I believe
      you have to because you will be instructing your TypeConverter which
      constructor to use and if you will be using the old one (my two parameter
      constructor), how will you serialize the value for the additional property?
      Perhaps you can customize that with a custom CodeDom but it will be really
      easier (and practical IMO) if your constructor (which the typeconverter will
      use) properly initializes your struct instance...

      --
      Joey Calisay



      "João Santa Bárbara" <joaosb@i24port ugal.com> wrote in message
      news:uoOlmdVCFH A.2288@TK2MSFTN GP14.phx.gbl...[color=blue]
      > hi there ,,
      > just try to put aonther property in your MyProperty structure but not add[/color]
      it[color=blue]
      > to the constructor.
      > and then try to change the vaule at design time. :(
      > thsk
      > JSB
      >
      >
      >
      > "joeycalisa y" <hcalisay@_spam killer_codex-systems.com> wrote in message
      > news:uXL7jZfAFH A.2156@TK2MSFTN GP10.phx.gbl...[color=green]
      > > sorry but i cannot do a vb.net equivalent.
      > >
      > > try to browse my attached example to get some ideas...
      > >
      > > --
      > > Joey Calisay
      > > http://spaces.msn.com/members/joeycalisay/
      > >
      > >
      > > "João Santa Bárbara" <joaosb@i24port ugal.com> wrote in message
      > > news:eD3Agj9$EH A.3840@tk2msftn gp13.phx.gbl...[color=darkred]
      > >> Hi all
      > >> i have a class that i have made and i made an Typeconverter as well,[/color][/color][/color]
      and[color=blue][color=green]
      > > my[color=darkred]
      > >> problem is
      > >> the code genereated in my form is
      > >>
      > >> Dim ClassTest1 As TestApplication .ClassTest = New[/color]
      > > TestApplication .ClassTest[color=darkred]
      > >> Me.MyClassTest = ClassTest1
      > >>
      > >> Wich is quite good, but my problem is in my property browser i cannot[/color]
      > > change[color=darkred]
      > >> any of my properties
      > >> ' need the "DesignerSerial izationVisibili ty " this in visible because[/color]
      > > this[color=darkred]
      > >> is a form that must be inherited and i don´t want this property tobe[/color]
      > > writen[color=darkred]
      > >> in the child form. unless it was changed in the child form .
      > >>
      > >> i have declare my property like this
      > >>
      > >> <LocalizableAtt ribute(True), _
      > >> Browsable(True) , _
      > >>[/color][/color][/color]
      DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Visible),[color=blue][color=green]
      > > _[color=darkred]
      > >> MergablePropert y(False)> _
      > >> Public Property MyClassTest() As ClassTest
      > >> Get
      > >> Return xMyClassTest
      > >> End Get
      > >> Set(ByVal Value As ClassTest)
      > >> xMyClassTest = Value
      > >> xMyClassTest_Ch angedProperty()
      > >> End Set
      > >> End Property
      > >> please see the code below
      > >>
      > >> <System.Compone ntModel.TypeCon verter(GetType( ClassTypeConver ter)), _
      > >> EditorBrowsable (EditorBrowsabl eState.Always), _
      > >> Serializable()> _
      > >> Public Class ClassTest
      > >> Inherits System.MarshalB yRefObject
      > >> Implements System.IDisposa ble
      > >> Private xText As String = "Texto"
      > >> Private xColor As System.Drawing. Color = Color.Red
      > >> Public Event ChangedProperty ()
      > >> Event testesss As EventHandler
      > >> Public Property Text() As String
      > >> Get
      > >> Return xText
      > >> End Get
      > >> Set(ByVal Value As String)
      > >> xText = Value
      > >> RaiseEvent ChangedProperty ()
      > >> End Set
      > >> End Property
      > >> Public Property Color() As System.Drawing. Color
      > >> Get
      > >> Return xColor
      > >> End Get
      > >> Set(ByVal Value As System.Drawing. Color)
      > >> xColor = Value
      > >> RaiseEvent ChangedProperty ()
      > >> End Set
      > >> End Property
      > >> <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
      > >> Private Function ShouldSerialize Color() As Boolean
      > >> Return Not Me.Color.Equals (Color.Red)
      > >> End Function
      > >> <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
      > >> Private Sub ResetColor()
      > >> Me.Color = Color.Red
      > >> End Sub
      > >> <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
      > >> Private Function ShouldSerialize Text() As Boolean
      > >> Return Me.Text <> "Texto"
      > >> End Function
      > >> <EditorBrowsabl e(EditorBrowsab leState.Advance d)> _
      > >> Private Sub ResetText()
      > >> xText = "Texto"
      > >> End Sub
      > >>
      > >> Public Sub Dispose() Implements System.IDisposa ble.Dispose
      > >> xColor = Nothing
      > >> End Sub
      > >> End Class
      > >>
      > >>
      > >> ' Type Converter Class
      > >> Imports System.Reflecti on
      > >> Imports System.Componen tModel
      > >> Imports System
      > >> Imports System.Drawing
      > >> Imports System.Drawing. Design
      > >> Imports System.Componen tModel.Design.S erialization
      > >> Public Class ClassTypeConver ter
      > >> Inherits ExpandableObjec tConverter
      > >> Public Overloads Overrides Function CanConvertTo(By Val context As
      > >> System.Componen tModel.ITypeDes criptorContext, ByVal destinationType As
      > >> System.Type) As Boolean
      > >> Try
      > >> If destinationType Is GetType(Instanc eDescriptor) Then
      > >> Return True
      > >> End If
      > >> Return MyBase.CanConve rtTo(context, destinationType )
      > >> Catch ex As Exception
      > >> MessageBox.Show ("Erro CanConvertTo " & ex.StackTrace)
      > >> End Try
      > >> End Function
      > >> Public Overloads Overrides Function ConvertTo(ByVal context As
      > >> System.Componen tModel.ITypeDes criptorContext, ByVal culture As
      > >> System.Globaliz ation.CultureIn fo, ByVal value As Object, ByVal
      > >> destinationType As System.Type) As Object
      > >> Try
      > >> If destinationType Is Nothing Then
      > >> Throw New ArgumentNullExc eption("Argumen tos Invalidos")
      > >> End If
      > >> If destinationType Is GetType(Instanc eDescriptor) Then
      > >> Dim ci As ConstructorInfo =
      > >> GetType(ClassTe st).GetConstruc tor(Type.EmptyT ypes)
      > >> Return New InstanceDescrip tor(ci, Nothing, False)
      > >> End If
      > >> Return MyBase.ConvertT o(context, culture, value, destinationType )
      > >> Catch ex As Exception
      > >> MessageBox.Show ("Erro ConvertTo" & ex.StackTrace)
      > >> End Try
      > >> End Function
      > >>
      > >>
      > >> Public Overloads Overrides Function CreateInstance( ByVal context As
      > >> System.Componen tModel.ITypeDes criptorContext, ByVal propertyValues As
      > >> System.Collecti ons.IDictionary ) As Object
      > >> Return New ClassTest 'Activator.Crea teInstance(GetT ype(ClassTest))
      > >> End Function
      > >> Public Overloads Overrides Function GetCreateInstan ceSupported(ByV al[/color]
      > > context[color=darkred]
      > >> As System.Componen tModel.ITypeDes criptorContext) As Boolean
      > >> Return True
      > >> End Function
      > >>
      > >> End Class
      > >>
      > >>[/color]
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...