纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.4 KiB

  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace ICSSoft.WebAPI.Areas.HelpPage.ModelDescriptions
  6. {
  7. internal static class ModelNameHelper
  8. {
  9. // Modify this to provide custom model name mapping.
  10. public static string GetModelName(Type type)
  11. {
  12. ModelNameAttribute modelNameAttribute = type.GetCustomAttribute<ModelNameAttribute>();
  13. if (modelNameAttribute != null && !String.IsNullOrEmpty(modelNameAttribute.Name))
  14. {
  15. return modelNameAttribute.Name;
  16. }
  17. string modelName = type.Name;
  18. if (type.IsGenericType)
  19. {
  20. // Format the generic type name to something like: GenericOfAgurment1AndArgument2
  21. Type genericType = type.GetGenericTypeDefinition();
  22. Type[] genericArguments = type.GetGenericArguments();
  23. string genericTypeName = genericType.Name;
  24. // Trim the generic parameter counts from the name
  25. genericTypeName = genericTypeName.Substring(0, genericTypeName.IndexOf('`'));
  26. string[] argumentTypeNames = genericArguments.Select(t => GetModelName(t)).ToArray();
  27. modelName = String.Format(CultureInfo.InvariantCulture, "{0}Of{1}", genericTypeName, String.Join("And", argumentTypeNames));
  28. }
  29. return modelName;
  30. }
  31. }
  32. }