PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/DSSharp.cs

http://github.com/fredericaltorres/DynamicSugarNet
C# | 135 lines | 65 code | 15 blank | 55 comment | 3 complexity | ffa8f7151865db203598f96f6d600a39 MD5 | raw file
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.IO;
  6. using System.Collections;
  7. #if !MONOTOUCH
  8. using System.Dynamic;
  9. #endif
  10. using System.Reflection;
  11. namespace DynamicSugar {
  12. /// <summary>
  13. /// Dynamic Sharp Exception
  14. /// </summary>
  15. public class DynamicSugarSharpException : System.Exception {
  16. public DynamicSugarSharpException(string message) : base(message) { }
  17. }
  18. /// <summary>
  19. /// Dynamic Sharp Helper Class
  20. /// </summary>
  21. public static partial class DS {
  22. #if !MONOTOUCH
  23. /// <summary>
  24. /// Initialize an Expando object with the properties of one or more instances passed
  25. /// as parameters. Then return the expando object.
  26. /// </summary>
  27. /// <param name="instances"></param>
  28. /// <returns>An expando object</returns>
  29. public static dynamic Expando(params object [] instances) {
  30. dynamic expando = new ExpandoObject();
  31. var expandoAsDict = expando as IDictionary<String, object>;
  32. for (int i = 0; i < instances.Length; i++){
  33. if(instances[i] is string){
  34. expandoAsDict.Add(instances[i].ToString(), instances[i+1]);
  35. i++;
  36. }
  37. else
  38. foreach (KeyValuePair<string, object> k in ReflectionHelper.GetDictionary(instances[i]))
  39. expandoAsDict.Add(k.Key, k.Value);
  40. }
  41. return expando;
  42. }
  43. /// <summary>
  44. /// Wrap an anonynous type into a MultiValues object to be returned
  45. /// as a function result.
  46. /// </summary>
  47. /// <param name="anonymousType"></param>
  48. /// <returns></returns>
  49. public static dynamic Values(object anonymousType){
  50. return DynamicSugar.MultiValues.Values(anonymousType);
  51. }
  52. #endif
  53. /// <summary>
  54. /// Convert the parameters passed to this function into a List Of T
  55. /// </summary>
  56. /// <typeparam name="T"></typeparam>
  57. /// <param name="values"></param>
  58. /// <returns></returns>
  59. public static List<T> List<T>(params T[] values) {
  60. return values.ToList<T>();
  61. }
  62. /// <summary>
  63. /// Return in a dictionary of string, object all the properties and fields of an instance
  64. /// </summary>
  65. /// <param name="instance">The instance</param>
  66. /// <param name="properties">Define the list of property and field to return. All properties and fields are returned if this parameter is not defined</param>
  67. /// <returns></returns>
  68. public static Dictionary<string,object> Dictionary(object instance, List<string> properties = null) {
  69. return ReflectionHelper.GetDictionary(instance, properties);
  70. }
  71. /// <summary>
  72. /// Return in a dictionary of string, T all the properties and fields of an instance.
  73. /// With the method the type of the dictionart value can be defined.
  74. /// </summary>
  75. /// <param name="instance">The instance</param>
  76. /// <param name="properties">Define the list of property and field to return. All properties and fields are returned if this parameter is not defined</param>
  77. /// <returns></returns>
  78. public static Dictionary<string, TValue> Dictionary<TValue>(object instance, List<string> properties = null) {
  79. Dictionary<string, TValue> d = new Dictionary<string,TValue>();
  80. foreach(var k in ReflectionHelper.GetDictionary(instance, properties)){
  81. TValue v = (TValue)Convert.ChangeType(k.Value, typeof(TValue));
  82. d.Add(k.Key, v);
  83. }
  84. return d;
  85. }
  86. /// <summary>
  87. /// Return a list of integer from 0 to max-1
  88. /// </summary>
  89. /// <param name="max"></param>
  90. /// <returns></returns>
  91. public static List<int> Range(int max) {
  92. return Range(max, 1);
  93. }
  94. /// <summary>
  95. /// Return a list of integer from 0 to max-1 with an increment
  96. /// </summary>
  97. /// <param name="max"></param>
  98. /// <param name="increment"></param>
  99. /// <returns></returns>
  100. public static List<int> Range(int max, int increment) {
  101. return Range(0, max, increment);
  102. }
  103. /// <summary>
  104. /// Return a list of integer from start to max-1 with an increment
  105. /// </summary>
  106. /// <param name="start"></param>
  107. /// <param name="max"></param>
  108. /// <param name="increment"></param>
  109. /// <returns></returns>
  110. public static List<int> Range(int start, int max, int increment) {
  111. int i = start;
  112. var l = new List<int>();
  113. while (i < max) {
  114. l.Add(i);
  115. i += increment;
  116. }
  117. return l;
  118. }
  119. }
  120. }