private static void SatisfyImportsInternal(this CompositionContext exportProvider, object objectWithLooseImports, AttributedModelProvider conventions)
        {
            if (exportProvider == null) throw new ArgumentNullException("exportProvider");
            if (objectWithLooseImports == null) throw new ArgumentNullException("objectWithLooseImports");
            if (conventions == null) throw new ArgumentNullException("conventions");

            var objType = objectWithLooseImports.GetType();

            foreach (var pi in objType.GetRuntimeProperties())
            {
                ImportInfo importInfo;
                var site = new PropertyImportSite(pi);
                if (ContractHelpers.TryGetExplicitImportInfo(pi.PropertyType, conventions.GetDeclaredAttributes(pi.DeclaringType, pi), site, out importInfo))
                {
                    object value;
                    if (exportProvider.TryGetExport(importInfo.Contract, out value))
                    {
                        pi.SetValue(objectWithLooseImports, value);
                    }
                    else if (!importInfo.AllowDefault)
                    {
                        throw new CompositionFailedException(string.Format(
                            Properties.Resources.CompositionContextExtensions_MissingDependency, pi.Name, objectWithLooseImports));
                    }
                }
            }

            var importsSatisfiedMethods = objectWithLooseImports.GetType().GetRuntimeMethods().Where(m =>
                m.CustomAttributes.Any(ca => ca.AttributeType == typeof(OnImportsSatisfiedAttribute)));

            foreach (var ois in importsSatisfiedMethods)
                ois.Invoke(objectWithLooseImports, null);
        }
        MvcContainerConfiguration(IEnumerable<Assembly> assemblies, AttributedModelProvider reflectionContext)
        {
            if (assemblies == null) throw new ArgumentNullException("assemblies");
              if (reflectionContext == null) throw new ArgumentNullException("reflectionContext");

              WithDefaultConventions(reflectionContext);
              WithAssemblies(assemblies);
        }
        public MefDependencyResolver(AttributedModelProvider builder, IDependencyResolver resolver)
        {
            _container = new ContainerConfiguration()
                .WithAssemblies(AppDomain.CurrentDomain.GetAssemblies(), builder)
                .CreateContainer();

            _resolver = resolver;
        }
示例#4
0
 public DiscoveredPart(
     TypeInfo partType,
     AttributedModelProvider attributeContext,
     ActivationFeature[] activationFeatures)
 {
     _partType = partType;
     _attributeContext = attributeContext;
     _activationFeatures = activationFeatures;
     _partMetadata = new Lazy<IDictionary<string, object>>(() => GetPartMetadata(partType));
 }
示例#5
0
 private DiscoveredPart(
     TypeInfo partType,
     AttributedModelProvider attributeContext,
     ActivationFeature[] activationFeatures,
     Lazy<IDictionary<string, object>> partMetadata)
 {
     _partType = partType;
     _attributeContext = attributeContext;
     _activationFeatures = activationFeatures;
     _partMetadata = partMetadata;
 }
        public static ContainerConfiguration WithAssembliesInPath(this ContainerConfiguration configuration, string path, AttributedModelProvider conventions, SearchOption searchOption = SearchOption.TopDirectoryOnly)
        {
            var assemblies = Directory
                .GetFiles(path, "*.dll", searchOption)
                .Select(AssemblyLoadContext.GetAssemblyName)
                .Select(AssemblyLoadContext.Default.LoadFromAssemblyName)
                .ToList();

            configuration = configuration.WithAssemblies(assemblies, conventions);

            return configuration;
        }
        public TypedPartExportDescriptorProvider(IEnumerable<Type> types, AttributedModelProvider attributeContext)
        {
            var activationFeatures = CreateActivationFeatures(attributeContext);
            var typeInspector = new TypeInspector(attributeContext, activationFeatures);

            foreach (var type in types)
            {
                DiscoveredPart part;
                if (typeInspector.InspectTypeForPart(type.GetTypeInfo(), out part))
                {
                    AddDiscoveredPart(part);
                }
            }
        }
 public static TAttribute[] GetDeclaredAttributes <TAttribute>(this AttributedModelProvider convention, Type reflectedType, ParameterInfo parameter) where TAttribute : Attribute
 {
     return(convention.GetCustomAttributes(reflectedType, parameter).OfType <TAttribute>().ToArray());
 }
 public static Attribute[] GetDeclaredAttributes(this AttributedModelProvider convention, Type reflectedType, MemberInfo member)
 {
     return(convention.GetCustomAttributes(reflectedType, member).ToArray());
 }
 public static TAttribute GetDeclaredAttribute <TAttribute>(this AttributedModelProvider convention, Type reflectedType, MemberInfo member) where TAttribute : Attribute
 {
     return(convention.GetCustomAttributes(reflectedType, member).OfType <TAttribute>().SingleOrDefault());
 }
 public PropertyInjectionFeature(AttributedModelProvider attributeContext)
 {
     _attributeContext = attributeContext;
 }
 /// <summary>
 /// Set public properties decorated with the <see cref="ImportAttribute"/>.
 /// </summary>
 /// <remarks>Uses reflection, is slow - caching would help here.</remarks>
 /// <param name="conventions">Conventions to apply when satisfying loose imports.</param>
 /// <param name="objectWithLooseImports">An object with decorated with import attributes.</param>
 /// <param name="compositionContext">Export provider that will supply imported values.</param>
 public static void SatisfyImports(this CompositionContext compositionContext, object objectWithLooseImports, AttributedModelProvider conventions)
 {
     SatisfyImportsInternal(compositionContext, objectWithLooseImports, conventions);
 }
 internal static ActivationFeature[] DebugGetActivationFeatures(AttributedModelProvider attributeContext)
 {
     return CreateActivationFeatures(attributeContext);
 }
 private static ActivationFeature[] CreateActivationFeatures(AttributedModelProvider attributeContext)
 {
     return new ActivationFeature[] {
         new DisposalFeature(),
         new PropertyInjectionFeature(attributeContext),
         new OnImportsSatisfiedFeature(attributeContext),
         new LifetimeFeature(),
     };
 }
示例#15
0
 public TypeInspector(AttributedModelProvider attributeContext, ActivationFeature[] activationFeatures)
 {
     _attributeContext = attributeContext;
     _activationFeatures = activationFeatures;
 }
 public OnImportsSatisfiedFeature(AttributedModelProvider attributeContext)
 {
     if (attributeContext == null) throw new ArgumentNullException("attributeContext");
     _attributeContext = attributeContext;
 }