Activator Createinstance
I m using RedGate to do some performance evaluation. I notice dynamically creating an instance using Activator.CreateInstance with two constructor parameters is.
Creates an instance of the type whose name is specified, using the named assembly and default constructor.Namespace:
System
Assembly:
mscorlib in mscorlib.dll
public static ObjectHandle CreateInstance
string assemblyName,
string typeName
assemblyName
Type:
System.String
The name of the assembly where the type named typeName is sought. For more information, see the Remarks section. If assemblyName is null, the executing assembly is searched. typeName
The fully qualified name of the preferred type.
Exception
Condition
ArgumentNullExceptionMissingMethodException
No matching public constructor was found. TypeLoadException
typename was not found in assemblyName. FileNotFoundException
assemblyName was not found. MethodAccessException
The caller does not have permission to call this constructor. MemberAccessException
You 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. InvalidComObjectExceptionNotSupportedExceptionBadImageFormatException
assemblyName is not a valid assembly.
-or-
The common language runtime CLR version 2.0 or later is currently loaded, and assemblyName 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.FileLoadException
An assembly or module was loaded twice with two different evidences.
The assembly name or code base is invalid.
Use ObjectHandle.Unwrap to unwrap the return value.
assemblyName can be either of the following:
The simple name of an assembly, without its path or file extension. For example, you would specify TypeExtensions for an assembly whose path and name are. bin TypeExtensions.dll.
The full name of a signed assembly, which consists of its simple name, version, culture, and public key token; for example, TypeExtensions, Version 1.0.0.0, Culture neutral, PublicKeyToken 181869f2f7435b51.
For more information on how the common language runtime identifies and loads assemblies, see How the Runtime Locates Assemblies. For information on using the application configuration file to define assembly locations, see Specifying an Assembly s Location. If assemblyName is found, it is loaded in the default context.
The following example defines a class named Person in an assembly named PersonInfo. Note that the Person class has two constructors, one of which is parameterless.
using System;
public class Person
private string _name;
public Person
public Person string name
this._name name;
public string Name
get return this._name;
set this._name value;
public override string ToString
return this._name;
The following example calls the CreateInstance String, String method to instantiate the Person class. It requires a reference to PersonInfo.dll to be added to the project. Because the CreateInstance String, String method calls the Person class default constructor, the example assigns a value to its Name property.
using System.Runtime.Remoting;
public class Example
public static void Main
ObjectHandle handle Activator.CreateInstance PersonInfo, Person ;
Person p Person handle.Unwrap ;
p.Name Samuel ;
Console.WriteLine p ;
// The example displays the following output:
// Samuel
.NET Framework
Available since 1.1
CreateInstance Overload
Activator Class
System Namespace
Return to top
Show:
Inherited
Protected.
Update: It appears that the access modifiers on TestClass affect the performance of Activator.CreateIntance. My bet would be due to access checks of some.
Gists are just like any repository. 01 May 2015 I was editing a gist recently when I thought. There s got to be a better way. So I tried to clone my gist from the.
Nov 17, 2005 Daniel Moth asked, in his post, how to choose between a generic type parameter vs. a formal parameter of typed as System.Type. For example, the following.
Reflection Examples C This example shows how to dynamically load assembly, how to create object instance, how to invoke method or how to get and set property value.
Just as an example how to add initialization in the constructor: Something similar to: Activator.CreateInstance Type.GetType ConsoleApplication1.Operation1.
Activator.CreateInstance Method (String, String) (System)
Creates an instance of the specified type using the constructor that best matches the specified parameters.Namespace:
System
Assembly:
mscorlib in mscorlib.dll NameDescriptionCreateInstance ActivationContext
Creates an instance of the type designated by the specified ActivationContext object.CreateInstance ActivationContext, String
Creates an instance of the type that is designated by the specified ActivationContext object and activated with the specified custom activation data.CreateInstance AppDomain, String, String
Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly and default constructor.CreateInstance AppDomain, String, String, Boolean, BindingFlags, Binder, Object, CultureInfo, Object
Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly and the constructor that best matches the specified parameters.CreateInstance AppDomain, String, String, Boolean, BindingFlags, Binder, Object, CultureInfo, Object, Evidence
Obsolete.Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly and the constructor that best matches the specified parameters.CreateInstance String, String
Creates an instance of the type whose name is specified, using the named assembly and default constructor.CreateInstance String, String, Boolean, BindingFlags, Binder, Object, CultureInfo, Object
Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the specified parameters.CreateInstance String, String, Boolean, BindingFlags, Binder, Object, CultureInfo, Object, Evidence
Obsolete.Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the specified parameters.CreateInstance String, String, Object
Creates an instance of the type whose name is specified, using the named assembly and default constructor.CreateInstance Type
Creates an instance of the specified type using that type s default constructor.CreateInstance Type, BindingFlags, Binder, Object, CultureInfo
Creates an instance of the specified type using the constructor that best matches the specified parameters.CreateInstance Type, BindingFlags, Binder, Object, CultureInfo, Object
Creates an instance of the specified type using the constructor that best matches the specified parameters.CreateInstance Type, Boolean
Creates an instance of the specified type using that type s default constructor.CreateInstance Type, Object
Creates an instance of the specified type using the constructor that best matches the specified parameters.CreateInstance Type, Object, Object
Creates an instance of the specified type using the constructor that best matches the specified parameters.CreateInstance
Creates an instance of the type designated by the specified generic type parameter, using the parameterless constructor.
Activator Class
System Namespace
Return to top
Show:
Inherited
Protected.
Fast version of Activator.CreateInstance Type and Activator.CreateInstance T methods using IL to improve performance.
This C program demonstrates the Lazy class and the IsValueCreated property. It shows lazy initialization.