/// <summary>
 /// Initializes the context from a part and the export.
 /// </summary>
 internal DecoratedExport(ComposablePartDefinition part, ExportDefinition export)
 {
     this.ExportDefinition = export;
     this.ExportingMember  = ReflectionModelServices.GetExportingMember(export);
     this.ExportingType    = ReflectionModelServices.GetPartType(part);
     this.NewMetadata      = new Dictionary <string, object>(export.Metadata);
 }
示例#2
0
 private ExportDefinition CreateWrapped(ExportDefinition export, Type type)
 {
     return(ReflectionModelServices.CreateExportDefinition(
                this.CreateWrapped(ReflectionModelServices.GetExportingMember(export), type),
                export.ContractName,
                export.Metadata.AsLazy(),
                null));
 }
示例#3
0
        public static T GetExport <T>(this object obj, string propertyName, string contractName)
        {
            LazyMemberInfo info = new LazyMemberInfo(obj.GetType().GetProperty(propertyName));
            var            ed   = ReflectionModelServices.CreateExportDefinition(info, contractName, null, null);
            var            ed1  = ReflectionModelServices.GetExportingMember(ed);
            T someService       = (T)(ed1.GetAccessors().GetValue(0) as System.Reflection.MethodInfo).Invoke(obj, null);

            return((T)someService);
        }
        public static IDictionary <string, object> WriteExportDefinition(ComposablePartDefinition owner, ExportDefinition exportDefinition)
        {
            Assumes.NotNull(owner);
            Assumes.NotNull(exportDefinition);

            LazyMemberInfo exportingMemberInfo = ReflectionModelServices.GetExportingMember(exportDefinition);

            IDictionary <string, object> cache = new Dictionary <string, object>();

            cache.WriteContractName(exportDefinition.ContractName);
            cache.WriteMetadata(exportDefinition.Metadata);
            cache.WriteValue(AttributedCacheServices.CacheKeys.MemberType, exportingMemberInfo.MemberType, MemberTypes.TypeInfo);
            cache.WriteLazyAccessors(exportingMemberInfo.GetAccessors(), ReflectionModelServices.GetPartType(owner));

            return(cache);
        }
 /// <summary>
 /// Returns <c>true</c> if the type given by <paramref name="implementationType"/> is already available as an export
 /// of <paramref cref="contractType"/> in the container.
 /// </summary>
 /// <param name="container"></param>
 /// <param name="contractType"></param>
 /// <param name="implementationType"></param>
 /// <returns></returns>
 bool IsTypeRegistered(TInternalContainer container, Type contractType, Type implementationType)
 {
     return(container.GetExports(
                new ContractBasedImportDefinition(
                    AttributedModelServices.GetContractName(contractType),
                    AttributedModelServices.GetTypeIdentity(contractType),
                    null,
                    ImportCardinality.ZeroOrMore,
                    false,
                    false,
                    CreationPolicy.Any))
            .Select(i =>
                    ReflectionModelServices.GetExportingMember(i.Definition))
            .Where(i =>
                   // export must be associated with a Type, implemented by the specified type
                   i.MemberType == MemberTypes.TypeInfo &&
                   i.GetAccessors().Any(j => ((TypeInfo)j).UnderlyingSystemType == implementationType.UnderlyingSystemType))
            .Any());
 }
        public AddInComposablePartDefinition(AddInCatalog catalog, ComposablePartDefinition composablePartDefinition)
        {
            this._composablePartDefinition = composablePartDefinition;

            List <KeyValuePair <string, object> > injectedMetadata = new List <KeyValuePair <string, object> >();

            injectedMetadata.Add(new KeyValuePair <string, object>(AddInCatalog.PackageIdMetadataName, catalog.PackageId));
            injectedMetadata.Add(new KeyValuePair <string, object>(AddInCatalog.PackageVersionMetadataName,
                                                                   catalog.PackageVersion));

            List <ExportDefinition> interceptedExports = new List <ExportDefinition>();

            foreach (ExportDefinition export in composablePartDefinition.ExportDefinitions)
            {
                ICompositionElement compositionElement = export as ICompositionElement;
                if (compositionElement == null)
                {
                    throw new InvalidOperationException("ExportDefinition doesn't implement ICompositionElement");
                }

                Dictionary <string, object> metadata = injectedMetadata.Concat(export.Metadata)
                                                       .ToDictionary(kvp => kvp.Key,
                                                                     kvp => kvp.Value);

                // TODO this will fail if export isn't a ReflectionMemberExportDefinition (Internal, so I can't check)
                LazyMemberInfo lazyMember = ReflectionModelServices.GetExportingMember(export);

                ExportDefinition interceptedExport =
                    ReflectionModelServices.CreateExportDefinition(lazyMember,
                                                                   export.ContractName,
                                                                   new Lazy <IDictionary <string, object> >(() => metadata),
                                                                   compositionElement.Origin);
                interceptedExports.Add(interceptedExport);
            }

            this._exportDefinitions = interceptedExports.ToArray();
        }
