Bewerken

Activator.CreateInstanceFrom Method

Definition

Creates an instance of the type whose name is specified, using the named assembly file and the constructor that best matches the specified parameters.

Overloads

Name Description
CreateInstanceFrom(String, String)

Creates an instance of the type whose name is specified, using the named assembly file and parameterless constructor.

CreateInstanceFrom(String, String, Object[])

Creates an instance of the type whose name is specified, using the named assembly file and parameterless constructor.

CreateInstanceFrom(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

Creates an instance of the type whose name is specified, using the named assembly file and the constructor that best matches the specified parameters.

CreateInstanceFrom(String, String)

Source:
Activator.cs
Source:
Activator.cs
Source:
Activator.cs
Source:
Activator.cs
Source:
Activator.cs

Creates an instance of the type whose name is specified, using the named assembly file and parameterless constructor.

public:
 static System::Runtime::Remoting::ObjectHandle ^ CreateInstanceFrom(System::String ^ assemblyFile, System::String ^ typeName);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Type and its constructor could be removed")]
public static System.Runtime.Remoting.ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName);
public static System.Runtime.Remoting.ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Type and its constructor could be removed")>]
static member CreateInstanceFrom : string * string -> System.Runtime.Remoting.ObjectHandle
static member CreateInstanceFrom : string * string -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstanceFrom (assemblyFile As String, typeName As String) As ObjectHandle

Parameters

assemblyFile
String

The name of a file that contains an assembly where the type named typeName is sought.

typeName
String

The name of the type to create an instance of.

Returns

A handle that must be unwrapped to access the newly created object, or null for Nullable<T> instances.

Attributes

Exceptions

typeName is null.

No matching public constructor was found.

typeName was not found in assemblyFile.

assemblyFile was not found.

The caller does not have permission to call this constructor.

Cannot create an instance of an abstract class, or this member was invoked with a late-binding mechanism.

The constructor, which was invoked through reflection, threw an exception.

The caller does have the required FileIOPermission.

assemblyFile is not a valid assembly.

-or-

The common language runtime (CLR) version 2.0 or later is currently loaded, and assemblyFile was compiled for a version of the CLR that is later than the currently loaded version. Note that the .NET Framework versions 2.0, 3.0, and 3.5 all use CLR version 2.0.

Examples

The following code example demonstrates how to call the CreateInstanceFrom method. This code example is part of a larger example provided for the Activator class.

// Create an instance of the SomeType class that is defined in this
// assembly.
System.Runtime.Remoting.ObjectHandle oh =
    Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase,
                                 typeof(SomeType).FullName);

// Call an instance method defined by the SomeType type using this object.
SomeType st = (SomeType) oh.Unwrap();

st.DoSomething(5);
// Create an instance of the SomeType class that is defined in this assembly.
let oh = 
    Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().Location, typeof<SomeType>.FullName)

// Call an instance method defined by the SomeType type using this object.
let st = oh.Unwrap() :?> SomeType

st.DoSomething 5
' Create an instance of the SomeType class that is defined in this assembly.
Dim oh As System.Runtime.Remoting.ObjectHandle = _
    Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase, _
                                 GetType(SomeType).FullName)

' Call an instance method defined by the SomeType type using this object.
Dim st As SomeType = CType(oh.Unwrap(), SomeType)

st.DoSomething(5)

Remarks

Use the ObjectHandle.Unwrap method to unwrap the return value.

For information about other exceptions that can be thrown by invoked methods, see the Exceptions section of the Assembly.LoadFrom and CreateInstance methods.

Applies to

CreateInstanceFrom(String, String, Object[])

Source:
Activator.cs
Source:
Activator.cs
Source:
Activator.cs
Source:
Activator.cs
Source:
Activator.cs

Creates an instance of the type whose name is specified, using the named assembly file and parameterless constructor.

public:
 static System::Runtime::Remoting::ObjectHandle ^ CreateInstanceFrom(System::String ^ assemblyFile, System::String ^ typeName, cli::array <System::Object ^> ^ activationAttributes);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Type and its constructor could be removed")]
public static System.Runtime.Remoting.ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, object?[]? activationAttributes);
public static System.Runtime.Remoting.ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, object?[]? activationAttributes);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Type and its constructor could be removed")>]
static member CreateInstanceFrom : string * string * obj[] -> System.Runtime.Remoting.ObjectHandle
static member CreateInstanceFrom : string * string * obj[] -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstanceFrom (assemblyFile As String, typeName As String, activationAttributes As Object()) As ObjectHandle

Parameters

assemblyFile
String

