Exemplo n.º 1
0
        private static string GetExampleValue(PropertyInfo prop, string append, int recursion)
        {
            Type propertyType = prop.PropertyType;

            string exampleValue = null;

            if (Attribute.IsDefined(prop, typeof(MiningSettingAttribute)))
            {
                MiningSettingAttribute attrib = Attribute.GetCustomAttribute(prop, typeof(MiningSettingAttribute)) as MiningSettingAttribute;

                if (attrib != null)
                {
                    exampleValue = attrib.ExampleValue;
                }
            }

            if (string.IsNullOrEmpty(exampleValue))
            {
                if (propertyType == typeof(string))
                {
                    if (string.IsNullOrEmpty(exampleValue))
                    {
                        exampleValue = "A string";
                    }
                }
                else if ((Attribute.IsDefined(propertyType, typeof(DataContractAttribute)) && !propertyType.IsAbstract) ||
                         (propertyType.IsArray && Attribute.IsDefined(propertyType.GetElementType(), typeof(DataContractAttribute)) && !propertyType.GetElementType().IsAbstract))
                {
                    Type propType = (propertyType.IsArray ? propertyType.GetElementType() : propertyType);

                    if (string.IsNullOrEmpty(exampleValue))
                    {
                        IEnumerable <PropertyInfo> serializableProperties = propType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(propertyInfo => propertyInfo.CanRead && propertyInfo.CanWrite && !Attribute.IsDefined(propertyInfo, typeof(IgnoreDataMemberAttribute)));
                        exampleValue = GetJSONFormatExample(propType, serializableProperties, append + "    ", false, recursion + 1);
                    }

                    if (propertyType.IsArray)
                    {
                        exampleValue = string.Format("[{0}]", exampleValue);
                    }
                }
                else if (propertyType.IsArray)
                {
                    if (string.IsNullOrEmpty(exampleValue))
                    {
                        exampleValue = "[]";
                    }
                }
                else
                {
                    exampleValue = "\"Unknown type. Disregard this example.\"";
                }
            }

            if (propertyType == typeof(string))
            {
                return(string.Format("\"{0}\"", exampleValue));
            }
            else
            {
                return(exampleValue);
            }
        }
Exemplo n.º 2
0
        public static void DisplayKnownTypeInfo(string typeName)
        {
            foreach (Type t in KnownTypes.Where(t => t.Name.ToLowerInvariant() == typeName.ToLowerInvariant()))
            {
                Console.WriteLine();
                Console.WriteLine();
                OuputJSONTypeString(t);
                Console.WriteLine();

                if (Attribute.IsDefined(t, typeof(MiningModuleAttribute)))
                {
                    MiningModuleAttribute attrib = Attribute.GetCustomAttribute(t, typeof(MiningModuleAttribute)) as MiningModuleAttribute;

                    if (attrib != null)
                    {
                        Console.WriteLine("    Description: ");
                        Console.WriteLine();
                        Console.WriteLine("        {0}", attrib.Description);
                    }
                }

                IEnumerable <PropertyInfo> serializableProperties = t.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(prop => prop.CanRead && prop.CanWrite && !Attribute.IsDefined(prop, typeof(IgnoreDataMemberAttribute)));

                foreach (PropertyInfo prop in serializableProperties)
                {
                    Console.WriteLine();

                    string name = "";

                    if (Attribute.IsDefined(prop, typeof(DataMemberAttribute)))
                    {
                        DataMemberAttribute dataMemberAttrib = Attribute.GetCustomAttribute(prop, typeof(DataMemberAttribute)) as DataMemberAttribute;

                        if (dataMemberAttrib != null)
                        {
                            name = dataMemberAttrib.Name;
                        }
                    }
                    else
                    {
                        name = prop.Name;
                    }

                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.Write("    {0}", name);
                    Console.ResetColor();
                    Console.Write(" : ");
                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                    Console.Write(prop.PropertyType.Name);
                    Console.ResetColor();

                    if (Attribute.IsDefined(prop, typeof(MiningSettingAttribute)))
                    {
                        MiningSettingAttribute attrib = Attribute.GetCustomAttribute(prop, typeof(MiningSettingAttribute)) as MiningSettingAttribute;

                        if (attrib != null)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkMagenta;
                            Console.WriteLine((attrib.Optional ? " (Optional)" : ""));
                            Console.ResetColor();
                            Console.WriteLine();
                            Console.WriteLine("        {0}", attrib.Description);
                        }
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                }

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("Example JSON Format:");
                Console.WriteLine();
                Console.WriteLine(GetJSONFormatExample(t, serializableProperties, "    "));

                Console.WriteLine();
            }
        }