AppDomain.CreateInstanceAndUnwrap Method (String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
Assembly: mscorlib (in mscorlib.dll)
public Object CreateInstanceAndUnwrap ( string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes )
public Object CreateInstanceAndUnwrap ( String assemblyName, String typeName, boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes )
public function CreateInstanceAndUnwrap ( assemblyName : String, typeName : String, ignoreCase : boolean, bindingAttr : BindingFlags, binder : Binder, args : Object[], culture : CultureInfo, activationAttributes : Object[], securityAttributes : Evidence ) : Object
Not applicable.
Parameters
- assemblyName
The display name of the assembly. See Assembly.FullName.
- typeName
The fully qualified name of the requested type, including the namespace but not the assembly, as returned by the Type.FullName property.
- ignoreCase
A Boolean value specifying whether to perform a case-sensitive search or not.
- bindingAttr
A combination of zero or more bit flags that affect the search for the typeName constructor. If bindingAttr is zero, a case-sensitive search for public constructors is conducted.
- binder
An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects using reflection. If binder is null, the default binder is used.
- args
The arguments to pass to the constructor. This array of arguments must match in number, order, and type the parameters of the constructor to invoke. If the default constructor is preferred, args must be an empty array or null.
- culture
A culture-specific object used to govern the coercion of types. If culture is a null reference (Nothing in Visual Basic), the CultureInfo for the current thread is used.
- activationAttributes
An array of one or more attributes that can participate in activation. Typically, an array that contains a single UrlAttribute object. The UrlAttribute specifies the URL that is required to activate a remote object. For a detailed description of client-activated objects, see Client Activation.
- securityAttributes
Information used to authorize creation of typeName.
Return Value
An instance of the object specified by typeName.| Exception type | Condition |
|---|---|
| assemblyName or typeName is a null reference (Nothing in Visual Basic). | |
| No matching constructor was found. | |
| typename was not found in assemblyName. | |
| assemblyName was not found. | |
| The caller does not have permission to call this constructor. | |
| The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject. | |
| The operation is attempted on an unloaded application domain. | |
| assemblyName is not a valid assembly. -or- Version 2.0 or later of the common language runtime is currently loaded and assemblyName was compiled with a later version. | |
| An assembly or module was loaded twice with two different evidences. |
This is a convenience method that combines CreateInstance and ObjectHandle.Unwrap. This method calls the default constructor for typeName.
See AssemblyName for the format of assemblyName. See the Type.FullName property for the format of typeName.
Note: |
|---|
| If you make an early-bound call to a method M of an object of type T1 that was returned by CreateInstanceAndUnwrap, and that method makes an early-bound call to a method of an object of type T2 in an assembly C other than the current assembly or the assembly containing T1, assembly C is loaded into the current application domain. This loading occurs even if the early-bound call to T1.M() was made in the body of a DynamicMethod, or in other dynamically generated code. If the current domain is the default domain, assembly C cannot be unloaded until the process ends. If the current domain later attempts to load assembly C, the load might fail. |
The following sample demonstrates the use of the ignoreCase parameter.
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, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", "SYSTEM.INT32", ignoreCase, BindingFlags.Default, null, null, null, null, null ); Console.WriteLine(instance.GetType()); } catch (TypeLoadException e) { Console.WriteLine(e.Message); } } }
import System.*;
import System.Reflection.*;
class Test
{
public static void main(String[] args)
{
InstantiateInt32(false); // Failed!
InstantiateInt32(true); // OK!
} //main
static void InstantiateInt32(boolean ignoreCase)
{
try {
AppDomain currentDomain = AppDomain.get_CurrentDomain();
Object instance = currentDomain.CreateInstanceAndUnwrap(
"mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"SYSTEM.INT32",
ignoreCase,
BindingFlags.Default,
null,
null,
null,
null,
null);
Console.WriteLine(instance.GetType());
}
catch (TypeLoadException e) {
Console.WriteLine(e.get_Message());
}
} //InstantiateInt32
} //Test
- FileIOPermissionAccess for the ability to access the location of the assembly. Associated enumeration: FileIOPermissionAccess.PathDiscovery
- FileIOPermissionAccess for the ability to read the file containing the assembly manifest. Associated enumeration: FileIOPermissionAccess.Read
- WebPermission for the ability to access the location of the assembly if the assembly is not local.
- SecurityPermission for the ability to call unmanaged code when creating an instance of a delegate. Associated enumeration: SecurityPermissionFlag.UnmanagedCode
- SecurityPermission to load an assembly with evidence. Associated enumeration: SecurityPermissionFlag.ControlEvidence.
- ReflectionPermission for the ability to invoke operations on all type members. Associated enumeration: ReflectionPermissionFlag.MemberAccess
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: