using System.Collections.Generic; using System.Linq; using JD.Kernel.StaticHelpers; using JD.Reflection.Interfaces; using JD.Reflection.MefImplementations.Model.Internal; using Type = System.Type; namespace JD.Reflection.MefImplementations.Model { public class PartDefinition { #region Variables private readonly IPartCreationInfo m_partCreationInfo; private readonly object m_lock = new object(); private IEnumerable m_imports; private IEnumerable m_exports; #endregion #region Constructor internal PartDefinition(IPartCreationInfo partCreationInfo) { Throw.IfNull(partCreationInfo, "partCreationInfo"); m_partCreationInfo = partCreationInfo; } #endregion #region Properties public IEnumerable ExportDefinitions { get { if (m_exports == null) { ExportDefinition[] exports = m_partCreationInfo.GetExports(this).ToArray(); lock (m_lock) { if (m_exports == null) { m_exports = exports; } } } return m_exports; } } // The import definitions declared exactly on this part (and not on base parts) public IEnumerable OwnImportDefinitions { get { return ImportDefinitions.Where(import => import.GetImportingMember().DeclaringType == Type); } } public IEnumerable ImportDefinitions { get { if (m_imports == null) { ImportDefinition[] imports = m_partCreationInfo.GetImports(this).ToArray(); lock (m_lock) { if (m_imports == null) { m_imports = imports; } } } return m_imports; } } public Type Type { get { return LazyType.Value; } } public Lazy LazyType { get { return m_partCreationInfo.LazyType; } } #endregion #region Static Creation private static readonly PartDefinition ms_empty = new PartDefinition(new EmptyCreationInfo()); public static PartDefinition Empty { get { return ms_empty; } } public static PartDefinition FromType(Type type) { return new PartDefinition(new ReflectionPartCreationInfo(type)); } #endregion #region Inner Types private class EmptyCreationInfo : IPartCreationInfo { public Lazy LazyType { get { return new Lazy(typeof(object)); } } public IEnumerable GetExports(PartDefinition part) { yield break; } public IEnumerable GetImports(PartDefinition part) { yield break; } } #endregion } }