|
using System;
using System.Collections.Generic;
using System.Text;
namespace Dialogik.Utils.Defines
{
public class Instancer
{
#region Members
private CreateHandler _createHandler;
#endregion
#region Constructors
public Instancer(string hashKey, Type cType)
{
_hashKey = hashKey;
RegisterType(cType, false);
}
public Instancer(string hashKey, Type cType, bool listType)
{
_hashKey = hashKey;
RegisterType(cType, listType);
}
#endregion
#region Property - HashKey
private string _hashKey;
public string HashKey
{
get { return _hashKey; }
set { _hashKey = value; }
}
#endregion
#region Property - ClassType
private Type _classType;
public Type ClassType
{
get { return _classType; }
set { _classType = value; }
}
#endregion
#region Method - RegisterType
private void RegisterType(Type cType, bool buildListType)
{
if (buildListType)
{
// we have to build generic ourselves
Type listBase = typeof(List<>);
Type listType = listBase.MakeGenericType(cType);
_classType = listType;
}
else
{
_classType = cType;
}
_createHandler = MethodCompiler.CreateNewHandler(_classType);
}
#endregion
#region Method - CreateNew
public object CreateNew()
{
object _object = null;
try
{
if (_createHandler != null)
{
_object = _createHandler();
}
else if (_classType != null)
{
_object = Activator.CreateInstance(_classType);
}
}
catch { }
return _object;
}
#endregion
};
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.