Object Construction

Introduction

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.

Accessing constructors

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");

Last edited Mar 7, 2010 at 8:53 AM by buunguyen, version 4

Comments

No comments yet.