public static BindingDefinitionMethod CreateInstance(MethodInfo methodInfo)
        {
            if (methodInfo == null)
            {
                return(null);
            }

            BindingDefinitionDescription definitionDescription = new BindingDefinitionDescription()
            {
                BindingExpression = methodInfo.Name, IsReadOnly = true
            };

            if (string.IsNullOrEmpty(definitionDescription.Description))
            {
                object[] descriptions = methodInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (descriptions != null && descriptions.Any())
                {
                    definitionDescription.Description = (descriptions[0] as DescriptionAttribute).Description;
                }
            }

            BindingDefinitionMethod definition = new BindingDefinitionMethod(definitionDescription)
            {
                BindingType = methodInfo.ReturnType,
                MethodInfo  = methodInfo
            };

            definition.ManageCollectionStatus();
            definition.ManageEnumAndNullable();

            return(definition);
        }
Exemple #2
0
 protected BindingDefinition(BindingDefinitionDescription bindingDefinitionDescription)
 {
     DefinitionDescription = bindingDefinitionDescription;
     CanNotify             = false;
     IsBoundWithData       = true;
     IsReadOnly            = DefinitionDescription?.IsReadOnly ?? true;
 }
        public static BindingDefinitionOptional CreateInstance(BindingDefinitionDescription definitionDescription)
        {
            BindingDefinitionOptional definition = new BindingDefinitionOptional(definitionDescription)
            {
                IsOptional = true
            };

            return(definition);
        }
Exemple #4
0
        /// <summary>
        /// List<PropertyInfo> propertyInfos:  To detect the 'new' properties
        /// </summary>
        public static BindingDefinitionProperty CreateInstance(PropertyInfo propertyInfo, BindingDefinitionDescription definitionDescription)
        {
            if (propertyInfo == null)
            {
                return(null);
            }

            if (definitionDescription == null)
            {
                definitionDescription = new BindingDefinitionDescription()
                {
                    Name = propertyInfo.Name
                }
            }
            ;

            definitionDescription.IsReadOnly = definitionDescription.IsReadOnly || !propertyInfo.CanWrite || propertyInfo.GetSetMethod() == null;

            if (string.IsNullOrEmpty(definitionDescription.Description))
            {
                object[] descriptions = propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (descriptions != null && descriptions.Any())
                {
                    definitionDescription.Description = (descriptions[0] as DescriptionAttribute).Description;
                }
            }

            BindingDefinitionProperty definition = new BindingDefinitionProperty(definitionDescription)
            {
                BindingType          = propertyInfo.PropertyType,
                BindingTypeIsGeneric = propertyInfo.PropertyType.IsGenericType,
                PropertyInfo         = propertyInfo,
                GetMethod            = propertyInfo.GetGetMethod(),
                SetMethod            = propertyInfo.GetSetMethod()
            };

            if (definition.BindingTypeIsGeneric)
            {
                definition.BindingGenericType           = definition.BindingType.GetGenericArguments()[0];
                definition.BindingGenericTypeDefinition = definition.BindingType.GetGenericTypeDefinition();
            }
            definition.CanNotify = !definition.IsACollection;

            definition.ManageCollectionStatus();
            definition.ManageEnumAndNullable();

            return(definition);
        }
