示例#1
0
        public object GetArgument(Type parameterType, ICustomAttributeProvider info, bool isOptional, object defaultValue)
        {
            // validate
            if (!PlasmaContainer.IsValidImplementationType(parameterType))
            {
                if (isOptional)
                {
                    return(GetArgumentDefaultOptional(defaultValue));
                }
                return(Default(parameterType));               // todo not tested for static mining
            }

            // detect requestedType
            var requestType = Unlazy(parameterType);

            return(GetArgumentRquestAndConvert(parameterType, requestType, info, isOptional));
        }
示例#2
0
            public ServiceEntry(PlasmaContainer provider, Lazy <object> instance)
            {
                if (instance == null)
                {
                    throw new ArgumentNullException("instance");
                }
                _instance = instance;
                _provider = provider;

                if (instance.IsValueCreated)                 // TODO if instance already created - no plumbing occurs!
                {
                    if (instance.Value != null)
                    {
                        ValidateImplementationType(instance.Value.GetType());
                    }
                }

                _serviceGetLazy = new Lazy <object>(GetService);
            }
示例#3
0
 public ReflectionMining(PlasmaContainer provider)
 {
     _provider = provider;
 }
示例#4
0
        public Type IfaceImpl(Type type)
        {
            var impl = FaceImplRegister.Get(type);

            if (impl != null)
            {
                return(impl);
            }

            if (!type.IsAbstract && type.IsClass)
            {
                return(type);
            }

            ValidateReflectionPermission();
            if (type.IsInterface || type.IsAbstract)
            {
                var dsias = (DefaultImplAttribute[])type.GetCustomAttributes(typeof(DefaultImplAttribute), true);
                if (dsias.Any())
                {
                    var dsia = dsias.FirstOrDefault(x => x.TargetType != null);
                    if (dsia == null)
                    {
                        throw new PlasmaException(string.Format(CultureInfo.CurrentCulture, "Service for type '{0}' cannot be loaded by non-resolvable DefaultImpl: " + string.Join(", ", dsias.Select(x => x.TypeAqn)), type.Name));
                    }
                    type = dsia.TargetType;
                }
                else
                {
                    Exception exinner = null;
                    try
                    {
                        if (type.IsInterface && type.Name.StartsWith("I"))
                        {
                            var expectedName = type.Name.Substring(1);

                            var matched = AssemblyAnalyzeCache.SearchFor(expectedName)
                                          .Where(x => type.IsAssignableFrom(x))
                                          .ToArray();

                            if (matched.Length == 1)
                            {
                                return(matched[0]);
                            }

                            var impls = AssemblyAnalyzeCache.ImplsOf(type);
                            if (impls != null && impls.Count == 1)
                            {
                                return(impls.First());
                            }
                            else if (impls == null)
                            {
                                throw new Exception(string.Format("Impls of type '{0}' not registered", type));
                            }
                            else
                            {
                                throw new Exception(string.Format("Impls of type '{0}': {1}", PlasmaContainer.GetTypeName(type), string.Join(", ", impls.Select(PlasmaContainer.GetTypeName))));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        exinner = ex;
                    }

                    throw new PlasmaException(string.Format(CultureInfo.CurrentCulture, "Cannot register service for type '{0}'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface", type.Name), exinner);
                }
            }
            return(type);
        }
示例#5
0
 /// <summary>
 /// Instantiate specified type
 /// </summary>
 public object CreateType(Type type)
 {
     type = IfaceImpl(type);
     PlasmaContainer.ValidateImplementationType(type);
     return(DefaultFactoryCore(type));
 }