示例#7
0
 /// <summary>
 /// Initializes the context from a part and the export.
 /// </summary>
 internal FilteredExport(ComposablePartDefinition part, ExportDefinition export)
 {
     this.ExportDefinition = export;
     this.ExportingMember  = ReflectionModelServices.GetExportingMember(export);
     this.ExportingType    = ReflectionModelServices.GetPartType(part).Value;
 }
示例#8
0
 public SerializableExportDefinition(ExportDefinition exportDefinition)
 {
     ContractName    = exportDefinition.ContractName;
     Metadata        = new SerializableDictionary <string, object>(exportDefinition.Metadata);
     ExportingMember = new SerializableLazyMemberInfo(ReflectionModelServices.GetExportingMember(exportDefinition));
 }
 private IEnumerable <ComposablePartDefinition> BuildParts(IQueryable <ComposablePartDefinition> parts)
 {
     return(parts.Select(def => ReflectionModelServices.CreatePartDefinition(
                             ReflectionModelServices.GetPartType(def),
                             true,
                             new Lazy <IEnumerable <ImportDefinition> >(() => def.ImportDefinitions),
                             new Lazy <IEnumerable <ExportDefinition> >(() => def.ExportDefinitions.Select(export =>
                                                                                                           ReflectionModelServices.CreateExportDefinition(
                                                                                                               ReflectionModelServices.GetExportingMember(export),
                                                                                                               export.ContractName,
                                                                                                               new Lazy <IDictionary <string, object> >(() => VisitExport(def, export)),
                                                                                                               this))),
                             new Lazy <IDictionary <string, object> >(() => VisitPart(def)),
                             this)));
 }
示例#10
0
        private ComposablePartDefinition RewritePart(ComposablePartDefinition definition)
        {
            var closedGenericContractType = ContractType.MakeGenericType(ExportingType.GetGenericArguments());
            var closedGenericTypeIdentity = AttributedModelServices.GetTypeIdentity(closedGenericContractType);
            var openGenericTypeIdentity   = AttributedModelServices.GetTypeIdentity(ContractType);
            var openGenericContractName   = AttributedModelServices.GetContractName(ContractType);
            var exports             = new List <ExportDefinition>();
            var hasRewrittenExports = false;

            foreach (var exportDefinition in definition.ExportDefinitions)
            {
                // Rewrite only exports having open-generics type identity of the contract type we do care about
                if (openGenericTypeIdentity == (string)exportDefinition.Metadata[CompositionConstants.ExportTypeIdentityMetadataName])
                {
                    // If both open-generic contract name and the present contract name are equal,
                    // contract name has to be rewritten to form a closed-generic contract
                    var contractName = openGenericContractName == exportDefinition.ContractName
                                           ? AttributedModelServices.GetContractName(closedGenericContractType)
                                           : exportDefinition.ContractName;

                    // Preserve all the metadata except the type identity as it has to be rewritten
                    var metadata = new Dictionary <string, object>
                    {
                        { CompositionConstants.ExportTypeIdentityMetadataName, closedGenericTypeIdentity }
                    };
                    foreach (var key in exportDefinition.Metadata.Keys.Where(key => key != CompositionConstants.ExportTypeIdentityMetadataName))
                    {
                        metadata.Add(key, exportDefinition.Metadata[key]);
                    }

                    // Rewrite the export
                    var rewrittenExport = ReflectionModelServices.CreateExportDefinition(
                        ReflectionModelServices.GetExportingMember(exportDefinition),
                        contractName,
                        new Lazy <IDictionary <string, object> >(() => metadata),
                        exportDefinition as ICompositionElement);

                    exports.Add(rewrittenExport);
                    hasRewrittenExports = true;
                }
                else
                {
                    // This export is ok, copy the original
                    exports.Add(exportDefinition);
                }
            }

            // If the part has any rewritten exports, we have to rewrite the part itself
            if (hasRewrittenExports)
            {
                return(ReflectionModelServices.CreatePartDefinition(
                           ReflectionModelServices.GetPartType(definition),
                           ReflectionModelServices.IsDisposalRequired(definition),
                           new Lazy <IEnumerable <ImportDefinition> >(() => definition.ImportDefinitions),
                           new Lazy <IEnumerable <ExportDefinition> >(() => exports),
                           new Lazy <IDictionary <string, object> >(() => definition.Metadata),
                           definition as ICompositionElement));
            }

            return(definition);
        }
