コード例 #1
0
        /// <summary> Create a Binding definition for a given 'System.Reflection.PropertyInfo'</summary>
        /// <param name="propertyInfo">The given 'System.Reflection.PropertyInfo'</param>
        /// <returns>The newly created Bindiçng definition or an exception is an error occurs</returns>
        public static IBindingDefinition CreateInstance(PropertyInfo propertyInfo)
        {
            if (propertyInfo == null)
            {
                return(null);
            }

            return(BindingDefinitionProperty.CreateInstance(propertyInfo, null));
        }
コード例 #2
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);
        }
コード例 #3
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);
            }
        }