Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflection_Engine: Extract descriptions of enum options #1586

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion Reflection_Engine/Query/Description.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public static string Description(this Type type)
}

/***************************************************/

[Description("Return the custom description of a C# class")]
public static string Description(this Type type, QuantityAttribute quantityAttribute)
{
Expand All @@ -120,7 +121,6 @@ public static string Description(this Type type, QuantityAttribute quantityAttri

string desc = "";


//If a quantity attribute is present, this is used to generate the default description
if (quantityAttribute != null)
{
Expand All @@ -137,6 +137,11 @@ public static string Description(this Type type, QuantityAttribute quantityAttri
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)
Expand Down Expand Up @@ -180,6 +185,41 @@ public static string IDescription(this object item)
return "";
}

/***************************************************/
/**** Private Methods ****/
/***************************************************/

[Description("Lists all the enum options and their descriptions.")]
private static string EnumItemDescription(this Type type)
{
FieldInfo[] fields = type.GetFields();
string desc = Environment.NewLine + "Enum values:";

int m = Math.Min(fields.Length, 20);

for (int i = 0; i < m; i++)
{
FieldInfo field = fields[i];

//Skip the value option
if (field.Name == "value__")
continue;

desc += Environment.NewLine;
desc += "-" + field.Name;

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

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

if (fields.Length > m)
desc += Environment.NewLine + "-...And more";

return desc;
}

/***************************************************/
}
}
Expand Down