Export (0) Print
Expand All

Assembly.CreateInstance Method (String, Boolean)

Locates the specified type from this assembly and creates an instance of it using the system activator, with optional case-sensitive search.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Function CreateInstance ( _
	typeName As String, _
	ignoreCase As Boolean _
) As Object

Parameters

typeName
Type: System.String

The Type.FullName of the type to locate.

ignoreCase
Type: System.Boolean

true to ignore the case of the type name; otherwise, false.

Return Value

Type: System.Object
An instance of the specified type created with the default constructor; or Nothing if typeName is not found. The type is resolved using the default binder, without specifying culture or activation attributes, and with BindingFlags set to Public or Instance.

Implements

_Assembly.CreateInstance(String, Boolean)

ExceptionCondition
ArgumentException

typeName is an empty string ("") or a string beginning with a null character.

-or-

The current assembly was loaded into the reflection-only context.

MissingMethodException

No matching constructor was found.

ArgumentNullException

typeName is Nothing.

FileNotFoundException

typeName requires a dependent assembly that could not be found.

FileLoadException

typeName requires a dependent assembly that was found but could not be loaded.

-or-

The current assembly was loaded into the reflection-only context, and typeName requires a dependent assembly that was not preloaded.

BadImageFormatException

typeName requires a dependent assembly, but the file is not a valid assembly.

-or-

typeName requires a dependent assembly that was compiled for a version of the runtime that is later than the currently loaded version.

If the runtime is unable to find typeName in the Assembly instance, it returns Nothing instead of throwing an exception. This might happen because:

  • You haven't specified the fully qualified name of the type.

  • The type doesn’t exist in the current Assembly instance.

The following example defines a Person class. It then calls the CreateInstance(String) method to instantiate it, but because the casing of the typeName argument doesn’t match that of the type's FullName property, the method returns Nothing. When the example passes the same string to the CreateInstance(String, Boolean) overload and specifies that the comparison should be case-insensitive, the Person class is found, and a Person object is successfully instantiated.

Imports System.Reflection
Imports Contoso.Libraries

Namespace Contoso.Libraries
   Public Class Person
      Private _name As String  

      Public Sub New()
      End Sub  

      Public Sub New(name As String)
         Me._name = name
      End Sub  

      Public Property Name As String  
         Get  
            Return Me._name
         End Get  
         Set  
            Me._name = value
         End Set  
      End Property  

      Public Overrides Function ToString() As String  
         Return Me._name
      End Function  
   End Class 
End Namespace  

Module Example
   Public Sub Main()
      Dim fullName As String = "contoso.libraries.person" 
      Dim assem As Assembly = GetType(Person).Assembly
      Dim p As Person = CType(assem.CreateInstance(fullName),
                              Person)
      If p IsNot Nothing Then
         p.Name = "John"
         Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                           p.GetType().Name, p)
      Else
         Console.WriteLine("Unable to instantiate a Person object" +
                           "with Assembly.CreateInstance(String)")
         ' Try case-insensitive type name comparison.
         p = CType(assem.CreateInstance(fullName, true), Person)
         If p IsNot Nothing Then 
            p.Name = "John"
            Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                              p.GetType().Name, p)
         Else 
            Console.WriteLine("Unable to instantiate a {0} object.", 
                              fullName)
         End If    
      End If    
   End Sub 
End Module 
' The example displays the following output: 
'    Unable to instantiate a Person object with Assembly.CreateInstance(String) 
'    Instantiated a Person object whose value is 'John'

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Show:
© 2015 Microsoft