// this is always called under a lock
        private ExportingMember GetExportingMemberFromDefinition(ExportDefinition definition)
        {
            ExportingMember result;
            ReflectionMemberExportDefinition reflectionExport = definition as ReflectionMemberExportDefinition;

            if (reflectionExport == null)
            {
                return(null);
            }

            int exportIndex = reflectionExport.GetIndex();

            if (_exportsCache == null)
            {
                _exportsCache = new Dictionary <int, ExportingMember>();
            }
            if (!_exportsCache.TryGetValue(exportIndex, out result))
            {
                result = GetExportingMember(definition);
                if (result != null)
                {
                    _exportsCache[exportIndex] = result;
                }
            }

            return(result);
        }
Example #2
0
            public IEnumerable <ExportDefinition> GetExports()
            {
                if (this._exports == null)
                {
                    yield break;
                }

                IEnumerable <ExportDefinition> exports = this._exports.Value;

                if (exports == null)
                {
                    yield break;
                }

                foreach (ExportDefinition export in exports)
                {
                    ReflectionMemberExportDefinition reflectionExport = export as ReflectionMemberExportDefinition;
                    if (reflectionExport == null)
                    {
                        throw new InvalidOperationException(
                                  string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidExportDefinition, export.GetType()));
                    }
                    yield return(reflectionExport);
                }
            }
Example #3
0
        public void Constructor()
        {
            MemberInfo     expectedMember = this.GetType();
            LazyMemberInfo expectedExportingMemberInfo = new LazyMemberInfo(expectedMember);

            string expectedContractName = "Contract";
            IDictionary <string, object> expectedMetadata = new Dictionary <string, object>();

            expectedMetadata["Key1"] = 1;
            expectedMetadata["Key2"] = "Value2";

            ReflectionMemberExportDefinition definition = CreateReflectionExportDefinition(expectedExportingMemberInfo, expectedContractName, expectedMetadata);

            Assert.Equal(expectedExportingMemberInfo, definition.ExportingLazyMember);
            Assert.Same(expectedMember, definition.ExportingLazyMember.GetAccessors()[0]);
            Assert.Equal(MemberTypes.TypeInfo, definition.ExportingLazyMember.MemberType);

            Assert.Same(expectedContractName, definition.ContractName);

            Assert.NotNull(definition.Metadata);
            Assert.True(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
            Assert.True(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));

            Assert.Null(((ICompositionElement)definition).Origin);
        }
Example #4
0
        public IEnumerable <ExportDefinition> GetExports()
        {
            if (_exports == null)
            {
                yield break;
            }

            IEnumerable <ExportDefinition> exports = _exports.Value;

            if (exports == null)
            {
                yield break;
            }

            foreach (ExportDefinition export in exports)
            {
                ReflectionMemberExportDefinition reflectionExport = export as ReflectionMemberExportDefinition;
                if (reflectionExport == null)
                {
                    throw new InvalidOperationException(
                              SR.Format(SR.ReflectionModel_InvalidExportDefinition, export.GetType()));
                }
                yield return(reflectionExport);
            }
        }
Example #5
0
        private static ExportingMember GetExportingMember(ExportDefinition definition)
        {
            ReflectionMemberExportDefinition exportDefinition = definition as ReflectionMemberExportDefinition;

            if (exportDefinition != null)
            {
                return(exportDefinition.ToExportingMember());
            }

            // Don't recognize it
            return(null);
        }
        private IDictionary <string, object?> TranslateExportMetadata(ReflectionMemberExportDefinition originalExport)
        {
            Dictionary <string, object?> metadata = new Dictionary <string, object?>(originalExport.Metadata, StringComparers.MetadataKeyNames);

            string?exportTypeIdentity = originalExport.Metadata.GetValue <string>(CompositionConstants.ExportTypeIdentityMetadataName);

            if (!string.IsNullOrEmpty(exportTypeIdentity))
            {
                metadata[CompositionConstants.ExportTypeIdentityMetadataName] = Translate(exportTypeIdentity, originalExport.Metadata.GetValue <int[]>(CompositionConstants.GenericExportParametersOrderMetadataName));
            }
            metadata.Remove(CompositionConstants.GenericExportParametersOrderMetadataName);

            return(metadata);
        }
Example #7
0
        public static LazyMemberInfo GetExportingMember(ExportDefinition exportDefinition)
        {
            Requires.NotNull(exportDefinition, "exportDefinition");

            ReflectionMemberExportDefinition reflectionExportDefinition = exportDefinition as ReflectionMemberExportDefinition;

            if (reflectionExportDefinition == null)
            {
                throw new ArgumentException(
                          string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidExportDefinition, exportDefinition.GetType()),
                          "exportDefinition");
            }

            return(reflectionExportDefinition.ExportingLazyMember);
        }