Exemple #5
0
        public static BindingDefinitionRoot CreateInstance(Type sourceType)
        {
            BindingDefinitionDescription definitionDescription = new BindingDefinitionDescription()
            {
                IsReadOnly = true
            };

            BindingDefinitionRoot definition = new BindingDefinitionRoot(definitionDescription)
            {
                BindingType = sourceType
            };

            if (definition.BindingType != null)
            {
                definition.ManageCollectionStatus();
                definition.ManageEnumAndNullable();
            }
            return(definition);
        }
        public static BindingDefinitionField CreateInstance(FieldInfo fieldInfo, BindingDefinitionDescription definitionDescription)
        {
            BindingDefinitionField definition = new BindingDefinitionField(definitionDescription)
            {
                BindingType          = fieldInfo.FieldType,
                BindingTypeIsGeneric = fieldInfo.FieldType.IsGenericType,
                FieldInfo            = fieldInfo
            };

            if (definition.BindingTypeIsGeneric)
            {
                definition.BindingGenericType           = definition.BindingType.GetGenericArguments()[0];
                definition.BindingGenericTypeDefinition = definition.BindingType.GetGenericTypeDefinition();
            }
            definition.ManageCollectionStatus();
            definition.ManageEnumAndNullable();

            return(definition);
        }
        public static BindingDefinitionHierarchical CreateInstance(Type type, BindingDefinitionDescription definitionDescription)
        {
            try
            {
                if (string.IsNullOrEmpty(definitionDescription.Name))
                {
                    definitionDescription.Name = definitionDescription.BindingExpression.Replace('.', '_');
                }

                BindingDefinitionHierarchical definition = new BindingDefinitionHierarchical(definitionDescription);

                string realBindingDefinitionExpression = definitionDescription.BindingExpression.Split('.')[0];

                BindingDefinitionDescription realBindingDefinitionDescription = new BindingDefinitionDescription()
                {
                    BindingExpression = realBindingDefinitionExpression, IsReadOnly = definitionDescription.IsReadOnly
                };
                definition.realBindingDefinition = BindingDefinitionFactory.CreateInstance(type, realBindingDefinitionDescription);

                string childBindingExpression = definitionDescription.BindingExpression.Substring(realBindingDefinitionExpression.Length + 1);
                BindingDefinitionDescription childBindingDescription = new BindingDefinitionDescription()
                {
                    BindingExpression = childBindingExpression, IsReadOnly = definitionDescription.IsReadOnly
                };
                definition.childBindingDefinition = BindingDefinitionFactory.CreateInstance(definition.realBindingDefinition.BindingType, childBindingDescription);

                definition.CanNotify = definition.childBindingDefinition.CanNotify;

                definition.BindingType = definition.childBindingDefinition.BindingType; // type;
                return(definition);
            }
            catch (Exception ex)
            {
                throw new BindingTemplateException($"Cannot create the 'Hierarchical BindingDefinition' '{definitionDescription.BindingExpression}'. {ex.Message}");
            }
        }
 private BindingDefinitionField(BindingDefinitionDescription definitionDescription) : base(definitionDescription)
 {
 }
 private BindingDefinitionOptional(BindingDefinitionDescription definitionDescription) : base(definitionDescription)
 {
 }
 private BindingDefinitionComposite(BindingDefinitionDescription bindingDefinitionDescription)
     : base(bindingDefinitionDescription)
 {
 }
        public static BindingDefinitionComposite CreateInstance(Type type, BindingDefinitionDescription definitionDescription)
        {
            try
            {
                if (string.IsNullOrEmpty(definitionDescription.Name))
                {
                    definitionDescription.Name = definitionDescription.BindingExpression.Replace('.', '_');
                    MatchCollection ret = ValidCharExtract.Matches(definitionDescription.Name);
                    StringBuilder   sb  = new StringBuilder();
                    foreach (Match m in ret)
                    {
                        sb.Append(m.Value);
                    }
                    definitionDescription.Name = sb.ToString();
                }

                definitionDescription.BindingExpression = definitionDescription.BindingExpression.Substring(1);
                definitionDescription.BindingExpression = definitionDescription.BindingExpression.Substring(0, definitionDescription.BindingExpression.Length - 1);

                BindingDefinitionComposite definition = null;
                string          bindingFormat         = definitionDescription.BindingExpression;
                List <string>   results = new List <string>();
                MatchCollection matches = Regex.Matches(bindingFormat, pattern);

                int cpt = -1;
                foreach (Match match in matches)
                {
                    string[] elements = match.Value.Split(new[] { "::" }, StringSplitOptions.None);
                    if (string.IsNullOrEmpty(elements[0]))
                    {
                        bindingFormat = bindingFormat.Replace($"{{{match.Value}}}", string.Empty);
                    }
                    else
                    {
                        int pos = results.FindIndex(s => s.Equals(elements[0]));
                        if (pos == -1)
                        {
                            results.Add(elements[0]);
                            pos = ++cpt;
                        }
                        else
                        {
                            pos = cpt;
                        }
                        string format = $"{{{match.Value}}}";
                        bindingFormat = bindingFormat.Replace(format, $"{{{pos}}}");
                    }
                }
                if (results.Count > 0)
                {
                    List <BindingDefinitionDescription> definitionDescriptions = new List <BindingDefinitionDescription>();
                    if (results.Count == 1)
                    {
                        definitionDescriptions.Add(new BindingDefinitionDescription()
                        {
                            BindingExpression = results[0], IsReadOnly = definitionDescription.IsReadOnly
                        });
                    }
                    else
                    {
                        definitionDescriptions.AddRange(results.Select(s => new BindingDefinitionDescription()
                        {
                            BindingExpression = s, IsReadOnly = true
                        }));
                    }

                    List <IBindingDefinition> nestedDefinitions = BindingDefinitionFactory.CreateInstances(type, definitionDescriptions);

                    if (nestedDefinitions.FirstOrDefault(nd => nd.IsACollection) != null)
                    {
                        throw new BindingTemplateException("The nested 'BindingDefinition' of a 'Composite BindingDefinition' cannot be a collection");
                    }

                    // If more than one nested definition, then force the Binding definition to be ReadOnly
                    definitionDescription.IsReadOnly = nestedDefinitions.Count > 1 || nestedDefinitions[0].IsReadOnly;
                    definition = new BindingDefinitionComposite(definitionDescription)
                    {
                        nestedDefinitions = nestedDefinitions, BindingFormat = bindingFormat, BindingType = typeof(string)
                    };
                    definition.canBeNotifiedNestedDefinitions = definition.nestedDefinitions.Where(d => d.CanNotify).ToList();
                    definition.CanNotify = definition.canBeNotifiedNestedDefinitions.Count > 0;
                }
                return(definition);
            }
            catch (Exception ex)
            {
                throw new BindingTemplateException($"Cannot create the 'Composite BindingDefinition' '{definitionDescription.BindingExpression}'. {ex.Message}");
            }
        }
Exemple #12
0
        /// <summary> Create a binding definition for a given <see cref="BindingDefinitionDescription"/> of a given <see cref="Type"/> </summary>
        /// <param name="sourceType">The Type on which the '<see cref="BindingDefinitionDescription"/>' is based</param>
        /// <param name="definitionDescription">The given <see cref="Type"/></param>
        /// <returns>The newly created Binding definition or an exception is an error occurs</returns>
        internal static IBindingDefinition CreateInstance(Type sourceType, BindingDefinitionDescription definitionDescription)
        {
            if (string.IsNullOrEmpty(definitionDescription?.BindingExpression))
            {
                return(null);
            }

            try
            {
                /// Optional
                ////////////
                if (sourceType == null)
                {
                    return(BindingDefinitionOptional.CreateInstance(definitionDescription));
                }

                /// Composite
                /////////////
                if (definitionDescription.BindingExpression.StartsWith("{") && definitionDescription.BindingExpression.EndsWith("}"))
                {
                    return(BindingDefinitionComposite.CreateInstance(sourceType, definitionDescription));
                }

                /// Hierarchical
                ////////////////
                if (definitionDescription.BindingExpression.Contains("."))
                {
                    return(BindingDefinitionHierarchical.CreateInstance(sourceType, definitionDescription));
                }


                //// keyword
                ////////////
                //if (definition == null && BindingDefinitionKeyWord.KeyWords.Contains(bindingName))
                //    definition = BindingDefinitionKeyWord.CreateInstances(bindingName);


                /// Properties
                //////////////
                List <PropertyInfo> propertyInfos = (from pi in sourceType.GetProperties()
                                                     where pi.Name.Equals(definitionDescription.BindingExpression) && pi.GetGetMethod() != null && pi.GetGetMethod().IsPublic
                                                     select pi).ToList();

                if (propertyInfos != null && propertyInfos.Count > 0)
                {
                    PropertyInfo propertyInfo;
                    if (propertyInfos.Count == 1)
                    {
                        propertyInfo = propertyInfos[0];
                    }
                    else // To take the keuword 'new' into account
                    {
                        propertyInfo = propertyInfos.FirstOrDefault(pi => { MethodInfo mi = pi.GetGetMethod();
                                                                            bool isNew    = (mi.Attributes & MethodAttributes.HideBySig) == MethodAttributes.HideBySig && mi.DeclaringType.Equals(sourceType);
                                                                            return(isNew); });
                    }
                    return(BindingDefinitionProperty.CreateInstance(propertyInfo, definitionDescription));
                }

                /// Fields
                //////////
                FieldInfo fieldInfo = sourceType.GetFields().FirstOrDefault(fi => fi.Name.Equals(definitionDescription.BindingExpression) && fi.IsPublic);
                if (fieldInfo != null)
                {
                    return(BindingDefinitionField.CreateInstance(fieldInfo, definitionDescription));
                }

                /// Methods
                ///////////
                MethodInfo methodInfo = sourceType.GetMethods().FirstOrDefault(mi => mi.Name.Equals(definitionDescription.BindingExpression) && mi.IsPublic);
                if (methodInfo != null)
                {
                    return(BindingDefinitionMethod.CreateInstance(methodInfo));
                }

                return(BindingDefinitionOptional.CreateInstance(definitionDescription));
            }
            catch (Exception ex)
            {
                throw new BindingTemplateException($"Cannot create 'BindingDefinition' between '{sourceType.Name}' and '{definitionDescription.BindingExpression}'", ex);
            }
        }
