ArgumentHelper.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. #pragma warning disable CS8604 // Possible null reference argument.
  7. namespace EasyDevCore.Common
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public static class ArgumentHelper
  13. {
  14. /// <summary>
  15. /// Outs the specified alias.
  16. /// </summary>
  17. /// <typeparam name="T"></typeparam>
  18. /// <param name="obj">The object.</param>
  19. /// <param name="name">The name.</param>
  20. /// <returns></returns>
  21. public static T Out<T>(this T obj, out T @name)
  22. {
  23. @name = obj;
  24. return obj;
  25. }
  26. /// <summary>
  27. /// Outs the specified selector.
  28. /// </summary>
  29. /// <typeparam name="T"></typeparam>
  30. /// <typeparam name="TResult">The type of the result.</typeparam>
  31. /// <param name="obj">The object.</param>
  32. /// <param name="selector">The selector.</param>
  33. /// <param name="name">The name.</param>
  34. /// <returns></returns>
  35. public static T Out<T, TResult>(this T obj, Func<T, TResult> selector, out TResult @name)
  36. {
  37. @name = selector(obj);
  38. return obj;
  39. }
  40. /// <summary>
  41. /// Gets the arguments has value pair format
  42. /// Ex1: new { Name = ... }
  43. /// Ex2: "Name1,Name2", "a", "b"
  44. /// </summary>
  45. /// <param name="args">The arguments.</param>
  46. /// <returns></returns>
  47. public static IDictionary<string, object> GetArgValues(params object[] args)
  48. {
  49. IDictionary<string, object> values = new Dictionary<string, object>();
  50. if (args.Length == 1)
  51. {
  52. return args[0].GetPropertyValues();
  53. }
  54. else
  55. {
  56. if (args[0] is not string)
  57. {
  58. throw new ArgumentException(args[0].GetTypeName() + " is not supported !");
  59. }
  60. var names = (args[0] as string).SplitByCommaOrSemiColon();
  61. if (names.Length != args.Length - 1)
  62. {
  63. throw new ArgumentException("Length of paramNames and values are not the same !");
  64. }
  65. for (int i = 0; i < names.Length; i++)
  66. {
  67. values.Add(names[i], args[i + 1]);
  68. }
  69. return values;
  70. }
  71. }
  72. }
  73. }