Example #8
0
        public static LazyMemberInfo GetExportingMember(ExportDefinition exportDefinition)
        {
            Requires.NotNull(exportDefinition, nameof(exportDefinition));

            ReflectionMemberExportDefinition reflectionExportDefinition = exportDefinition as ReflectionMemberExportDefinition;

            if (reflectionExportDefinition == null)
            {
                throw new ArgumentException(
                          SR.Format(SR.ReflectionModel_InvalidExportDefinition, exportDefinition.GetType()),
                          nameof(exportDefinition));
            }

            return(reflectionExportDefinition.ExportingLazyMember);
        }
        private List <ExportDefinition> PopulateExports(List <LazyMemberInfo> members)
        {
            List <ExportDefinition> exports = new List <ExportDefinition>();

            foreach (ExportDefinition originalExport in _originalPartCreationInfo.GetExports())
            {
                ReflectionMemberExportDefinition reflectionExport = originalExport as ReflectionMemberExportDefinition;
                if (reflectionExport == null)
                {
                    // we always ignore these
                    continue;
                }

                exports.Add(TranslateExpot(reflectionExport, members));
            }

            return(exports);
        }
Example #10
0
        public void Constructor_NullMetadata()
        {
            MemberInfo     expectedMember = this.GetType();
            LazyMemberInfo expectedExportingMemberInfo = new LazyMemberInfo(expectedMember);

            string expectedContractName = "Contract";

            ReflectionMemberExportDefinition definition = CreateReflectionExportDefinition(expectedExportingMemberInfo, expectedContractName, null);

            Assert.Equal(expectedExportingMemberInfo, definition.ExportingLazyMember);
            Assert.Same(expectedMember, definition.ExportingLazyMember.GetAccessors()[0]);
            Assert.Equal(MemberTypes.TypeInfo, definition.ExportingLazyMember.MemberType);

            Assert.Same(expectedContractName, definition.ContractName);

            Assert.NotNull(definition.Metadata);
            Assert.Equal(0, definition.Metadata.Count);

            Assert.Null(((ICompositionElement)definition).Origin);
        }
        public ExportDefinition TranslateExpot(ReflectionMemberExportDefinition reflectionExport, List <LazyMemberInfo> members)
        {
            ExportDefinition?export      = null;
            LazyMemberInfo   lazyMember  = reflectionExport.ExportingLazyMember;
            var capturedLazyMember       = lazyMember;
            var capturedReflectionExport = reflectionExport;

            string contractName = Translate(reflectionExport.ContractName, reflectionExport.Metadata.GetValue <int[]>(CompositionConstants.GenericExportParametersOrderMetadataName));

            LazyMemberInfo exportingMember = new LazyMemberInfo(capturedLazyMember.MemberType, () => GetAccessors(capturedLazyMember));
            Lazy <IDictionary <string, object?> > lazyMetadata = new Lazy <IDictionary <string, object?> >(() => TranslateExportMetadata(capturedReflectionExport));

            export = new ReflectionMemberExportDefinition(
                exportingMember,
                new LazyExportDefinition(contractName, lazyMetadata),
                ((ICompositionElement)reflectionExport).Origin);

            members.Add(capturedLazyMember);

            return(export);
        }
        private IDictionary<string, object> TranslateExportMetadata(ReflectionMemberExportDefinition originalExport)
        {
            Dictionary<string, object> metadata = new Dictionary<string, object>(originalExport.Metadata, StringComparers.MetadataKeyNames);

            string exportTypeIdentity = originalExport.Metadata.GetValue<string>(CompositionConstants.ExportTypeIdentityMetadataName);
            if (!string.IsNullOrEmpty(exportTypeIdentity))
            {
                metadata[CompositionConstants.ExportTypeIdentityMetadataName] = this.Translate(exportTypeIdentity, originalExport.Metadata.GetValue<int[]>(CompositionConstants.GenericExportParametersOrderMetadataName));
            }
            metadata.Remove(CompositionConstants.GenericExportParametersOrderMetadataName);

            return metadata;
        }
        public ExportDefinition TranslateExpot(ReflectionMemberExportDefinition reflectionExport, List<LazyMemberInfo> members)
        {
            ExportDefinition export = null;
            LazyMemberInfo lazyMember = reflectionExport.ExportingLazyMember;
            var capturedLazyMember = lazyMember;
            var capturedReflectionExport = reflectionExport;

            string contractName = this.Translate(reflectionExport.ContractName, reflectionExport.Metadata.GetValue<int[]>(CompositionConstants.GenericExportParametersOrderMetadataName));

            LazyMemberInfo exportingMember = new LazyMemberInfo(capturedLazyMember.MemberType, () => GetAccessors(capturedLazyMember));
            Lazy<IDictionary<string, object>> lazyMetadata = new Lazy<IDictionary<string, object>>(() => this.TranslateExportMetadata(capturedReflectionExport));



            export = new ReflectionMemberExportDefinition(
                exportingMember,
                new LazyExportDefinition(contractName, lazyMetadata),
                ((ICompositionElement)reflectionExport).Origin);

            members.Add(capturedLazyMember);

            return export;
        }