Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

AppDomain.CreateInstanceAndUnwrap Method

Creates a new instance of a specified type.

Overload List

Creates a new instance of the specified type. Parameters specify the assembly where the type is defined, and the name of the type.

[Visual Basic] Overloads Public Function CreateInstanceAndUnwrap(String, String) As Object
[C#] public object CreateInstanceAndUnwrap(string, string);
[C++] public: Object* CreateInstanceAndUnwrap(String*, String*);
[JScript] public function CreateInstanceAndUnwrap(String, String) : Object;

Creates a new instance of the specified type. Parameters specify the assembly where the type is defined, the name of the type, and an array of activation attributes.

[Visual Basic] Overloads Public Function CreateInstanceAndUnwrap(String, String, Object()) As Object
[C#] public object CreateInstanceAndUnwrap(string, string, object[]);
[C++] public: Object* CreateInstanceAndUnwrap(String*, String*, Object[]);
[JScript] public function CreateInstanceAndUnwrap(String, String, Object[]) : Object;

Creates a new instance of the specified type. Parameters specify the name of the type, and how it is found and created.

[Visual Basic] Overloads Public Function CreateInstanceAndUnwrap(String, String, Boolean, BindingFlags, Binder, Object(), CultureInfo, Object(), Evidence) As Object
[C#] public object CreateInstanceAndUnwrap(string, string, bool, BindingFlags, Binder, object[], CultureInfo, object[], Evidence);
[C++] public: Object* CreateInstanceAndUnwrap(String*, String*, bool, BindingFlags, Binder*, Object[], CultureInfo*, Object[], Evidence*);
[JScript] public function CreateInstanceAndUnwrap(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence) : Object;

Example

[Visual Basic, C#, C++] The following sample demonstrates the use of the ignoreCase parameter.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of CreateInstanceAndUnwrap. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Reflection

Module Test

   Sub Main()
      InstantiateINT32(False)    ' Failed!
      InstantiateINT32(True)    ' OK!
   End Sub 'Main

   Sub InstantiateINT32(ignoreCase As Boolean)
      Try
         Dim currentDomain As AppDomain = AppDomain.CurrentDomain
         Dim instance As Object = currentDomain.CreateInstanceAndUnwrap( _
            "mscorlib", _
            "SYSTEM.INT32", _
            ignoreCase, _
            BindingFlags.Default, _
            Nothing, _
            Nothing, _
            Nothing, _
            Nothing, _
            Nothing  _
         )
         Console.WriteLine(instance.GetType())
      Catch e As TypeLoadException
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateINT32

End Module 'Test

[C#] 
using System;
using System.Reflection;

class Test {

   static void Main() {
      InstantiateINT32(false);     // Failed!
      InstantiateINT32(true);      // OK!
   }
   
   static void InstantiateINT32(bool ignoreCase) {
      try {
         AppDomain currentDomain = AppDomain.CurrentDomain;
         object instance = currentDomain.CreateInstanceAndUnwrap(
            "mscorlib",
            "SYSTEM.INT32",
            ignoreCase,
            BindingFlags.Default,
            null,
            null,
            null,
            null,
            null
         );
         Console.WriteLine(instance.GetType());
      } catch (TypeLoadException e) {
         Console.WriteLine(e.Message);
      }
   }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

static void InstantiateINT32(bool ignoreCase) {
   try {
      AppDomain*  currentDomain = AppDomain::CurrentDomain;
      Object* instance = currentDomain->CreateInstanceAndUnwrap(S"mscorlib",
         S"SYSTEM.INT32",
         ignoreCase,
         BindingFlags::Default,
         0,
         0,
         0,
         0,
         0);
      Console::WriteLine(instance->GetType());
   } catch (TypeLoadException* e) {
      Console::WriteLine(e->Message);
   }
}

int main() {
   InstantiateINT32(false);     // Failed!
   InstantiateINT32(true);      // OK!
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

AppDomain Class | AppDomain Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.