Mercurial > silverbladetech
view MetroWpf/MetroWpf.Framework/Extensions/TypeExtensions.cs @ 24:a8b50a087544
Stocks and FxRates working, new menu introduced. Working nicely so far
author | adminsh@apollo |
---|---|
date | Tue, 20 Mar 2012 20:18:35 +0000 |
parents | 060f02cd4591 |
children |
line wrap: on
line source
using System; using System.Reflection; namespace MetroWpf { /// <summary> /// Extension methods for the reflection meta data type "Type" /// </summary> public static class TypeExtensions { #region · Extensions · /// <summary> /// Creates and returns an instance of the desired type /// </summary> /// <param name="type">The type to be instanciated.</param> /// <param name="constructorParameters">Optional constructor parameters</param> /// <returns>The instanciated object</returns> /// <example> /// <code> /// var type = Type.GetType(".NET full qualified class Type") /// var instance = type.CreateInstance(); /// </code> /// </example> public static object CreateInstance(this Type type, params object[] constructorParameters) { return CreateInstance<object>(type, constructorParameters); } /// <summary> /// Creates and returns an instance of the desired type casted to the generic parameter type T /// </summary> /// <typeparam name="T">The data type the instance is casted to.</typeparam> /// <param name="type">The type to be instanciated.</param> /// <param name="constructorParameters">Optional constructor parameters</param> /// <returns>The instanciated object</returns> /// <example> /// <code> /// var type = Type.GetType(".NET full qualified class Type") /// var instance = type.CreateInstance<IDataType>(); /// </code> /// </example> public static T CreateInstance<T>(this Type type, params object[] constructorParameters) { var instance = Activator.CreateInstance(type, constructorParameters); return (T)instance; } #endregion } }