// SLF.NET - Simple Logging Façade for .NET
// Contact and Information: http://slf.codeplex.com
//
// This library is free software; you can redistribute it and/or
// modify it in any way you see fit as long as this copyright
// notice is not being removed.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE
using System;
namespace Slf
{
///
/// Provides helper methods that supplement the
/// .
///
internal static class ActivatorUtils
{
///
/// Instantiates an object of the given type.
///
/// The type being instantiated
/// The full type name
/// A logger which is used to log any
/// problems which occur in the instantiation process.
///
internal static T Instantiate(string typeName)
{
Type typeToInstantiate = Type.GetType(typeName);
// check that the type can be located
if (typeToInstantiate == null)
{
return default(T);
}
// create instance of the given type
object instantiatedInstance;
try
{
instantiatedInstance = Activator.CreateInstance(typeToInstantiate);
}
catch (Exception e)
{
return default(T);
}
// make sure we do infact have an item of the correct type
T instantiatedType = default(T);
try
{
instantiatedType = (T)instantiatedInstance;
}
catch (InvalidCastException)
{
}
return instantiatedType;
}
}
}