예제 #1
0
 /// <summary>
 ///     Validate that our Type selector is not null
 /// </summary>
 /// <param name="selector">The TypeSelector instance</param>
 public static void IsNotNull(ITypeSelector selector)
 {
     if (selector == null)
     {
         throw new ArgumentNullException("selector");
     }
 }
예제 #2
0
        public IEnumerable <Type> RetrieveTypes(ITypeSelector selector)
        {
            var choosenSelector     = selector ?? new AllSelector();
            var typesToRegistration = _sourceTypes.Where(choosenSelector.DoesSelect).ToList();

            return(typesToRegistration);
        }
예제 #3
0
        private IList CreateaArrayInstance(Type propertyType, Type targetType = null)
        {
            //Get the underlying type used int he array
            //var elementType = propertyType.GetElementType(); //Works only for arrays
            var elementType = propertyType.IsGenericType ? propertyType.GetGenericArguments()[0] : propertyType.GetElementType(); //Works for IList<T> / IEnumerable<T>

            //Get a number of elements we want to create
            //Note: (between 1 and 10 for now)
            var elementCount = Numbers.Int(1, 10);

            //Create an instance of our target array
            IList arrayInstance = null;

            //If we're working with a generic list or any other sort of collection
            if (propertyType.IsGenericTypeDefinition)
            {
                arrayInstance = (IList)GenericHelper.CreateGeneric(propertyType, elementType);
            }
            else
            {
                arrayInstance = (IList)GenericHelper.CreateGeneric(typeof(List <>), elementType);
            }

            //Determine if there's a selector available for this type
            var           hasSelector = TypeMap.CountSelectors(elementType) > 0;
            ITypeSelector selector    = null;

            //So we have a type available for this selector..
            if (hasSelector)
            {
                selector = TypeMap.GetBaseSelector(elementType);
            }

            //If the element in the array isn't the same type as the parent object (recursive objects, like trees)
            if (elementType != targetType)
            {
                for (var i = 0; i < elementCount; i++)
                {
                    //Create a new element instance
                    var element = SafeObjectCreate(elementType);

                    if (hasSelector)
                    {
                        selector.Generate(ref element);
                    }

                    //If the element type is a class populate it recursively
                    else if (elementType.IsClass)
                    {
                        var subProperties = elementType.GetProperties();

                        //Populate all of the properties on this object
                        ProcessProperties(subProperties, element);
                    }

                    arrayInstance.Add(element);
                }
            }
            return(arrayInstance);
        }
예제 #4
0
 public CombinedSelector AddSelector(ITypeSelector selector)
 {
     if (selector.SupportedType != SupportedType)
     {
         throw new ArgumentException(nameof(selector));
     }
     _selectors.Add(selector);
     return(this);
 }
        // Draws a representation of the property's value.
        public override void PaintValue(System.Drawing.Design.PaintValueEventArgs e)
        {
            ITypeSelector ts = (ITypeSelector)MathNode.GetService(typeof(ITypeSelector));

            if (ts != null)
            {
                ts.PaintValue(e);
            }
        }
        // Indicates whether the UITypeEditor supports painting a
        // representation of a property's value.
        public override bool GetPaintValueSupported(System.ComponentModel.ITypeDescriptorContext context)
        {
            ITypeSelector ts = (ITypeSelector)MathNode.GetService(typeof(ITypeSelector));

            if (ts != null)
            {
                return(ts.GetPaintValueSupported(context));
            }
            return(false);
        }
 private static IEnumerable<Type> SelectPotentialTypes(IEnumerable<ITypesProvider> typesProviders, ITypeSelector convention)
 {
     try
     {
         var potentialTypes = typesProviders.AsParallel().SelectMany(provider => provider.RetrieveTypes(convention));
         return potentialTypes.ToList();
     }
     catch (AggregateException e)
     {
         throw e.FlattenTryBubbleUp();
     }
 }
 public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
 {
     if (context != null)
     {
         ITypeSelector ts = (ITypeSelector)MathNode.GetService(typeof(ITypeSelector));
         if (ts != null)
         {
             return(ts.GetUIEditorStyle(context));
         }
     }
     return(UITypeEditorEditStyle.None);
 }
