public static IDictionary <string, object> WriteImportDefinition(ComposablePartDefinition owner, ContractBasedImportDefinition importDefinition)
        {
            Assumes.NotNull(owner);
            Assumes.NotNull(importDefinition);

            Lazy <Type> partType = ReflectionModelServices.GetPartType(owner);

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

            cache.WriteContractName(importDefinition.ContractName);
            cache.WriteValue(AttributedCacheServices.CacheKeys.RequiredTypeIdentity, importDefinition.RequiredTypeIdentity, null);
            cache.WriteRequiredMetadata(importDefinition.RequiredMetadata);
            cache.WriteValue(AttributedCacheServices.CacheKeys.RequiredCreationPolicy, importDefinition.RequiredCreationPolicy, CreationPolicy.Any);
            cache.WriteValue(AttributedCacheServices.CacheKeys.Cardinality, importDefinition.Cardinality, ImportCardinality.ExactlyOne);
            if (ReflectionModelServices.IsImportingParameter(importDefinition))
            {
                cache.WriteValue(AttributedCacheServices.CacheKeys.ImportType, AttributedCacheServices.ImportTypes.Parameter);
                cache.WriteLazyParameter(
                    ReflectionModelServices.GetImportingParameter(importDefinition),
                    partType);
            }
            else
            {
                // don't write anything for import type - member assumed
                LazyMemberInfo importingMemberInfo = ReflectionModelServices.GetImportingMember(importDefinition);
                cache.WriteValue(AttributedCacheServices.CacheKeys.IsRecomposable, importDefinition.IsRecomposable, false);
                cache.WriteValue(AttributedCacheServices.CacheKeys.MemberType, importingMemberInfo.MemberType, MemberTypes.Property);
                cache.WriteLazyAccessors(
                    importingMemberInfo.GetAccessors(),
                    partType);
            }

            return(cache);
        }
Example #2
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);
        }
 private static SerializableExportDefinition CreateTypeExport(
     ExportDefinition export,
     LazyMemberInfo memberInfo,
     Func <Type, TypeIdentity> identityGenerator)
 {
     Debug.Assert(memberInfo.GetAccessors().Count() == 1, "Only expecting one accessor for a type export.");
     Debug.Assert(memberInfo.GetAccessors().First() is Type, "Expecting the export to be a Type.");
     return(TypeBasedExportDefinition.CreateDefinition(
                export.ContractName,
                memberInfo.GetAccessors().First() as Type,
                identityGenerator));
 }
        private static ExportDefinition CreateExportDefinition(string contractName, Type type)
        {
            LazyMemberInfo memberInfo = new LazyMemberInfo(MemberTypes.TypeInfo, type);

            Lazy <IDictionary <string, object> > metadata = new Lazy <IDictionary <string, object> >(() => {
                string typeIdentity = AttributedModelServices.GetTypeIdentity(type);
                return(new Dictionary <string, object> {
                    { CompositionConstants.ExportTypeIdentityMetadataName, typeIdentity }
                });
            });

            ExportDefinition exportDefinition = ReflectionModelServices.CreateExportDefinition(memberInfo, contractName, metadata, null);

            return(exportDefinition);
        }
Example #5
0
        private IEnumerable <ExportDefinition> GetExportDefinitions(Type implementationType, Type contractType)
        {
            var lazyMember  = new LazyMemberInfo(implementationType);
            var contracName = AttributedModelServices.GetContractName(contractType);
            var metadata    = new Lazy <IDictionary <string, object> >(() =>
            {
                var md = new Dictionary <string, object>();
                md.Add(CompositionConstants.ExportTypeIdentityMetadataName, AttributedModelServices.GetTypeIdentity(contractType));
                return(md);
            });

            return(new []
            {
                ReflectionModelServices.CreateExportDefinition(lazyMember, contracName, metadata, null)
            });
        }
        public static ExportDefinition ReadExportDefinition(ComposablePartDefinition owner, IDictionary <string, object> cache)
        {
            Assumes.NotNull(owner);
            Assumes.NotNull(cache);

            LazyMemberInfo exportingMemberInfo = new LazyMemberInfo(
                cache.ReadValue <MemberTypes>(AttributedCacheServices.CacheKeys.MemberType, MemberTypes.TypeInfo),
                cache.ReadLazyAccessors(ReflectionModelServices.GetPartType(owner)));


            return(ReflectionModelServices.CreateExportDefinition(
                       exportingMemberInfo,
                       cache.ReadContractName(),
                       cache.ReadLazyMetadata(),
                       owner as ICompositionElement));
        }
        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);
        }