示例#11
0
        private IEnumerable <Tuple <Type, PartDefinition> > ExtractImportsAndExports(Assembly assembly, Func <Type, TypeIdentity> createTypeIdentity)
        {
            var catalog = new AssemblyCatalog(assembly);

            foreach (var part in catalog.Parts)
            {
                var exports = new List <SerializableExportDefinition>();
                foreach (var export in part.ExportDefinitions)
                {
                    var memberInfo = ReflectionModelServices.GetExportingMember(export);
                    SerializableExportDefinition exportDefinition = null;
                    switch (memberInfo.MemberType)
                    {
                    case MemberTypes.Method:
                        exportDefinition = CreateMethodExport(export, memberInfo, createTypeIdentity);
                        break;

                    case MemberTypes.Property:
                        exportDefinition = CreatePropertyExport(export, memberInfo, createTypeIdentity);
                        break;

                    case MemberTypes.NestedType:
                    case MemberTypes.TypeInfo:
                        exportDefinition = CreateTypeExport(export, memberInfo, createTypeIdentity);
                        break;

                    default:
                        throw new NotImplementedException();
                    }

                    if (exportDefinition != null)
                    {
                        exports.Add(exportDefinition);
                        m_Logger.Log(
                            LevelToLog.Info,
                            string.Format(
                                CultureInfo.InvariantCulture,
                                "Discovered export: {0}",
                                exportDefinition));
                    }
                    else
                    {
                        m_Logger.Log(
                            LevelToLog.Warn,
                            string.Format(
                                CultureInfo.InvariantCulture,
                                "Unable to process export: {0} on a {1}",
                                export.ContractName,
                                memberInfo.MemberType));
                    }
                }

                var imports = new List <SerializableImportDefinition>();
                foreach (var import in part.ImportDefinitions)
                {
                    Debug.Assert(import is ContractBasedImportDefinition, "All import objects should be ContractBasedImportDefinition objects.");
                    var contractImport = import as ContractBasedImportDefinition;

                    SerializableImportDefinition importDefinition = !ReflectionModelServices.IsImportingParameter(contractImport)
                        ? CreatePropertyImport(contractImport, createTypeIdentity)
                        : CreateConstructorParameterImport(contractImport, createTypeIdentity);

                    if (importDefinition != null)
                    {
                        imports.Add(importDefinition);
                        m_Logger.Log(
                            LevelToLog.Info,
                            string.Format(
                                CultureInfo.InvariantCulture,
                                "Discovered import: {0}",
                                importDefinition));
                    }
                    else
                    {
                        m_Logger.Log(
                            LevelToLog.Warn,
                            string.Format(
                                CultureInfo.InvariantCulture,
                                "Unable to process import: {0}",
                                import.ContractName));
                    }
                }

                var type = ReflectionModelServices.GetPartType(part).Value;
                yield return(new Tuple <Type, PartDefinition>(
                                 type,
                                 new PartDefinition
                {
                    Identity = createTypeIdentity(type),
                    Exports = exports,
                    Imports = imports,
                    Actions = Enumerable.Empty <ScheduleActionDefinition>(),
                    Conditions = Enumerable.Empty <ScheduleConditionDefinition>(),
                }));
            }
        }