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

Activator.CreateInstanceFrom Method (String, String, Object[])

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Shared Function CreateInstanceFrom( _
   ByVal assemblyFile As String, _
   ByVal typeName As String, _
   ByVal activationAttributes() As Object _
) As ObjectHandle
[C#]
public static ObjectHandle CreateInstanceFrom(
 string assemblyFile,
 string typeName,
 object[] activationAttributes
);
[C++]
public: static ObjectHandle* CreateInstanceFrom(
 String* assemblyFile,
 String* typeName,
 Object* activationAttributes __gc[]
);
[JScript]
public static function CreateInstanceFrom(
   assemblyFile : String,
 typeName : String,
 activationAttributes : Object[]
) : ObjectHandle;

Parameters

assemblyFile
The name of a file that contains an assembly where the type named typeName is sought.
typeName
The name of the preferred type.
activationAttributes
An array of one or more attributes that can participate in activation.

Return Value

A handle that must be unwrapped to access the newly created instance.

Exceptions

Exception Type Condition
ArgumentNullException typeName is a null reference (Nothing in Visual Basic).
MissingMethodException No matching public constructor was found.
TypeLoadException typename was not found in assemblyFile.
FileNotFoundException assemblyFile was not found.
MethodAccessException The caller does not have permission to call this constructor.
MemberAccessException Cannot create an instance of an abstract class or this member was invoked with a late-binding mechanism.
TargetInvocationException The constructor, which was invoked through reflection, threw an exception.
NotSupportedException activationAttributes is not an empty array and the type being created does not derive from MarshalByRefObject.
SecurityException The caller does have the required FileIOPermission.

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.

Example

[Visual Basic, C#, JScript] The following example demonstrates an overload of the CreateInstanceFrom method.

[Visual Basic] 
Dim hdlSample As ObjectHandle
Dim myExtenderInterface As IMyExtenderInterface
Dim activationAttributes() = {New SynchronizationAttribute()}

' Assumes that SampleAssembly.dll exists in the same directory as this assembly.
hdlSample = Activator.CreateInstanceFrom("SampleAssembly.dll", "SampleNamespace.SampleClass", activationAttributes)
' Assumes that the SampleClass implements an interface provided by
' this application.
myExtenderInterface = CType(hdlSample.Unwrap(), IMyExtenderInterface)
Console.WriteLine(myExtenderInterface.SampleMethod("Bill"))

[C#] 
ObjectHandle            hdlSample;
IMyExtenderInterface    myExtenderInterface;
object[]                activationAttributes = {new SynchronizationAttribute()};

// Assumes that SampleAssembly.dll exists in the same directory as this assembly.
hdlSample = Activator.CreateInstanceFrom("SampleAssembly.dll", "SampleNamespace.SampleClass", activationAttributes);
// Assumes that the SampleClass implements an interface provided by
// this application.
myExtenderInterface = (IMyExtenderInterface)hdlSample.Unwrap();
Console.WriteLine(myExtenderInterface.SampleMethod("Bill"));

[JScript] 
var hdlSample : ObjectHandle;
var myExtenderInterface : IMyExtenderInterface;
var activationAttributes : Object[] = [new SynchronizationAttribute()];

// Assumes that SampleAssembly.dll exists in the same directory as this assembly.
hdlSample = Activator.CreateInstanceFrom("SampleAssembly.dll", "SampleNamespace.SampleClass", activationAttributes);
// Assumes that the SampleClass implements an interface provided by
// this application.
myExtenderInterface = IMyExtenderInterface(hdlSample.Unwrap());
Console.WriteLine(myExtenderInterface.SampleMethod("Bill"));

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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

  • SecurityPermission for the ability to call unmanaged code when creating an instance of a delegate. Associated enumeration: SecurityPermissionFlag/UnmanagedCode
  • ReflectionPermission for the ability to invoke operations on all type members. Associated enumeration: ReflectionPermissionFlag/MemberAccess
  • FileIOPermission for the ability to search directory paths and read their contents. Associated enumerations: FileIOPermissionAccess/PathDiscovery and Read

See Also

Activator Class | Activator Members | System Namespace | Activator.CreateInstanceFrom Overload List

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