Example #8
0
        public static PropertyInfo FindProperty(LazyMemberInfo member)
        {
            var accessor      = member.GetAccessors()[0];
            var declaringType = accessor.DeclaringType;

            foreach (var property in declaringType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                var getter = property.GetGetMethod(true);
                var setter = property.GetSetMethod(true);
                if (getter == accessor || setter == accessor)
                {
                    return(property);
                }
            }

            return(null);
        }
        private static SerializableExportDefinition CreatePropertyExport(
            ExportDefinition export,
            LazyMemberInfo memberInfo,
            Func <Type, TypeIdentity> identityGenerator)
        {
            // this is really ugly because we assume that the underlying methods for a property are named as:
            // get_PROPERTYNAME and set_PROPERTYNAME. In this case we assume that exports always
            // have a get method.
            var getMember = memberInfo.GetAccessors().First(m => m.Name.Contains("get_"));
            var name      = getMember.Name.Substring("get_".Length);
            var property  = getMember.DeclaringType.GetProperty(name);

            return(PropertyBasedExportDefinition.CreateDefinition(
                       export.ContractName,
                       property,
                       identityGenerator));
        }
Example #10
0
        private static void AddMemberType(ExportSource exportSource, ImportDefinition definition)
        {
            try
            {
                LazyMemberInfo member = ReflectionModelServices.GetImportingMember(definition);
                switch (member.MemberType)
                {
                case MemberTypes.Property:
                    MemberInfo[] accessors = member.GetAccessors();
                    MethodInfo   setter    = accessors.OfType <MethodInfo>().Single(m => m.ReturnType == typeof(void));
                    Type         type      = setter.GetParameters()[0].ParameterType;
                    exportSource.AddType(type);
                    return;

                default:
                    return;
                }
            }
            catch (ArgumentException)
            {
                // There is no TryGetImportingMember method, so if definition is of the wrong type, we just swallow exception
            }
        }
        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();
        }
Example #12
0
        public SerializableLazyMemberInfo(LazyMemberInfo lazyMemberInfo)
        {
            MemberType = lazyMemberInfo.MemberType;

            var accessor = lazyMemberInfo.GetAccessors()[0];

            if (MemberType == MemberTypes.TypeInfo)
            {
                DeclaringTypeAssemblyQualifiedName = ((Type)accessor).AssemblyQualifiedName;
                MemberName = null;
            }
            else
            {
                // For properties, fields, etc. we keep the declaring type and the member name.
                DeclaringTypeAssemblyQualifiedName = accessor.DeclaringType.AssemblyQualifiedName;
                MemberName = accessor.Name;
            }

            if (MemberType == MemberTypes.Property)
            {
                // Remove the "get_" prefix from the property name.
                MemberName = MemberName.Substring(4);
            }
        }
        public static ContractBasedImportDefinition ReadImportDefinition(ComposablePartDefinition owner, IDictionary <string, object> cache)
        {
            Assumes.NotNull(owner);
            Assumes.NotNull(cache);

            Lazy <Type>         partType = ReflectionModelServices.GetPartType(owner);
            ICompositionElement origin   = owner as ICompositionElement;

            if (cache.ReadValue <string>(AttributedCacheServices.CacheKeys.ImportType) == AttributedCacheServices.ImportTypes.Parameter)
            {
                return(ReflectionModelServices.CreateImportDefinition(
                           cache.ReadLazyParameter(partType),
                           cache.ReadContractName(),
                           cache.ReadValue <string>(AttributedCacheServices.CacheKeys.RequiredTypeIdentity),
                           cache.ReadRequiredMetadata(),
                           cache.ReadValue <ImportCardinality>(AttributedCacheServices.CacheKeys.Cardinality, ImportCardinality.ExactlyOne),
                           cache.ReadValue <CreationPolicy>(AttributedCacheServices.CacheKeys.RequiredCreationPolicy, CreationPolicy.Any),
                           origin));
            }
            else
            {
                LazyMemberInfo importingMemberInfo = new LazyMemberInfo(
                    cache.ReadValue <MemberTypes>(AttributedCacheServices.CacheKeys.MemberType, MemberTypes.Property),
                    cache.ReadLazyAccessors(partType));

                return(ReflectionModelServices.CreateImportDefinition(
                           importingMemberInfo,
                           cache.ReadContractName(),
                           cache.ReadValue <string>(AttributedCacheServices.CacheKeys.RequiredTypeIdentity),
                           cache.ReadRequiredMetadata(),
                           cache.ReadValue <ImportCardinality>(AttributedCacheServices.CacheKeys.Cardinality, ImportCardinality.ExactlyOne),
                           cache.ReadValue <bool>(AttributedCacheServices.CacheKeys.IsRecomposable, false),
                           cache.ReadValue <CreationPolicy>(AttributedCacheServices.CacheKeys.RequiredCreationPolicy, CreationPolicy.Any),
                           origin));
            }
        }
Example #14
0
 private LazyMemberInfo CreateWrapped(LazyMemberInfo lazyMember, Type type)
 {
     return(new LazyMemberInfo(
                lazyMember.MemberType,
                () => { this.OnTypeLoaded(type); return lazyMember.GetAccessors(); }));
 }