123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Collections;
- namespace Topten.JsonKit
- {
- static class Utils
- {
-
- public static IEnumerable<MemberInfo> GetAllFieldsAndProperties(Type t)
- {
- if (t == null)
- return Enumerable.Empty<FieldInfo>();
- BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;
- return t.GetMembers(flags).Where(x => x is FieldInfo || x is PropertyInfo).Concat(GetAllFieldsAndProperties(t.BaseType));
- }
- public static Type FindGenericInterface(Type type, Type tItf)
- {
- foreach (var t in type.GetInterfaces())
- {
-
- if (t.IsGenericType && t.GetGenericTypeDefinition() == tItf)
- return t;
- }
- return null;
- }
- public static bool IsPublic(MemberInfo mi)
- {
-
- var fi = mi as FieldInfo;
- if (fi != null)
- return fi.IsPublic;
-
-
- var pi = mi as PropertyInfo;
- if (pi != null)
- {
- var gm = pi.GetGetMethod(true);
- return (gm != null && gm.IsPublic);
- }
- return false;
- }
- public static Type ResolveInterfaceToClass(Type tItf)
- {
-
- if (tItf.IsGenericType)
- {
- var genDef = tItf.GetGenericTypeDefinition();
-
- if (genDef == typeof(IList<>))
- {
- return typeof(List<>).MakeGenericType(tItf.GetGenericArguments());
- }
-
- if (genDef == typeof(IDictionary<,>) && tItf.GetGenericArguments()[0] == typeof(string))
- {
- return typeof(Dictionary<,>).MakeGenericType(tItf.GetGenericArguments());
- }
- }
-
- if (tItf == typeof(IEnumerable))
- return typeof(List<object>);
-
- if (tItf == typeof(IDictionary))
- return typeof(Dictionary<string, object>);
- return tItf;
- }
- public static long ToUnixMilliseconds(DateTime This)
- {
- return (long)This.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
- }
- public static DateTime FromUnixMilliseconds(long timeStamp)
- {
- return new DateTime(1970, 1, 1).AddMilliseconds(timeStamp);
- }
- public static void DeleteFile(string filename)
- {
- try
- {
- System.IO.File.Delete(filename);
- }
- catch
- {
-
- }
- }
- }
- }
|