//******************************************************************************************************
// FastObjectFactory.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/eclipse-1.0.php
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 01/25/2010 - James R. Carroll
// Generated original version of source code.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Reflection.Emit;
namespace GSF
{
///
/// Quickly creates new objects based on specified type.
///
/// Type of object to create quickly.
///
/// You can use the alternate implementation if you only have the of
/// an object available (such as when you are using reflection).
///
public static class FastObjectFactory where T : class, new()
{
// Static object creation delegate specific to type T - one instance will be created per type by the compiler
private static readonly Func s_createObjectFunction;
static FastObjectFactory()
{
// This is markedly faster than using Activator.CreateInstance
Type type = typeof(T);
DynamicMethod dynMethod = new DynamicMethod("ctor$" + type.Name, type, null, type);
ILGenerator ilGen = dynMethod.GetILGenerator();
ilGen.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
ilGen.Emit(OpCodes.Ret);
s_createObjectFunction = (Func)dynMethod.CreateDelegate(typeof(Func));
}
///
/// Gets delegate that quickly creates new instance of the specfied type.
///
public static Func CreateObjectFunction
{
get
{
return s_createObjectFunction;
}
}
}
///
/// Quickly creates new objects based on specified type.
///
///
/// should be used when you only have the of an object available (such as when you are
/// using reflection), otherwise you should use the generic .
///
public static class FastObjectFactory
{
// We cache object creation functions by type so they are only created once
private static readonly ConcurrentDictionary s_createObjectFunctions = new ConcurrentDictionary();
///
/// Gets delegate that creates new instance of the .
///
/// Type of object to create quickly.
/// Delegate to use to quickly create new objects.
/// does not support parameterless public constructor.
public static Func