コード例 #1
0
ファイル: OutputAttribute.cs プロジェクト: youngpong/BHoM
        /***************************************************/

        public OutputAttribute(string name, string description, Type quantity = null)
        {
            Name        = name;
            Description = description;
            if (quantity != null && typeof(QuantityAttribute).IsAssignableFrom(quantity) && quantity != typeof(QuantityAttribute))
            {
                Quantity = (QuantityAttribute)Activator.CreateInstance(quantity);
            }
        }
コード例 #2
0
        public static string Description(this QuantityAttribute quantity)
        {
            if (quantity == null)
            {
                Compute.RecordWarning("Cannot query the description of a null quantity attribute. An empty string will be returned isntead.");
                return("");
            }

            return("This is a " + quantity.GetType().Name + " [" + quantity.SIUnit + "]");
        }
コード例 #3
0
        public static List <OutputAttribute> OutputAttributes(this MethodBase method)
        {
            if (method.IsMultipleOutputs())
            {
                Dictionary <int, MultiOutputAttribute> outputDefs = method.GetCustomAttributes <MultiOutputAttribute>().ToDictionary(x => x.Index);
                Type[] types = method.OutputType().GetGenericArguments();

                List <OutputAttribute> outputs = new List <OutputAttribute>();
                for (int i = 0; i < types.Length; i++)
                {
                    if (outputDefs.ContainsKey(i))
                    {
                        string desc = outputDefs[i].Description;

                        if (types[i] != null)
                        {
                            desc += Environment.NewLine;
                            QuantityAttribute quantityAttribute = outputDefs[i].Quantity;
                            desc += types[i].UnderlyingType().Type.Description(quantityAttribute);
                        }
                        outputs.Add(new OutputAttribute(outputDefs[i].Name, desc));
                    }
                    else
                    {
                        outputs.Add(new OutputAttribute(types[i].UnderlyingType().Type.Name.Substring(0, 1), ""));
                    }
                }
                return(outputs);
            }
            else
            {
                OutputAttribute attribute = method.GetCustomAttribute <OutputAttribute>();
                if (attribute != null)
                {
                    return new List <OutputAttribute> {
                               attribute
                    }
                }
                ;
                else
                {
                    return(new List <OutputAttribute>());
                }
            }
        }
コード例 #4
0
        public static string Description(this MemberInfo member)
        {
            DescriptionAttribute descriptionAttribute = member.GetCustomAttribute <DescriptionAttribute>();
            QuantityAttribute    quantityAttribute    = member.GetCustomAttribute <QuantityAttribute>();

            string desc = "";

            if (descriptionAttribute != null && !string.IsNullOrWhiteSpace(descriptionAttribute.Description))
            {
                desc = descriptionAttribute.Description + Environment.NewLine;
            }

            if (member is PropertyInfo && (typeof(IObject).IsAssignableFrom(((PropertyInfo)member).PropertyType)))
            {
                desc += ((PropertyInfo)member).PropertyType.Description(quantityAttribute) + Environment.NewLine;
            }

            return(desc);
        }
コード例 #5
0
        public static string OutputDescription(this MethodBase method)
        {
            OutputAttribute   attribute         = method.GetCustomAttribute <OutputAttribute>();
            QuantityAttribute quantityAttribute = null;

            string desc = "";

            if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Description))
            {
                desc = attribute.Description + Environment.NewLine;
            }

            if (attribute != null)
            {
                quantityAttribute = attribute.Quantity;
            }

            desc += method.OutputType().Description(quantityAttribute);

            return(desc);
        }
コード例 #6
0
        public static string Description(this ParameterInfo parameter)
        {
            IEnumerable <InputAttribute> inputDesc         = parameter.Member.GetCustomAttributes <InputAttribute>().Where(x => x.Name == parameter.Name);
            QuantityAttribute            quantityAttribute = null;
            string desc = "";

            if (inputDesc.Count() > 0)
            {
                desc = inputDesc.First().Description + Environment.NewLine;
                quantityAttribute = inputDesc.First().Quantity;
            }
            else
            {
                //If no input descs are found, check if inputFromProperty descs can be found
                IEnumerable <InputFromProperty> inputFromPropDesc = parameter.Member.GetCustomAttributes <InputFromProperty>().Where(x => x.InputName == parameter.Name);

                //Only valid for engine type methods
                MethodInfo methodInfo = parameter.Member as MethodInfo;
                if (inputFromPropDesc.Count() > 0 && methodInfo != null && methodInfo.DeclaringType != null)
                {
                    Type returnType = methodInfo.ReturnType.UnderlyingType().Type;
                    if (returnType != null)
                    {
                        //Try to find matching proeprty type, matching both name and type
                        PropertyInfo prop = returnType.GetProperty(inputFromPropDesc.First().PropertyName, parameter.ParameterType);

                        //If found return description of property
                        if (prop != null)
                        {
                            return(prop.Description());
                        }
                    }
                }
            }
            if (parameter.ParameterType != null)
            {
                desc += parameter.ParameterType.Description(quantityAttribute);
            }
            return(desc);
        }
コード例 #7
0
        public static string Description(this Type type, QuantityAttribute quantityAttribute)
        {
            if (type == null)
            {
                return("");
            }

            DescriptionAttribute attribute = type.GetCustomAttribute <DescriptionAttribute>();

            string desc = "";

            //If a quantity attribute is present, this is used to generate the default description
            if (quantityAttribute != null)
            {
                desc += "This is a " + quantityAttribute.GetType().Name + " [" + quantityAttribute.SIUnit + "]";
                desc += " (as a " + type.ToText(type.Namespace.StartsWith("BH.")) + ")";
                return(desc);
            }

            //Add the default description
            desc += "This is a " + type.ToText(type.Namespace.StartsWith("BH."));

            if (attribute != null)
            {
                desc += ":" + Environment.NewLine;
                desc += attribute.Description;
            }

            //If type is enum, list options with descriptions
            if (type.IsEnum)
            {
                desc += EnumItemDescription(type);
            }

            Type innerType = type;

            while (typeof(IEnumerable).IsAssignableFrom(innerType) && innerType.IsGenericType)
            {
                innerType = innerType.GenericTypeArguments.First();
            }

            if (innerType.IsInterface)
            {
                desc += Environment.NewLine;
                desc += "This can be of the following types: ";
                List <Type> t = innerType.ImplementingTypes();
                int         m = Math.Min(15, t.Count);

                for (int i = 0; i < m; i++)
                {
                    desc += $"{t[i].ToText()}, ";
                }

                if (t.Count > m)
                {
                    desc += "and more...";
                }
                else
                {
                    desc = desc.Remove(desc.Length - 2, 2);
                }

                return(desc);
            }

            return(desc);
        }