Results 1 to 6 of 6

Thread: [2005] Reflected Type Constructor

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Question [2005] Reflected Type Constructor

    Code:
     Dim pair As KeyValuePair(Of Type, String) = DirectCast(Me.cmbClass.SelectedItem _
                , KeyValuePair(Of Type, String))
            Dim init As System.Reflection.ConstructorInfo = pair.Key.TypeInitializer
    I've stored the type I want to create, and a string description in a combobox, but I haven't been able to find a way to get a handle on and make use of the constructor of that type from here. This code I found using intellisense looked like it should work, but init is nothing after this runs.

    I'm trying to get a handle on the constructor for my type, any ideas?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Reflected Type Constructor

    I haven't confirmed this but I'd guess that the TypeInitializer property returns a ConstructorInfo for the constructor with no parameters. Does your type have such a constructor? If not then you'd have to use GetConstructor or GetConstructors.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Re: [2005] Reflected Type Constructor

    if you mean does my type have a default constructor with no arguments, then no it doesn't. I'll see if I can find GetConstructor.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Reflected Type Constructor

    A default constructor inherently has no parameters, but I'd think that TypeInitializer would return an object even if you'd declared your own constructor with no parameters. We can test it though. Let's define three classes:
    vb.net Code:
    1. Public Class Class1
    2.  
    3. End Class
    4.  
    5.  
    6. Public Class Class2
    7.  
    8.     Public Sub New()
    9.  
    10.     End Sub
    11.  
    12. End Class
    13.  
    14.  
    15. Public Class Class3
    16.  
    17.     Public Sub New(ByVal someParameter As Object)
    18.  
    19.     End Sub
    20.  
    21. End Class
    Class1 has only its default constructor, Class2 has an explicit constructor with no parameters and Class3 has an explicit constructor with a parameter. No let's try this code:
    vb.net Code:
    1. Dim type1 As Type = GetType(Class1)
    2. Dim type2 As Type = GetType(Class2)
    3. Dim type3 As Type = GetType(Class3)
    4.  
    5. MessageBox.Show((type1.TypeInitializer IsNot Nothing).ToString(), "Class1")
    6. MessageBox.Show((type2.TypeInitializer IsNot Nothing).ToString(), "Class2")
    7. MessageBox.Show((type3.TypeInitializer IsNot Nothing).ToString(), "Class3")
    When I tested that I got False in all three cases. As such, I really don't know what the TypeInitializer property is supposed to be for. Stick with GetConstructor or GetConstructors and you should be OK.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: [2005] Reflected Type Constructor

    TypeInitializer will get the "Shared Sub New()" which is the initializer for the whole type rather than an instance of the class.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Reflected Type Constructor

    Quote Originally Posted by Merrion
    TypeInitializer will get the "Shared Sub New()" which is the initializer for the whole type rather than an instance of the class.
    Aha. Sense it makes young Skywalker.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width