[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?
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.
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.
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:
Public Class Class1
End Class
Public Class Class2
Public Sub New()
End Sub
End Class
Public Class Class3
Public Sub New(ByVal someParameter As Object)
End Sub
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:
Dim type1 As Type = GetType(Class1)
Dim type2 As Type = GetType(Class2)
Dim type3 As Type = GetType(Class3)
MessageBox.Show((type1.TypeInitializer IsNot Nothing).ToString(), "Class1")
MessageBox.Show((type2.TypeInitializer IsNot Nothing).ToString(), "Class2")
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.
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.
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.