comparison MetroWpf/MetroWpf.Framework/Extensions/TypeExtensions.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
comparison
equal deleted inserted replaced
14:741981715d94 15:060f02cd4591
1 using System;
2 using System.Reflection;
3
4 namespace MetroWpf
5 {
6 /// <summary>
7 /// Extension methods for the reflection meta data type "Type"
8 /// </summary>
9 public static class TypeExtensions
10 {
11 #region · Extensions ·
12
13 /// <summary>
14 /// Creates and returns an instance of the desired type
15 /// </summary>
16 /// <param name="type">The type to be instanciated.</param>
17 /// <param name="constructorParameters">Optional constructor parameters</param>
18 /// <returns>The instanciated object</returns>
19 /// <example>
20 /// <code>
21 /// var type = Type.GetType(".NET full qualified class Type")
22 /// var instance = type.CreateInstance();
23 /// </code>
24 /// </example>
25 public static object CreateInstance(this Type type, params object[] constructorParameters)
26 {
27 return CreateInstance<object>(type, constructorParameters);
28 }
29
30 /// <summary>
31 /// Creates and returns an instance of the desired type casted to the generic parameter type T
32 /// </summary>
33 /// <typeparam name="T">The data type the instance is casted to.</typeparam>
34 /// <param name="type">The type to be instanciated.</param>
35 /// <param name="constructorParameters">Optional constructor parameters</param>
36 /// <returns>The instanciated object</returns>
37 /// <example>
38 /// <code>
39 /// var type = Type.GetType(".NET full qualified class Type")
40 /// var instance = type.CreateInstance&lt;IDataType&gt;();
41 /// </code>
42 /// </example>
43 public static T CreateInstance<T>(this Type type, params object[] constructorParameters)
44 {
45 var instance = Activator.CreateInstance(type, constructorParameters);
46 return (T)instance;
47 }
48
49 #endregion
50 }
51 }