本文整理汇总了C#中CompositionContract类的典型用法代码示例。如果您正苦于以下问题:C# CompositionContract类的具体用法?C# CompositionContract怎么用?C# CompositionContract使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CompositionContract类属于命名空间,在下文中一共展示了CompositionContract类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetExportDescriptors
public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor)
{
// Avoid trying to create defaults-of-defaults-of...
if (contract.ContractName != null && contract.ContractName.StartsWith(Constants.DefaultContractNamePrefix))
return NoExportDescriptors;
var implementations = descriptorAccessor.ResolveDependencies("test for default", contract, false);
if (implementations.Any())
return NoExportDescriptors;
var defaultImplementationDiscriminator = Constants.DefaultContractNamePrefix + (contract.ContractName ?? "");
IDictionary<string, object> copiedConstraints = null;
if (contract.MetadataConstraints != null)
copiedConstraints = contract.MetadataConstraints.ToDictionary(k => k.Key, k => k.Value);
var defaultImplementationContract = new CompositionContract(contract.ContractType, defaultImplementationDiscriminator, copiedConstraints);
CompositionDependency defaultImplementation;
if (!descriptorAccessor.TryResolveOptionalDependency("default", defaultImplementationContract, true, out defaultImplementation))
return NoExportDescriptors;
return new[]
{
new ExportDescriptorPromise(
contract,
"Default Implementation",
false,
() => new[] {defaultImplementation},
_ =>
{
var defaultDescriptor = defaultImplementation.Target.GetDescriptor();
return ExportDescriptor.Create((c, o) => defaultDescriptor.Activator(c, o), defaultDescriptor.Metadata);
})
};
}
示例2: GetExportDescriptors
/// <summary>
/// <see cref="ILogger"/>生成処理
///
/// https://mef.codeplex.com/wikipage?title=ProgrammingModelExtensions&referringTitle=Documentation
/// </summary>
/// <param name="contract"></param>
/// <param name="descriptorAccessor"></param>
/// <returns></returns>
public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor)
{
if (contract.ContractType == typeof(ILogger))
{
string value = string.Empty;
CompositionContract unwrap;
contract.TryUnwrapMetadataConstraint("LogType", out value, out unwrap);
string header = string.Format("[{0}] LogExportDescriptorProvider - ", value);
return new ExportDescriptorPromise[]
{
new ExportDescriptorPromise(
contract,
"ConsoleMef2 ILogger",
true,
NoDependencies,
_ => ExportDescriptor.Create((c, o) => new Logger() { Header = header }, NoMetadata)
)
};
}
else
return NoExportDescriptors;
}
示例3: GetExportDescriptors
public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(
CompositionContract contract,
DependencyAccessor dependencyAccessor)
{
if (contract == null) throw new ArgumentNullException("contract");
if (dependencyAccessor == null) throw new ArgumentNullException("dependencyAccessor");
string key;
CompositionContract unwrapped;
if (!contract.TryUnwrapMetadataConstraint(SettingKey, out key, out unwrapped) ||
!unwrapped.Equals(new CompositionContract(unwrapped.ContractType)) ||
!SupportedSettingTypes.Contains(unwrapped.ContractType) ||
!ConfigurationManager.AppSettings.AllKeys.Contains(key))
yield break;
var value = ConfigurationManager.AppSettings.Get(key);
var converted = Convert.ChangeType(value, contract.ContractType);
yield return new ExportDescriptorPromise(
contract,
"System.Configuration.ConfigurationManager.AppSettings",
true,
NoDependencies,
_ => ExportDescriptor.Create((c, o) => converted, NoMetadata));
}
示例4: GetExportDescriptors
/// <summary>
/// Promise export descriptors for the specified export key.
/// </summary>
/// <param name="contract">The export key required by another component.</param><param name="descriptorAccessor">Accesses the other export descriptors present in the composition.</param>
/// <returns>
/// Promises for new export descriptors.
/// </returns>
/// <remarks>
/// A provider will only be queried once for each unique export key.
/// The descriptor accessor can only be queried immediately if the descriptor being promised is an adapter, such as
/// <see cref="T:System.Lazy`1"/>; otherwise, dependencies should only be queried within execution of the function provided
/// to the <see cref="T:System.Composition.Hosting.Core.ExportDescriptorPromise"/>. The actual descriptors provided should not close over or reference any
/// aspect of the dependency/promise structure, as this should be able to be GC'ed.
/// </remarks>
public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor)
{
var isSupported = this.servicesSupport.GetOrAdd(
contract.ContractType,
type => this.serviceProvider.GetService(type) != null);
if (!isSupported)
{
return ExportDescriptorProvider.NoExportDescriptors;
}
return new[]
{
new ExportDescriptorPromise(
contract,
contract.ContractType.Name,
true, // is shared
ExportDescriptorProvider.NoDependencies,
dependencies => ExportDescriptor.Create(
(c, o) =>
{
var instance = this.serviceProvider.GetService(contract.ContractType);
var disposable = instance as IDisposable;
if (disposable != null)
{
c.AddBoundInstance(disposable);
}
return instance;
},
ExportDescriptorProvider.NoMetadata))
};
}
示例5: TryGetSingleForExport
public bool TryGetSingleForExport(CompositionContract exportKey, out ExportDescriptor defaultForExport)
{
ExportDescriptor[] allForExport;
if (!_partDefinitions.TryGetValue(exportKey, out allForExport))
{
lock (_thisLock)
{
if (!_partDefinitions.ContainsKey(exportKey))
{
var updatedDefinitions = new Dictionary<CompositionContract, ExportDescriptor[]>(_partDefinitions);
var updateOperation = new ExportDescriptorRegistryUpdate(updatedDefinitions, _exportDescriptorProviders);
updateOperation.Execute(exportKey);
_partDefinitions = updatedDefinitions;
}
}
allForExport = (ExportDescriptor[])_partDefinitions[exportKey];
}
if (allForExport.Length == 0)
{
defaultForExport = null;
return false;
}
// This check is duplicated in the update process- the update operation will catch
// cardinality violations in advance of this in all but a few very rare scenarios.
if (allForExport.Length != 1)
throw ThrowHelper.CardinalityMismatch_TooManyExports(exportKey.ToString());
defaultForExport = allForExport[0];
return true;
}
示例6: CompositionDependency
private CompositionDependency(CompositionContract contract, ExportDescriptorPromise target, bool isPrerequisite, object site)
{
_target = target;
_isPrerequisite = isPrerequisite;
_site = site;
_contract = contract;
}
示例7: GetExportDescriptors
public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor)
{
if (contract == null) throw new ArgumentNullException("contract");
if (descriptorAccessor == null) throw new ArgumentNullException("descriptorAccessor");
string name;
CompositionContract unwrapped;
var connectionStringSettings = ConfigurationManager.ConnectionStrings.OfType<ConnectionStringSettings>().ToArray();
if (!contract.TryUnwrapMetadataConstraint(NameKey, out name, out unwrapped) ||
!unwrapped.Equals(new CompositionContract(unwrapped.ContractType)) ||
!(unwrapped.ContractType == typeof(string) || typeof(DbConnectionStringBuilder).IsAssignableFrom(unwrapped.ContractType)) ||
!connectionStringSettings.Any(cs => cs.Name == name))
yield break;
var stringValue = connectionStringSettings.Single(cs => cs.Name == name).ConnectionString;
object value = stringValue;
if (contract.ContractType != typeof(string))
{
var stringBuilder = Activator.CreateInstance(contract.ContractType) as DbConnectionStringBuilder;
if (stringBuilder == null) yield break;
stringBuilder.ConnectionString = stringValue;
value = stringBuilder;
}
yield return new ExportDescriptorPromise(
contract,
"System.Configuration.ConfigurationManager.ConnectionStrings",
true,
NoDependencies,
_ => ExportDescriptor.Create((c, o) => value, NoMetadata));
}
示例8: Missing
/// <summary>
/// Construct a placeholder for a missing dependency. Note that this is different
/// from an optional dependency - a missing dependency is an error.
/// </summary>
/// <param name="site">A marker used to identify the individual dependency among
/// those on the dependent part.</param>
/// <param name="contract">The contract required by the dependency.</param>
public static CompositionDependency Missing(CompositionContract contract, object site)
{
Requires.NotNull(contract, nameof(contract));
Requires.NotNull(site, nameof(site));
return new CompositionDependency(contract, site);
}
示例9: ChangingTheTypeOfAContractPreservesTheContractName
public void ChangingTheTypeOfAContractPreservesTheContractName()
{
var name = "a";
var c = new CompositionContract(typeof(object), name);
var d = c.ChangeType(typeof(AType));
Assert.Equal(name, d.ContractName);
}
示例10: ResolveRequiredDependency
/// <summary>
/// Resolve a required dependency on exactly one implemenation of a contract.
/// </summary>
/// <param name="site">A tag describing the dependency site.</param>
/// <param name="contract">The contract required by the site.</param>
/// <param name="isPrerequisite">True if the dependency must be satisifed before corresponding exports can be retrieved; otherwise, false.</param>
/// <returns>The dependency.</returns>
public CompositionDependency ResolveRequiredDependency(object site, CompositionContract contract, bool isPrerequisite)
{
CompositionDependency result;
if (!TryResolveOptionalDependency(site, contract, isPrerequisite, out result))
return CompositionDependency.Missing(contract, site);
return result;
}
示例11: Oversupplied
/// <summary>
/// Construct a placeholder for an "exactly one" dependency that cannot be
/// configured because multiple target implementations exist.
/// </summary>
/// <param name="site">A marker used to identify the individual dependency among
/// those on the dependent part.</param>
/// <param name="targets">The targets found when expecting only one.</param>
/// <param name="contract">The contract required by the dependency.</param>
public static CompositionDependency Oversupplied(CompositionContract contract, IEnumerable<ExportDescriptorPromise> targets, object site)
{
Requires.NotNull(targets, nameof(targets));
Requires.NotNull(site, nameof(site));
Requires.NotNull(contract, nameof(contract));
return new CompositionDependency(contract, targets, site);
}
示例12: Satisfied
/// <summary>
/// Construct a dependency on the specified target.
/// </summary>
/// <param name="target">The export descriptor promise from another part
/// that this part is dependent on.</param>
/// <param name="isPrerequisite">True if the dependency is a prerequisite
/// that must be satisfied before any exports can be retrieved from the dependent
/// part; otherwise, false.</param>
/// <param name="site">A marker used to identify the individual dependency among
/// those on the dependent part.</param>
/// <param name="contract">The contract required by the dependency.</param>
public static CompositionDependency Satisfied(CompositionContract contract, ExportDescriptorPromise target, bool isPrerequisite, object site)
{
Requires.NotNull(target, nameof(target));
Requires.NotNull(site, nameof(site));
Requires.NotNull(contract, nameof(contract));
return new CompositionDependency(contract, target, isPrerequisite, site);
}
示例13: ResolveDependencies
/// <summary>
/// Resolve dependencies on all implementations of a contract.
/// </summary>
/// <param name="site">A tag describing the dependency site.</param>
/// <param name="contract">The contract required by the site.</param>
/// <param name="isPrerequisite">True if the dependency must be satisifed before corresponding exports can be retrieved; otherwise, false.</param>
/// <returns>Dependencies for all implementations of the contact.</returns>
public IEnumerable<CompositionDependency> ResolveDependencies(object site, CompositionContract contract, bool isPrerequisite)
{
var all = GetPromises(contract).ToArray();
var result = new CompositionDependency[all.Length];
for (var i = 0; i < all.Length; ++i)
result[i] = CompositionDependency.Satisfied(contract, all[i], isPrerequisite, site);
return result;
}
示例14: GetExportDescriptors
public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor descriptorAccessor )
{
// var type = convention( contract.ContractType ) ?? contract.ContractType;
contract.ContractType
.Append( convention( contract.ContractType ) )
.WhereAssigned()
.Distinct()
.Each( Initializer );
yield break;
}
示例15: GetExportDescriptors
public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor)
{
if (!contract.Equals(_supportedContract))
return NoExportDescriptors;
var implementations = descriptorAccessor.ResolveDependencies("test for existing", contract, false);
if (implementations.Any())
return NoExportDescriptors;
return new[] { new ExportDescriptorPromise(contract, "test metadataProvider", false, NoDependencies, _ => ExportDescriptor.Create((c, o) => DefaultObject, NoMetadata)) };
}