예제 #9
0
        /// <summary>
        /// Clone a deep copy of <see cref="TypeTable"/> with the same settings as this instance.
        ///
        /// Any modifications made to the new instance returned should not affect the parent instance.
        /// </summary>
        /// <returns>A deep copy of the current <see cref="TypeTable"/>.</returns>
        public TypeTable Clone()
        {
            // create a deep copy
            var newTypeMap = new Dictionary <Type, LinkedList <ITypeSelector> >();

            foreach (var pair in _typeMap)
            {
                var newList = new ITypeSelector[pair.Value.Count];
                pair.Value.CopyTo(newList, 0);
                newTypeMap.Add(pair.Key, new LinkedList <ITypeSelector>(newList));
            }
            return(new TypeTable(false, newTypeMap));
        }
예제 #10
0
        public ITypeConfig Create(ITypeSelector selector)
        {
            switch (selector.SelectorType)
            {
            case TypeSelectorType.Details:
                return(this.Create(selector.Type, selector.Size, selector.Precision, selector.Scale, selector.IsNullable));

            case TypeSelectorType.Property:
                return(this.Create(selector.Property));

            default:
                throw new NotImplementedException();
            }
        }
예제 #11
0
        /// <summary>
        ///     Add a strongly typed selector to the
        /// </summary>
        /// <param name="selector">A ITypeSelector implementation for Type T</param>
        /// <param name="position">
        ///     Optional parameter - indicates whether you want this type selector to be used first or last in
        ///     the sequence of available selectors for this type
        /// </param>
        public void AddSelector(ITypeSelector selector, SelectorPosition position = SelectorPosition.First)
        {
            var activeType = selector.TargetType;

            CreateTypeIfNotExists(activeType);

            if (position == SelectorPosition.First)
            {
                //If the user wants to add this selector to the front of the list (default), do that
                _typeMap[activeType].AddFirst(selector);
            }
            else //Otherwise, add this type to the back of the list
            {
                _typeMap[activeType].AddLast(selector);
            }
        }
        public DefaultTypeMapper(
            IDictionary <ITypeDescription, IGeneratedType> typeDescritpions,
            ITypeSelector typeSelector)
        {
            if (typeDescritpions == null)
            {
                throw new ArgumentNullException("typeDescritpions");
            }

            if (typeSelector == null)
            {
                throw new ArgumentNullException("typeSelector");
            }

            this.typeDescritpions = typeDescritpions;
            this.typeSelector     = typeSelector;
        }
        public DefaultTypeMapper(
            IDictionary<ITypeDescription, IGeneratedType> typeDescritpions,
            ITypeSelector typeSelector)
        {
            if (typeDescritpions == null)
            {
                throw new ArgumentNullException("typeDescritpions");
            }

            if (typeSelector == null)
            {
                throw new ArgumentNullException("typeSelector");
            }

            this.typeDescritpions = typeDescritpions;
            this.typeSelector = typeSelector;
        }
예제 #14
0
        static ITypeSelector GetTypeSelector(Type parameterType)
        {
            ITypeSelector typeSelector = null;

            if (parameterType.Equals(typeof(object)))
            {
                typeSelector = new FullTypeSelector();
            }
            else
            {
                typeSelector = new TypeSelector {
                    BaseType = parameterType
                };
                //typeSelector.BaseType = parameterType;
            }

            typeSelector.Title = "Choose the type to instantiate";
            typeSelector.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            return(typeSelector);
        }
