Exemplo n.º 1
0
 public void is_enumerable()
 {
     EnumerableInstance.IsEnumerable(typeof(IWidget[])).ShouldBeTrue();
     EnumerableInstance.IsEnumerable(typeof(IList <IWidget>)).ShouldBeTrue();
     EnumerableInstance.IsEnumerable(typeof(IEnumerable <IWidget>)).ShouldBeTrue();
     EnumerableInstance.IsEnumerable(typeof(List <IWidget>)).ShouldBeTrue();
     EnumerableInstance.IsEnumerable(typeof(IWidget)).ShouldBeFalse();
 }
Exemplo n.º 2
0
        public PluginFamily Build(Type type)
        {
            if (EnumerableInstance.IsEnumerable(type))
            {
                var family = new PluginFamily(type);
                family.SetDefault(new AllPossibleInstance(type));

                return(family);
            }

            return(null);
        }
Exemplo n.º 3
0
        public static IDependencySource SourceFor(string ctorOrSetter, string name, Type dependencyType, object value)
        {
            if (value == null)
            {
                if (dependencyType.IsSimple())
                {
                    return(new DependencyProblem
                    {
                        Message = MissingPrimitiveWarning,
                        Type = ctorOrSetter,
                        Name = name,
                        ReturnedType = dependencyType
                    });
                }

                if (EnumerableInstance.IsEnumerable(dependencyType))
                {
                    return(new AllPossibleValuesDependencySource(dependencyType));
                }

                return(new DefaultDependencySource(dependencyType));
            }

            if (value is IDependencySource)
            {
                return(value as IDependencySource);
            }

            if (value is Instance)
            {
                return(value.As <Instance>().ToDependencySource(dependencyType));
            }

            if (value.GetType().CanBeCastTo(dependencyType))
            {
                return(new Constant(dependencyType, value));
            }

            if (dependencyType.IsSimple())
            {
                try
                {
                    return(new Constant(dependencyType, ConvertType(value, dependencyType)));
                }
                catch (Exception)
                {
                    return(new DependencyProblem
                    {
                        Type = ctorOrSetter,
                        Name = name,
                        ReturnedType = dependencyType,
                        Message =
                            CastingError.ToFormat(value, value.GetType().GetFullName(), dependencyType.GetFullName())
                    });
                }
            }

            if (EnumerableInstance.IsEnumerable(dependencyType))
            {
                var coercion     = EnumerableInstance.DetermineCoercion(dependencyType);
                var coercedValue = coercion.Convert(value.As <IEnumerable <object> >());

                return(new Constant(dependencyType, coercedValue));
            }


            return(new DependencyProblem
            {
                Type = ctorOrSetter,
                Name = name,
                ReturnedType = dependencyType,
                Message = UnableToDetermineDependency.ToFormat(dependencyType.GetFullName(), value)
            });
        }