-
Notifications
You must be signed in to change notification settings - Fork 4
Object Construction
Ricardo Diaz edited this page Aug 7, 2017
·
1 revision
Before continuing it is important that you have read the Getting Started page, in particular the section on how to customize the behavior of Fasterflect using Flags, as well as the general introduction to Accessing with Fasterflect.
Fasterflect allows you to create instances of reference type, struct type and array type via the following overload of CreateInstance(). It also integrates with ConstructorInfo to allow you trigger the Fasterflect engine directly from instances of ConstructorInfo class.
object CreateInstance( this Type type, params object[] parameters );
object CreateInstance( this Type type, Flags bindingFlags, params object[] parameters );
object CreateInstance( this Type type, Type[] parameterTypes, params object[] parameters );
object CreateInstance( this Type type, Type[] parameterTypes, Flags bindingFlags, params object[] parameters );
object CreateInstance( this ConstructorInfo ctorInfo, params object[] parameters );
Sample usages:
// Create a Person array of 15 elements
var array = typeof(Person[]).CreateInstance(15);
// Create a person object with one argument
var person = typeof(Person).CreateInstance("name");
// Create a person via ConstructorInfo
var ctorInfo = typeof(Person).Constructor(new Type[] {typeof(string)}); // lookup the constructor
person = ctorInfo.CreateInstance("name");