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

99 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using JD.Collections.Extensions;
using JD.Kernel.StaticHelpers;
using JD.Reflection.MefImplementations.Metadata;
namespace JD.Reflection.MefImplementations.Model
{
public class ExportDefinition
{
#region Constants
public const string ExportTypeIdentityMetadataKey = "ExportTypeIdentity";
public const string TypeIdentityMetadataKey = "TypeIdentity";
#endregion
#region Variables
private readonly PartDefinition m_part;
private readonly string m_contractName;
private readonly IReadOnlyDictionary<string, object> m_metadata;
#endregion
#region Constructor
static ExportDefinition()
{
// Some validation
Throw.InvalidProgramIf(typeof (ITypeIdentityMetadata).GetProperty(TypeIdentityMetadataKey) == null, "Inconsistent TypeIdentity Key");
}
public ExportDefinition(PartDefinition part, string contractName, IDictionary<string, object> metadata)
{
Throw.IfNull(part, "part");
Throw.IfEmpty(contractName, "contractName");
m_part = part;
m_contractName = contractName;
m_metadata = metadata == null ? new Dictionary<string, object>() : (IReadOnlyDictionary<string, object>)metadata.AsReadOnly();
}
#endregion
#region Properties
public PartDefinition Part
{
get { return m_part; }
}
public string ContractName
{
get { return m_contractName; }
}
public IReadOnlyDictionary<string, object> Metadata
{
get { return m_metadata; }
}
public string ExportTypeIdentity
{
get
{
object value;
m_metadata.TryGetValue(ExportTypeIdentityMetadataKey, out value);
return (string)value;
}
}
public string TypeIdentity
{
get
{
object value;
m_metadata.TryGetValue(TypeIdentityMetadataKey, out value);
return (string)value;
}
}
#endregion
#region Methods
public virtual object CreatePart()
{
return Activator.CreateInstance(Part.Type);
}
public override string ToString()
{
return $"{TypeIdentity} (Contract = {ContractName})";
}
#endregion
}
}