Utils.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // JsonKit v0.5 - A simple but flexible Json library in a single .cs file.
  2. //
  3. // Copyright (C) 2014 Topten Software (contact@toptensoftware.com) All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this product
  6. // except in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the
  11. // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. // either express or implied. See the License for the specific language governing permissions
  13. // and limitations under the License.
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Reflection;
  18. using System.Collections;
  19. namespace Topten.JsonKit
  20. {
  21. static class Utils
  22. {
  23. // Get all fields and properties of a type
  24. public static IEnumerable<MemberInfo> GetAllFieldsAndProperties(Type t)
  25. {
  26. if (t == null)
  27. return Enumerable.Empty<FieldInfo>();
  28. BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;
  29. return t.GetMembers(flags).Where(x => x is FieldInfo || x is PropertyInfo).Concat(GetAllFieldsAndProperties(t.BaseType));
  30. }
  31. public static Type FindGenericInterface(Type type, Type tItf)
  32. {
  33. foreach (var t in type.GetInterfaces())
  34. {
  35. // Is this a generic list?
  36. if (t.IsGenericType && t.GetGenericTypeDefinition() == tItf)
  37. return t;
  38. }
  39. return null;
  40. }
  41. public static bool IsPublic(MemberInfo mi)
  42. {
  43. // Public field
  44. var fi = mi as FieldInfo;
  45. if (fi != null)
  46. return fi.IsPublic;
  47. // Public property
  48. // (We only check the get method so we can work with anonymous types)
  49. var pi = mi as PropertyInfo;
  50. if (pi != null)
  51. {
  52. var gm = pi.GetGetMethod(true);
  53. return (gm != null && gm.IsPublic);
  54. }
  55. return false;
  56. }
  57. public static Type ResolveInterfaceToClass(Type tItf)
  58. {
  59. // Generic type
  60. if (tItf.IsGenericType)
  61. {
  62. var genDef = tItf.GetGenericTypeDefinition();
  63. // IList<> -> List<>
  64. if (genDef == typeof(IList<>))
  65. {
  66. return typeof(List<>).MakeGenericType(tItf.GetGenericArguments());
  67. }
  68. // IDictionary<string,> -> Dictionary<string,>
  69. if (genDef == typeof(IDictionary<,>) && tItf.GetGenericArguments()[0] == typeof(string))
  70. {
  71. return typeof(Dictionary<,>).MakeGenericType(tItf.GetGenericArguments());
  72. }
  73. }
  74. // IEnumerable -> List<object>
  75. if (tItf == typeof(IEnumerable))
  76. return typeof(List<object>);
  77. // IDictionary -> Dictionary<string,object>
  78. if (tItf == typeof(IDictionary))
  79. return typeof(Dictionary<string, object>);
  80. return tItf;
  81. }
  82. public static long ToUnixMilliseconds(DateTime This)
  83. {
  84. return (long)This.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
  85. }
  86. public static DateTime FromUnixMilliseconds(long timeStamp)
  87. {
  88. return new DateTime(1970, 1, 1).AddMilliseconds(timeStamp);
  89. }
  90. public static void DeleteFile(string filename)
  91. {
  92. try
  93. {
  94. System.IO.File.Delete(filename);
  95. }
  96. catch
  97. {
  98. // Don't care
  99. }
  100. }
  101. }
  102. }