예제 #15
0
 public SerializationSettings AddTypeSelector(ITypeSelector selector)
 {
     if (TypeSelectors.TryGetValue(selector.SupportedType, out var existing))
     {
         if (existing is CombinedSelector combined)
         {
             combined.AddSelector(existing);
         }
         else
         {
             TypeSelectors[selector.SupportedType] = new CombinedSelector(selector.SupportedType)
                                                     .AddSelector(existing)
                                                     .AddSelector(selector);
         }
     }
     else
     {
         TypeSelectors[selector.SupportedType] = selector;
     }
     return(this);
 }
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (edSvc != null)
            {
                edSvc.CloseDropDown();
                ITypeSelector ts = (ITypeSelector)MathNode.GetService(typeof(ITypeSelector));
                if (ts != null)
                {
                    UITypeEditorEditStyle style = ts.GetUIEditorStyle(context);
                    if (style == UITypeEditorEditStyle.DropDown)
                    {
                        IDataSelectionControl dropdown = ts.GetUIEditorDropdown(context, provider, value);
                        if (dropdown != null)
                        {
                            edSvc.DropDownControl((Control)dropdown);
                            object v = dropdown.UITypeEditorSelectedValue;
                            if (v != null)
                            {
                                return(v);
                            }
                        }
                    }
                    else if (style == UITypeEditorEditStyle.Modal)
                    {
                        IDataSelectionControl modal = ts.GetUIEditorModal(context, provider, value);
                        if (modal != null)
                        {
                            if (edSvc.ShowDialog((Form)modal) == DialogResult.OK)
                            {
                                return(modal.UITypeEditorSelectedValue);
                            }
                        }
                    }
                }
            }
            return(value);
        }
 public DefaultTypeService(ITypeActivator typesActivator, ITypeSelector typeDiscovery, IAssemblyProvider assemblyProvider)
 {
     _typesActivator   = typesActivator;
     _typeDiscovery    = typeDiscovery;
     _assemblyProvider = assemblyProvider;
 }
 public IEnumerable<Type> RetrieveTypes(ITypeSelector selector)
 {
     return _existingServiceCollection.Where(x => x.ImplementationType != null).Select(x => x.ImplementationType);
 }
예제 #19
0
 public RegisterSelectorOnOriginal(Func <ITypeSelector> selector)
 {
     _selector = selector();
 }
예제 #20
0
 public RegisterSelectorOnClone(Func <ITypeSelector> selector)
 {
     _selector = selector();
 }
예제 #21
0
 public RegisterSelectorBeforeAnyClones(Func <ITypeSelector> selector)
 {
     Selector = selector();
 }
예제 #22
0
        public static IServiceTypeSelector AddTypes <T1, T2, T3>(this ITypeSelector selector)
        {
            Preconditions.NotNull(selector, nameof(selector));

            return(selector.AddTypes(typeof(T1), typeof(T2), typeof(T3)));
        }
예제 #23
0
 public IEnumerable<Type> RetrieveTypes(ITypeSelector selector)
 {
     var choosenSelector = selector ?? new AllSelector();
     var typesToRegistration = _sourceTypes.Where(choosenSelector.DoesSelect).ToList();
     return typesToRegistration;
 }
예제 #24
0
        public static IServiceTypeSelector AddType <T>(this ITypeSelector selector)
        {
            Preconditions.NotNull(selector, nameof(selector));

            return(selector.AddTypes(typeof(T)));
        }
예제 #25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConventionBindingBuilder"/> class.
 /// </summary>
 /// <param name="bindingRoot">The binding root.</param>
 /// <param name="typeSelector">The type selector.</param>
 public ConventionBindingBuilder(IBindingRoot bindingRoot, ITypeSelector typeSelector)
 {
     this.bindingRoot  = bindingRoot;
     this.typeSelector = typeSelector;
 }
예제 #26
0
파일: TypeReader.cs 프로젝트: M-Ch/Liteson
        private static IEnumerable <DeferredEntry> ReadProperties(object target, DeserializationContext context, IReadOnlyDictionary <string, PropertyBinding> all)
        {
            var bufferPart = new BufferPart();
            var reader     = context.Reader;
            List <DeferredEntry> deferred = null;

            while (true)
            {
                var token = reader.Read(ref bufferPart, out var propertyName);
                if (token == JsonToken.ObjectEnd)
                {
                    return(deferred);
                }
                if (token != JsonToken.String)
                {
                    throw Exceptions.BadToken(reader, token, JsonToken.String);
                }

                var hasProperty = all.TryGetValue(propertyName, out var property);
                token = reader.Read(ref bufferPart, out var _);
                if (token != JsonToken.NameSeparator)
                {
                    throw Exceptions.BadToken(reader, token, JsonToken.NameSeparator);
                }

                if (hasProperty)
                {
                    ITypeSelector typeSelector = null;
                    var           shouldDeffer = context.TypeSelectors?.TryGetValue(property.Descriptor.Type, out typeSelector) == true;
                    if (!shouldDeffer)
                    {
                        property.Setter(target, property.Descriptor.Reader(context));
                    }
                    else
                    {
                        var entry = new DeferredEntry
                        {
                            Selector = typeSelector,
                            Binding  = property,
                            Snapshot = reader.TakeSnapshot()
                        };
                        ValueSkipper.SkipNext(reader);
                        if (deferred == null)
                        {
                            deferred = new List <DeferredEntry>();
                        }
                        deferred.Add(entry);
                    }
                }
                else
                {
                    ValueSkipper.SkipNext(reader);
                }

                token = reader.Read(ref bufferPart, out var _);
                if (token == JsonToken.ObjectEnd)
                {
                    return(deferred);
                }
                if (token != JsonToken.ValueSeparator)
                {
                    throw Exceptions.BadToken(reader, token, JsonToken.ValueSeparator);
                }
            }
        }
