Creating an instance with parameterless constructor : BindingFlags « Reflection « VB.Net






Creating an instance with parameterless constructor

 

Imports System
Imports System.Reflection
Imports System.IO


Public Class TestClass
    Public Name As String
    Private values() As [Object] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

    Default Public Property Item(ByVal index As Integer) As [Object]
        Get
            Return values(index)
        End Get
        Set(ByVal Value As [Object])
            values(index) = Value
        End Set
    End Property

    Public ReadOnly Property Value() As [Object]
        Get
            Return "the value"
        End Get
    End Property

    Public Sub New(ByVal initName As String)
        Name = initName
    End Sub

    Public Sub New()
        MyClass.New("initialName")
    End Sub

    Private methodCalled As Integer = 0

    Public Shared Sub SayHello()
        Console.WriteLine("Hello")
    End Sub

    Public Sub AddUp()
        methodCalled += 1
        Console.WriteLine("AddUp Called {0} times", methodCalled)
    End Sub

    Public Shared Function ComputeSum(ByVal d1 As Double, ByVal d2 As Double) As Double
        Return d1 + d2
    End Function

    Public Shared Sub PrintName(ByVal firstName As [String], ByVal lastName As [String])
        Console.WriteLine("{0},{1}", lastName, firstName)
    End Sub

    Public Sub PrintTime()
        Console.WriteLine(DateTime.Now)
    End Sub

    Public Sub Swap(ByRef a As Integer, ByRef b As Integer)
        Dim x As Integer = a
        a = b
        b = x
    End Sub
End Class

<DefaultMemberAttribute("PrintTime")> _
Public Class TestClass2

    Public Sub PrintTime()
        Console.WriteLine(DateTime.Now)
    End Sub
End Class

Public Class Base
    Shared BaseOnlyPrivate As Integer = 0
    Protected Shared BaseOnly As Integer = 0
End Class

Public Class Derived
    Inherits Base
    Public Shared DerivedOnly As Integer = 0
End Class

Public Class MostDerived
    Inherits Derived
End Class



Class Invoke

    Public Shared Sub Main()
        Dim t As Type = GetType(TestClass)
        Dim c As New TestClass()
        Dim args() As Object = {100.09, 184.45}
        Dim obj As Object = GetType(TestClass).InvokeMember("TestClass", BindingFlags.CreateInstance, _
            Nothing, Nothing, New Object() {})
        Console.WriteLine("Instance of {0} created.", obj.GetType().Name)
    End Sub
End Class

   
  








Related examples in the same category

1.BindingFlags Enumeration
2.Call an instance method
3.Call a method with parameters
4.BindingFlags.GetField, SetField
5.Get an indexed property value
6.Get a field or property
7.Invoking a method with named parameters
8.Call the default member of a type
9.Invoking a method with ByRef parameters
10.Creating an instance with a constructor that has parameters
11.DeclaredOnly instance members
12.Using IgnoreCase and invoking the PrintName method
13.Using FlattenHierarchy to get inherited static protected and public members
14.Without FlattenHierarchy