using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.Common;
namespace SuperSocket.SocketBase.Provider
{
///
/// Export Factory
///
[Serializable]
public class ExportFactory
{
///
/// Gets or sets the type.
///
///
/// The type.
///
public string TypeName { get; set; }
private Type m_LoadedType;
[NonSerialized]
private object m_Instance;
///
/// Initializes a new instance of the class.
///
public ExportFactory()
{
}
///
/// Initializes a new instance of the class.
///
/// The instance.
public ExportFactory(object instance)
{
m_Instance = instance;
}
///
/// Initializes a new instance of the class.
///
/// Name of the type.
public ExportFactory(string typeName)
{
TypeName = typeName;
}
///
/// Ensures the instance's existance.
///
public void EnsureInstance()
{
if (m_Instance != null)
return;
m_Instance = CreateInstance();
}
private object CreateInstance()
{
if (m_LoadedType == null)
{
m_LoadedType = AssemblyUtil.GetType(TypeName, true, true);
}
return Activator.CreateInstance(m_LoadedType);
}
///
/// Creates the export type instance.
///
///
///
public T CreateExport()
{
if (m_Instance != null)
return (T)m_Instance;
return (T)CreateInstance();
}
///
/// Creates the export type instance from the instance creator.
///
///
/// The creator.
///
public T CreateExport(Func creator)
{
if (m_LoadedType == null)
{
m_LoadedType = AssemblyUtil.GetType(TypeName, true, true);
}
return (T)creator(m_LoadedType);
}
}
}