JD2022-TU1/main/tools/framework/JD.Reflection/MefImplementations/Model/PartDefinition.cs

130 lines
3.4 KiB
C#

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<ImportDefinition> m_imports;
private IEnumerable<ExportDefinition> m_exports;
#endregion
#region Constructor
internal PartDefinition(IPartCreationInfo partCreationInfo)
{
Throw.IfNull(partCreationInfo, "partCreationInfo");
m_partCreationInfo = partCreationInfo;
}
#endregion
#region Properties
public IEnumerable<ExportDefinition> 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<ImportDefinition> OwnImportDefinitions
{
get { return ImportDefinitions.Where(import => import.GetImportingMember().DeclaringType == Type); }
}
public IEnumerable<ImportDefinition> 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<Type> 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<Type> LazyType
{
get { return new Lazy<Type>(typeof(object)); }
}
public IEnumerable<ExportDefinition> GetExports(PartDefinition part)
{
yield break;
}
public IEnumerable<ImportDefinition> GetImports(PartDefinition part)
{
yield break;
}
}
#endregion
}
}