dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] How to get a constructor by reflection

  1. #1
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    [RESOLVED] How to get a constructor by reflection

    I have this reflection code on the constructor of a virtual class named Mother:

    Code:
        Public MustInherit Class Mother
          
            Public MustInherit Class MotherParameters
            End Class 'MotherParameters
    
            ''' <summary>  
            ''' Do not add any user code to your implementation of this constructor.
            ''' This constructor must contain one line of code which calls MyBase.New(Parameters). 
            ''' adding any other code will trow an exception. Use Initializator(...) instead. 
            ''' </summary>  
            ''' <param name="Parameters">Generic parameters who should inherit MotherParameters class.</param>  
            ''' <remarks>Parameters will be passed to Initializator(...) method. Use it for class initialization.</remarks>  
            Public Sub New(ByRef Parameters As MotherParameters)
    
                'Use reflection to compare the constructor of the current item to that of a  
                'predefined "template" item which has the desired constructor layout  
                Dim constructiontypes() As Type = New System.Type() {GetType(MotherParameters)}
                Dim sourceType As Type = Me.GetType
                
                If sourceType.GetConstructors.Count = 1 Then
                    Dim c1 As System.Reflection.ConstructorInfo
                    c1 = sourceType.GetConstructor(constructiontypes)
    the objective is to get the constructor of the running inherited class in this line:
    Code:
    c1 = sourceType.GetConstructor(constructiontypes)
    It returns C1=Nothing because MotherParameters is a virtual class
    instead of a type like string or integer.

    Is there a work around?
    [Vb.NET 2008 (ex Express)]

  2. #2
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: How to get a constructor by reflection

    i think it is a ByRef-parameter in the abstract Mother class constructor. since the argument is a reference type, specifying ByRef as pass type will make it a pointer to pointer to MotherParameters.

    you may change the statement into this.

    Code:
    Dim constructiontypes() As Type = New System.Type() {system.type.gettype(GetType(MotherParameters).fullname & "&")}
    Busy

  3. #3
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: How to get a constructor by reflection

    Quote Originally Posted by Thread1 View Post
    i think it is a ByRef-parameter in the abstract Mother class constructor. since the argument is a reference type, specifying ByRef as pass type will make it a pointer to pointer to MotherParameters.

    you may change the statement into this.

    Code:
    Dim constructiontypes() As Type = New System.Type() {system.type.gettype(GetType(MotherParameters).fullname & "&")}
    That was the problem. Thanks.
    [Vb.NET 2008 (ex Express)]

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)