Пример #1
0
 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));
 }
        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);
        }
        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);
        }
Пример #4
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);
        }
Пример #5
0
        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));
        }
Пример #6
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
            }
        }
Пример #7
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);
            }
        }
Пример #8
0
 private LazyMemberInfo CreateWrapped(LazyMemberInfo lazyMember, Type type)
 {
     return(new LazyMemberInfo(
                lazyMember.MemberType,
                () => { this.OnTypeLoaded(type); return lazyMember.GetAccessors(); }));
 }