Utils.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // Define JsonKit_NO_DYNAMIC to disable Expando support
  15. // Define JsonKit_NO_EMIT to disable Reflection.Emit
  16. // Define JsonKit_NO_DATACONTRACT to disable support for [DataContract]/[DataMember]
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Reflection;
  21. using System.Collections;
  22. namespace Topten.JsonKit
  23. {
  24. static class Utils
  25. {
  26. // Get all fields and properties of a type
  27. public static IEnumerable<MemberInfo> GetAllFieldsAndProperties(Type t)
  28. {
  29. if (t == null)
  30. return Enumerable.Empty<FieldInfo>();
  31. BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;
  32. return t.GetMembers(flags).Where(x => x is FieldInfo || x is PropertyInfo).Concat(GetAllFieldsAndProperties(t.BaseType));
  33. }
  34. public static Type FindGenericInterface(Type type, Type tItf)
  35. {
  36. foreach (var t in type.GetInterfaces())
  37. {
  38. // Is this a generic list?
  39. if (t.IsGenericType && t.GetGenericTypeDefinition() == tItf)
  40. return t;
  41. }
  42. return null;
  43. }
  44. public static bool IsPublic(MemberInfo mi)
  45. {
  46. // Public field
  47. var fi = mi as FieldInfo;
  48. if (fi != null)
  49. return fi.IsPublic;
  50. // Public property
  51. // (We only check the get method so we can work with anonymous types)
  52. var pi = mi as PropertyInfo;
  53. if (pi != null)
  54. {
  55. var gm = pi.GetGetMethod(true);
  56. return (gm != null && gm.IsPublic);
  57. }
  58. return false;
  59. }
  60. public static Type ResolveInterfaceToClass(Type tItf)
  61. {
  62. // Generic type
  63. if (tItf.IsGenericType)
  64. {
  65. var genDef = tItf.GetGenericTypeDefinition();
  66. // IList<> -> List<>
  67. if (genDef == typeof(IList<>))
  68. {
  69. return typeof(List<>).MakeGenericType(tItf.GetGenericArguments());
  70. }
  71. // IDictionary<string,> -> Dictionary<string,>
  72. if (genDef == typeof(IDictionary<,>) && tItf.GetGenericArguments()[0] == typeof(string))
  73. {
  74. return typeof(Dictionary<,>).MakeGenericType(tItf.GetGenericArguments());
  75. }
  76. }
  77. // IEnumerable -> List<object>
  78. if (tItf == typeof(IEnumerable))
  79. return typeof(List<object>);
  80. // IDicitonary -> Dictionary<string,object>
  81. if (tItf == typeof(IDictionary))
  82. return typeof(Dictionary<string, object>);
  83. return tItf;
  84. }
  85. public static long ToUnixMilliseconds(DateTime This)
  86. {
  87. return (long)This.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
  88. }
  89. public static DateTime FromUnixMilliseconds(long timeStamp)
  90. {
  91. return new DateTime(1970, 1, 1).AddMilliseconds(timeStamp);
  92. }
  93. public static void DeleteFile(string filename)
  94. {
  95. try
  96. {
  97. System.IO.File.Delete(filename);
  98. }
  99. catch
  100. {
  101. // Don't care
  102. }
  103. }
  104. }
  105. }