Exemple #13
0
 private BindingDefinitionRoot(BindingDefinitionDescription definitionDescription) : base(definitionDescription)
 {
 }
Exemple #14
0
 protected BindingDefinitionProperty(BindingDefinitionDescription bindingDefinitionDescription) : base(bindingDefinitionDescription)
 {
 }
 private BindingDefinitionHierarchical(BindingDefinitionDescription definitionDescription) : base(definitionDescription)
 {
 }
Exemple #16
0
        /// <summary> Create a binding definition for a given <see cref="BindingDefinitionDescription"/> owned by a given <see cref="FilterOwner"/> </summary>
        /// <param name="templateDefinition">The <see cref="FilterOwner"/> that owned the <see cref="BindingDefinitionDescription"/></param>
        /// <param name="bindingDefinitionDescription">the given <see cref="BindingDefinitionDescription"/></param>
        /// <returns>The newly created Binding definition or an exception is an error occurs</returns>
        public static IBindingDefinition CreateInstances(TemplateDefinition templateDefinition, BindingDefinitionDescription bindingDefinitionDescription)
        {
            IBindingDefinition ret = null;

            if (templateDefinition != null && bindingDefinitionDescription != null)
            {
                if (bindingDefinitionDescription.IsConst)
                {
                    ret = BindingDefinitionConstante.CreateInstance(bindingDefinitionDescription);
                }
                else
                {
                    Type type = templateDefinition.MainBindingDefinition?.BindingType;
                    ret = CreateInstance(type, bindingDefinitionDescription);
                }
            }
            return(ret);
        }
        public static BindingDefinitionDescription CreateBindingDescription(ITemplateDefinition templateDefinition, string toAnalyze, string trimmedToAnalyze)
        {
            BindingDefinitionDescription ret = null;
            List <string> options            = null;

            if (!string.IsNullOrEmpty(trimmedToAnalyze))
            {
                string bindingExpression;
                bool   isConstante = false;
                // Constante
                if (!trimmedToAnalyze.StartsWith("{") || trimmedToAnalyze.StartsWith("["))
                {
                    isConstante = true;
                    if (trimmedToAnalyze.StartsWith("["))
                    {
                        if (!trimmedToAnalyze.EndsWith("]"))
                        {
                            throw new BindingTemplateException($"Cannot create constante BindingDefinition from '{toAnalyze}': cannot find the closing ']'");
                        }
                        bindingExpression = trimmedToAnalyze.Substring(1, trimmedToAnalyze.Length - 2);

                        int postSep = bindingExpression.LastIndexOf("::");
                        if (postSep != -1)
                        {
                            string   optionsString = bindingExpression.Substring(0, postSep);
                            string[] optionsArray  = optionsString.Split(';');
                            options           = optionsArray.Where(p => !string.IsNullOrEmpty(p)).Select(p => p.Trim()).ToList();
                            bindingExpression = bindingExpression.Substring(postSep + 2);
                        }
                    }
                    else
                    {
                        bindingExpression = toAnalyze;
                    }
                }
                // No Constante
                else
                {
                    if (!trimmedToAnalyze.EndsWith("}"))
                    {
                        throw new BindingTemplateException($"Cannot create BindingDefinition from '{toAnalyze}': cannot find the closing '}}'");
                    }
                    bindingExpression = trimmedToAnalyze.Substring(1, trimmedToAnalyze.Length - 2);

                    int postSep = bindingExpression.LastIndexOf("::");
                    if (postSep != -1)
                    {
                        string   optionsString = bindingExpression.Substring(0, postSep);
                        string[] optionsArray  = optionsString.Split(';');
                        options           = optionsArray.Where(p => !string.IsNullOrEmpty(p)).Select(p => p.Trim()).ToList();
                        bindingExpression = bindingExpression.Substring(postSep + 2);
                    }
                    else if (bindingExpression.StartsWith("=")) // USe for Formula not bind with the model
                    {
                        options           = new List <string>(new [] { "F" + bindingExpression });
                        bindingExpression = string.Empty;
                    }
                }

                ret = new BindingDefinitionDescription(templateDefinition, bindingExpression, isConstante, options);
            }
            return(ret);
        }
 private BindingDefinitionMethod(BindingDefinitionDescription definitionDescription) : base(definitionDescription)
 {
 }