예제 #27
0
파일: Matcher.cs 프로젝트: jpf/faker-csharp
        /// <summary>
        /// Protected method used to implement our selector-matching strategy. Uses a greedy approach.
        /// </summary>
        /// <param name="property">The meta-data about the property for which we will be finding a match</param>
        /// <param name="targetObject">The object which will receive the property injection</param>
        protected virtual void ProcessProperty(PropertyInfo property, object targetObject)
        {
            //Get the type of the property
            var propertyType = property.PropertyType;

            //Determine if we have a selector-on-hand for this data type
            var selectorCount = TypeMap.CountSelectors(propertyType);

            //We have some matching selectors, so we'll evaluate and return the best match
            if (selectorCount > 0)
            {
                //Evaluate all of the possible selectors and find the first available match
                var selector = EvaluateSelectors(property, TypeMap.GetSelectors(propertyType));

                //We found a matching selector
                if (!(selector is MissingSelector))
                {
                    selector.Generate(targetObject, property); //Bind the property
                    return;                                    //Exit
                }
            }

            //Check to see if the type is a class and has a default constructor
            if (propertyType.IsClass && propertyType.GetConstructor(Type.EmptyTypes) != null)
            {
                var subProperties = propertyType.GetProperties();

                //Create an instance of the underlying subclass
                var subClassInstance = Activator.CreateInstance(propertyType);

                //Match all of the properties on the subclass
                ProcessProperties(subProperties, subClassInstance);

                //Bind the sub-class back onto the original target object
                property.SetValue(targetObject, subClassInstance, null);

                return; //Exit
            }

            //Check to see if the type is an array or any other sort of collection
            if (typeof(IList).IsAssignableFrom(propertyType))
            {
                //Get the underlying type used int he array
                var elementType = propertyType.GetElementType();

                //Get a number of elements we want to create
                //Note: (between 0 and 10 for now)
                var elementCount = Numbers.Int(0, 10);

                //Create an instance of our target array
                IList arrayInstance = null;

                //If we're working with a generic list or any other sort of collection
                if (propertyType.IsGenericType)
                {
                    arrayInstance = (IList)GenericHelper.CreateGeneric(propertyType, elementType);
                }
                else
                {
                    arrayInstance = (IList)GenericHelper.CreateGeneric(typeof(List <>), elementType);
                }

                //Determine if there's a selector available for this type
                var           hasSelector = TypeMap.CountSelectors(elementType) > 0;
                ITypeSelector selector    = null;

                if (hasSelector)
                {
                    //selector = EvaluateSelectors(ele)
                }

                for (var i = 0; i < elementCount; i++)
                {
                    //Create a new element instance
                    var element = Activator.CreateInstance(elementType);

                    if (hasSelector)
                    {
                    }

                    //If the element type is a sub-class, then populate it recursively
                    if (elementType.IsClass)
                    {
                        var subProperties = propertyType.GetProperties();

                        //Populate all of the properties on this object
                        ProcessProperties(subProperties, element);
                    }

                    arrayInstance.Add(element);
                }
            }
        }
예제 #28
0
 private static IEnumerable <Type> SelectPotentialTypes(IEnumerable <ITypesProvider> typesProviders, ITypeSelector convention)
 {
     try
     {
         var potentialTypes = typesProviders.AsParallel().SelectMany(provider => provider.RetrieveTypes(convention));
         return(potentialTypes.ToList());
     }
     catch (AggregateException e)
     {
         throw e.FlattenTryBubbleUp();
     }
 }
 public IEnumerable <Type> RetrieveTypes(ITypeSelector selector)
 {
     return(_existingServiceCollection.Where(x => x.ImplementationType != null).Select(x => x.ImplementationType));
 }
예제 #30
0
 internal static void ValidateExpression <T>(ITypeSelector selector, Func <T> setter)
 {
     ExpressionValidator.IsNotNull(() => setter, setter);
     ExpressionValidator.IsNotNull(selector);
 }