Пример #1
0
        private static IDictionary <string, FunctionParameter> GetModelParameters(Type modelType)
        {
            var dictionary = new Dictionary <string, FunctionParameter>();

            foreach (var info in modelType.GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                if (((info.GetSetMethod(false) == null)) ||
                    info.GetCustomAttributes(typeof(FunctionParameterIgnoreAttribute), false).Any())
                {
                    continue;
                }

                var propertyType = info.PropertyType;
                var name         = info.Name;

                FunctionParameterAttribute functionParameterAttribute = null;

                var source = info.GetCustomAttributes <FunctionParameterAttribute>(false).ToList();
                if (source.Count > 1)
                {
                    Log.LogWarning(LogTitle, String.Format("More than one '{0}' attribute defined on property '{1}'", typeof(FunctionParameterAttribute).Name, name));
                }
                else
                {
                    functionParameterAttribute = source.FirstOrDefault();
                }

                WidgetFunctionProvider widgetProvider = null;
                if ((functionParameterAttribute != null) && functionParameterAttribute.HasWidgetMarkup)
                {
                    try
                    {
                        widgetProvider = functionParameterAttribute.GetWidgetFunctionProvider(modelType, info);
                    }
                    catch (Exception exception)
                    {
                        Log.LogWarning(LogTitle, String.Format("Failed to get widget function provider for parameter property {0}.", info.Name));
                        Log.LogWarning(LogTitle, exception);
                    }
                }

                if (!dictionary.ContainsKey(name))
                {
                    dictionary.Add(name, new FunctionParameter(name, propertyType, functionParameterAttribute, widgetProvider));
                }
            }

            return(dictionary);
        }
        internal static ParameterProfile BuildParameterProfile(string name, Type type, FunctionParameterAttribute attribute, Type controllerType)
        {
            BaseValueProvider defaultValueProvider = new NoValueValueProvider();
            string            label    = name;
            string            helpText = String.Empty;

            if (!attribute.Label.IsNullOrEmpty())
            {
                label = attribute.Label;
            }

            if (!attribute.Help.IsNullOrEmpty())
            {
                helpText = attribute.Help;
            }

            bool isRequired = !attribute.HasDefaultValue;

            if (!isRequired)
            {
                defaultValueProvider = new ConstantValueProvider(attribute.DefaultValue);
            }

            WidgetFunctionProvider widgetProvider = attribute.GetWidgetFunctionProvider(controllerType, null);

            bool hideInSimpleView = attribute.HideInSimpleView;

            if (widgetProvider == null)
            {
                widgetProvider = StandardWidgetFunctions.GetDefaultWidgetFunctionProviderByType(type, isRequired);
            }

            return(new ParameterProfile(name, type, isRequired, defaultValueProvider, widgetProvider, label,
                                        new HelpDefinition(helpText),
                                        hideInSimpleView));
        }
Пример #3
0
        /// <summary>
        /// Extracts the function paramteres from an object that represents a function.
        /// </summary>
        /// <param name="functionObject">The object that represents a function.</param>
        /// <param name="baseFunctionType">Type of the base function.</param>
        /// <param name="filePath">Physical file location of the file behind the function, used for logging.</param>
        /// <returns></returns>
        public static IDictionary <string, FunctionParameter> GetParameters(object functionObject, Type baseFunctionType, string filePath)
        {
            var functionParameters = new Dictionary <string, FunctionParameter>();
            IDictionary <string, WidgetFunctionProvider> parameterWidgets = GetParameterWidgets(functionObject);

            var type = functionObject.GetType();

            while (type != baseFunctionType && type != null)
            {
                var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.DeclaredOnly);
                foreach (var property in properties)
                {
                    // Skipping overriden base properties
                    if (property.GetAccessors()[0].GetBaseDefinition().DeclaringType == baseFunctionType)
                    {
                        continue;
                    }
                    // Skipping private setters
                    if (property.GetSetMethod(false) == null)
                    {
                        continue;
                    }
                    // Skipping explicitly ignored attributes
                    if (property.GetCustomAttributes(typeof(FunctionParameterIgnoreAttribute), false).Any())
                    {
                        continue;
                    }

                    var propType = property.PropertyType;
                    var name     = property.Name;

                    FunctionParameterAttribute attr = null;
                    var attributes = property.GetCustomAttributes(typeof(FunctionParameterAttribute), false).Cast <FunctionParameterAttribute>().ToList();

                    if (attributes.Count > 1)
                    {
                        Log.LogWarning(LogTitle, "More than one '{0}' attribute defined on property '{1}'. Location: '{2}'"
                                       .FormatWith(typeof(FunctionParameterAttribute).Name, name, filePath));
                    }
                    else
                    {
                        attr = attributes.FirstOrDefault();
                    }

                    WidgetFunctionProvider attibuteBasedWidgetProvider = null;
                    WidgetFunctionProvider methodBasedWidgetProvider   = null;

                    if (attr != null && attr.HasWidgetMarkup)
                    {
                        try
                        {
                            attibuteBasedWidgetProvider = attr.GetWidgetFunctionProvider(type, property);
                        }
                        catch (Exception ex)
                        {
                            Log.LogWarning(LogTitle, "Failed to get widget function provider for parameter property {0}. Location: '{1}'"
                                           .FormatWith(property.Name, filePath));
                            Log.LogWarning(LogTitle, ex);
                        }
                    }

                    parameterWidgets.TryGetValue(name, out methodBasedWidgetProvider);

                    if (methodBasedWidgetProvider != null && attibuteBasedWidgetProvider != null)
                    {
                        Log.LogWarning(LogTitle, "Widget for property {0} is defined in both {1} attribute and in {2}() method. Remove one of the definitions. Location: '{3}'"
                                       .FormatWith(property.Name, nameof(FunctionParameterAttribute), nameof(IParameterWidgetsProvider.GetParameterWidgets), filePath));
                    }

                    if (!functionParameters.ContainsKey(name))
                    {
                        functionParameters.Add(name, new FunctionParameter(name, propType, attr, attibuteBasedWidgetProvider ?? methodBasedWidgetProvider));
                    }
                }

                type = type.BaseType;
            }

            return(functionParameters);
        }