The name of a file that contains an assembly where the type named typeName is sought.

typeName
String

The name of the type to create an instance of.

activationAttributes
Object[]

An array of one or more attributes that can participate in activation. This is typically an array that contains a single UrlAttribute object that specifies the URL that is required to activate a remote object.

This parameter is related to client-activated objects. Client activation is a legacy technology that is retained for backward compatibility but is not recommended for new development. Distributed applications should instead use Windows Communication Foundation.

Returns

A handle that must be unwrapped to access the newly created object, or null for Nullable<T> instances.

Attributes

Exceptions

typeName is null.

No matching public constructor was found.

typeName was not found in assemblyFile.

assemblyFile was not found.

The caller does not have permission to call this constructor.

Cannot create an instance of an abstract class, or this member was invoked with a late-binding mechanism.

The constructor, which was invoked through reflection, threw an exception.

activationAttributes is not an empty array, and the type being created does not derive from MarshalByRefObject.

The caller does have the required FileIOPermission.

assemblyFile is not a valid assembly.

-or-

The common language runtime (CLR) version 2.0 or later is currently loaded, and assemblyFile was compiled for a version of the CLR that is later than the currently loaded version. Note that the .NET Framework versions 2.0, 3.0, and 3.5 all use CLR version 2.0.

Remarks

Use the ObjectHandle.Unwrap method to unwrap the return value.

For information about other exceptions that can be thrown by invoked methods, see the Exceptions section of the Assembly.LoadFrom and CreateInstance methods.

Applies to

CreateInstanceFrom(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

Source:
Activator.cs
Source:
Activator.cs
Source:
Activator.cs
Source:
Activator.cs
Source:
Activator.cs

Creates an instance of the type whose name is specified, using the named assembly file and the constructor that best matches the specified parameters.

public:
 static System::Runtime::Remoting::ObjectHandle ^ CreateInstanceFrom(System::String ^ assemblyFile, System::String ^ typeName, bool ignoreCase, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ args, System::Globalization::CultureInfo ^ culture, cli::array <System::Object ^> ^ activationAttributes);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Type and its constructor could be removed")]
public static System.Runtime.Remoting.ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes);
public static System.Runtime.Remoting.ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Type and its constructor could be removed")>]
static member CreateInstanceFrom : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> System.Runtime.Remoting.ObjectHandle
static member CreateInstanceFrom : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstanceFrom (assemblyFile As String, typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object()) As ObjectHandle

Parameters

assemblyFile
String

The name of a file that contains an assembly where the type named typeName is sought.

typeName
String

The name of the type to create an instance of.

ignoreCase
Boolean

true to specify that the search for typeName is not case-sensitive; false to specify that the search is case-sensitive.

bindingAttr
BindingFlags

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
Binder

An object that uses bindingAttr and args to seek and identify the typeName constructor. If binder is null, the default binder is used.

args
Object[]

An array of arguments that match in number, order, and type the parameters of the constructor to invoke. If args is an empty array or null, the constructor that takes no parameters (the parameterless constructor) is invoked.

culture
CultureInfo

Culture-specific information that governs the coercion of args to the formal types declared for the typeName constructor. If culture is null, the CultureInfo for the current thread is used.

activationAttributes
Object[]

An array of one or more attributes that can participate in activation. This is typically an array that contains a single UrlAttribute object that specifies the URL that is required to activate a remote object.

This parameter is related to client-activated objects. Client activation is a legacy technology that is retained for backward compatibility but is not recommended for new development. Distributed applications should instead use Windows Communication Foundation.

Returns

A handle that must be unwrapped to access the newly created object, or null for Nullable<T> instances.

Attributes

Exceptions

typeName is null.

No matching constructor was found.

typeName was not found in assemblyFile.

assemblyFile was not found.

The caller does not have permission to call this constructor.

Cannot create an instance of an abstract class, or this member was invoked with a late-binding mechanism.

The constructor, which was invoked through reflection, threw an exception.

The caller does not have the required FileIOPermission.

activationAttributes is not an empty array, and the type being created does not derive from MarshalByRefObject.

assemblyFile is not a valid assembly.

-or-

The common language runtime (CLR) version 2.0 or later is currently loaded, and assemblyFile was compiled for a version of the CLR that is later than the currently loaded version. Note that the .NET Framework versions 2.0, 3.0, and 3.5 all use CLR version 2.0.

Remarks

Use the ObjectHandle.Unwrap method to unwrap the return value.

For information about other exceptions that can be thrown by invoked methods, see the Exceptions section of the Assembly.LoadFrom and CreateInstance methods.

Applies to