public static void Error(string message) { LogRecord .Create() .SetMessage(message) .Error(); }
public byte[] ToByteArray(byte[] airBag) { if (Format == DataFormat.ByteArray) { return(null == Value ? airBag : (byte[])Value); } else { try { return(null == Value ? airBag : (byte[])Convert.ChangeType( Value, typeof(byte[]) )); } catch (Exception ex) { LogRecord .Create() .Add(ex) .Error(); return(airBag); } } }
public static Type GetUnderlyingType <T>() { Type key = typeof(T); if (!key.IsEnum) { throw new NotSupportedException( LogRecord .Create() .SetMessage("Only enum type allowed") .Add("Type", key.FullName) .Error() .Message ); } lock (_UnderlyingTypeLock) { Type value; if (!_UnderlyingTypes.TryGetValue(key, out value)) { value = Enum.GetUnderlyingType(typeof(T)); _UnderlyingTypes.Add(key, value); } return(value); } }
public static void Warn(string message) { LogRecord .Create() .SetMessage(message) .Warn(); }
public static void Debug(string message) { LogRecord .Create() .SetMessage(message) .Debug(); }
public static void Info(string message) { LogRecord .Create() .SetMessage(message) .Info(); }
public static void SetValue( object instance, FieldInfo info, object val) { if (null == val) { val = string.Empty; } try { Type type = info.FieldType; if (type.IsEnum) { // Enum if (null == val) { val = Reflector.GetEnumDefaultValue( info.FieldType ); } val = Enum.Parse(type, val.ToString(), true); } else { val = Parse(type, val); } info.SetValue(instance, val); } catch (Exception ex) { LogRecord.Create().Add(ex).Error(); } }
public static string ToPhysicalPath(string path) { string physicalPath = path; try { if (!Path.IsPathRooted(path)) { if (IsWebApp) { physicalPath = HostingEnvironment.MapPath(path); } else { physicalPath = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, path ); } } return(physicalPath); } catch (Exception ex) { LogRecord .Create() .Add(ex) .Add("path", path) .Add("IsWebApp", IsWebApp) .Error(); throw; } }
public static void Fatal(string message) { LogRecord .Create() .SetMessage(message) .Fatal(); }
public static string CreateFolder(string path) { var physicalPath = ToPhysicalPath(path); try { if (!Directory.Exists(physicalPath)) { var info = Directory.CreateDirectory(physicalPath); if (null == info) { throw new Exception(String.Format( "Create folder failure ({0}).", physicalPath )); } } return(physicalPath); } catch (Exception ex) { LogRecord .Create() .Add(ex) .Add("path", path) .Error(); throw; } }
public static T CreateInstance <T>() { try { return((T)CreateInstance(typeof(T))); } catch (Exception ex) { LogRecord.Create().Add(ex).Error(); return(default(T)); } }
public static object CreateInstance( Type type, Type[] types, object[] objs) { try { var info = type.GetConstructor(types); return(info.Invoke(objs) ?? null); } catch (Exception ex) { LogRecord.Create().Add(ex).Error(); return(null); } }
public static Type GetPrimitiveType(string fullName) { try { return(Type.GetType(fullName)); } catch (Exception ex) { LogRecord .Create() .Add(ex) .Error(); return(default(Type)); } }
public static object CreateInstance(Type type) { try { var info = type.GetConstructor(new Type[0]); if (null != info) { return(info.Invoke(new object[0])); } } catch (Exception ex) { LogRecord.Create().Add(ex).Error(); } return(null); }
public override void DoAfterBuiltinStart() { LogRecord .Create() .SetTitle("RunTime") .Add("UserDomainName", RunTime.UserDomainName) .Add("ComputerName", RunTime.ComputerName) .Add("UserName", RunTime.UserName) .Add("OperatingSystemBits", RunTime.OperatingSystemBits) .Add("ProcessorBits", RunTime.ProcessorBits) .Add("OSVersion", Environment.OSVersion.VersionString) .Add("IsWebApp", RunTime.IsWebApp) .Add("BinFolder", RunTime.BinFolder) .Info(); LogRecord .Create() .SetTitle("Kernel") .Add("AppID", Config.Kernel.AppID) .Add("Debug", Config.Kernel.Debug) .Add( "SkipAssemblyPrefixes", Config.Kernel.SkipAssemblyPrefixes.Join(", ") ) .Add( "OnlyAssemblyPrefixes", Config.Kernel.OnlyAssemblyPrefixes.Join(", ") ) .Info(); LogRecord .Create() .SetTitle("Builtin") .AddRange(Config.Current.BuiltinSection) .Info(); LogRecord .Create() .SetTitle("Plugin") .AddRange(Config.Current.PluginSection) .Info(); LogRecord .Create() .SetTitle("Application") .AddRange(Config.Current.ApplicationSection) .Info(); base.DoAfterBuiltinStart(); }
public static MethodInfo GetMethod( Type type, string methodName) { MethodInfo mi = null; try { MethodInfo[] infos = type.GetMethods( BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); if (null == infos) { return(null); } foreach (MethodInfo info in infos) { if (methodName.Equals( info.Name, StringComparison.OrdinalIgnoreCase)) { if (mi == null) { mi = info; } else { throw new AmbiguousMatchException( "Overload Method Found." ); } } } } catch (AmbiguousMatchException exAm) { LogRecord.Create().Add(exAm).Error(); } catch (Exception ex) { LogRecord.Create().Add(ex).Error(); } return(mi); }
public static PropertyInfo GetProperty( Type type, string propertyName) { try { var infos = type.GetProperties(); if (null == infos) { return(null); } foreach (PropertyInfo info in infos) { if (propertyName.Equals(info.Name)) { return(info); } } } catch (Exception ex) { LogRecord.Create().Add(ex).Error(); } return(null); }
public static object Invoke( string name, object obj, params object[] parameters) { try { var info = GetMethod(obj.GetType(), name); if (null == info) { throw new Exception( new[] { obj.GetType().FullName, " type have not such method called ", name, }.Join() ); } return(info.Invoke(obj, parameters)); } catch (Exception ex) { LogRecord.Create().Add(ex).Error(); } return(null); }
public static FieldInfo GetField(Type type, string fieldName) { try { // USER_NAME >> username string newFieldName = fieldName.Replace( Symbol.UnderScore, string.Empty ).ToLower(); fieldName = fieldName.ToLower(); var infos = type.GetFields(); if (null == infos) { return(null); } FieldInfo maybeInfo = null; foreach (FieldInfo info in infos) { if (fieldName.Equals(info.Name.ToLower())) { return(info); } if (newFieldName.Equals(info.Name.ToLower())) { maybeInfo = info; } } return(maybeInfo); } catch (Exception ex) { LogRecord.Create().Add(ex).Error(); return(null); } }
internal static object NullToEmptyAndTrim( Type objType, object objValue, PropertyInfo propertyInfo, object propertyValue) { try { // 1. objType is null if (null == objType) { return(null); } // 2. objValue is null if (null == objValue) { // 2.1 ValueType if (objType.IsValueType) { return(objValue); } // 2.2 String if (objType.Equals(typeof(string))) { return(string.Empty); } // 2.3 CreateInstance objValue = Reflector.CreateInstance(objType); objValue = NullToEmptyAndTrim( objType, objValue, null, null ); return(objValue); } // 3. propertyInfo is null if (null == propertyInfo) { // 3.1 ValueType if (objType.IsValueType) { return(objValue); } // 3.2 String if (objType.Equals(typeof(string))) { string v = objValue as string; return(v.Trim()); } // 3.3 Properties foreach (var pi in objType.GetProperties()) { if (!pi.CanRead || !pi.CanWrite) { continue; } object v = Reflector.GetValue(pi, objValue); NullToEmptyAndTrim(objType, objValue, pi, v); } return(objValue); } var propertyType = propertyInfo.PropertyType; // 4. propertyValue is null if (null == propertyValue) { // 4.1 ValueType if (propertyType.IsValueType) { return(propertyValue); } // 4.2 String if (propertyType.Equals(typeof(string))) { Reflector.SetValue( objValue, propertyInfo, string.Empty ); return(objValue); } // 4.3 IEnumerable if (propertyType.IsDerived <IEnumerable>()) { var v = typeof(List <>); var genericArgs = propertyType.GetGenericArguments(); if (null != genericArgs) { var concreteType = v.MakeGenericType(genericArgs); var newList = Activator.CreateInstance(concreteType); Reflector.SetValue(objValue, propertyInfo, newList); } return(objValue); } // 4.4 Properties object x = Reflector.CreateInstance(propertyType); foreach (var pi in propertyType.GetProperties()) { if (!pi.CanRead || !pi.CanWrite) { continue; } object v = pi.GetValue(x, new object[0]); x = NullToEmptyAndTrim(propertyType, x, pi, v); } Reflector.SetValue(objValue, propertyInfo, x); return(objValue); } // 5. all is not null // 5.1 ValueType if (propertyType.IsValueType) { return(objValue); } // 5.2 String if (propertyType.Equals(typeof(string))) { string v = propertyValue as string; propertyValue = v.Trim(); Reflector.SetValue(objValue, propertyInfo, v); return(objValue); } // 5.3 IEnumerable if (propertyType.IsDerived <IEnumerable>()) { var v = typeof(List <>); var genericArgs = propertyType.GetGenericArguments(); if (null != genericArgs) { var concreteType = v.MakeGenericType(genericArgs); var newList = Activator.CreateInstance(concreteType); var list = propertyValue as IEnumerable; foreach (var one in list) { object o = one; object o2 = NullToEmptyAndTrim( genericArgs[0], o, null, null ); Reflector.Invoke("Add", newList, new { o2 }); } Reflector.SetValue(objValue, propertyInfo, newList); } return(objValue); } // 5.4 Properties object y = propertyValue; foreach (var pi in propertyType.GetProperties()) { if (!pi.CanRead || !pi.CanWrite) { continue; } object v = pi.GetValue(propertyValue, new object[0]); y = NullToEmptyAndTrim(propertyType, y, pi, v); } Reflector.SetValue(objValue, propertyInfo, y); } catch (Exception ex) { LogRecord.Create().Add(ex).Error(); } return(objValue); }
public static EnumRef Get(Type type) { if (!type.IsEnum) { throw new NotSupportedException( LogRecord .Create() .SetMessage("Only enum type allowed") .Add("Type", type.FullName) .Error() .Message ); } EnumRef ef; if (_EnumRefs.TryGetValue(type.FullName, out ef)) { return(ef); } lock (_EnumRefLock) { if (_EnumRefs.TryGetValue(type.FullName, out ef)) { return(ef); } ef = new EnumRef() { DefaultValue = type.GetDefaultValue().ToStringX(), Description = type.GetDescription().AirBag(type.Name), Category = type.GetCategory(), FullName = type.FullName, Type = type, }; var infos = type.GetFields().ToList(); string[] names = Enum.GetNames(type); Array values = Enum.GetValues(type); for (int i = 0; i < names.Length; i++) { var info = infos.FirstOrDefault(x => x.Name == names[i] ); if (null == info) { throw new NotSupportedException( LogRecord .Create() .SetMessage("Enum cache error") .Add("Type", type.FullName) .Error() .Message ); } var ei = new EnumItem() { Category = info.GetCategory(), Description = info.GetDescription().AirBag(info.Name), Type = type, Name = info.Name, Value = (int)values.GetValue(i), }; ef.Items.Add(ei); } _EnumRefs.Add(type.FullName, ef); return(ef); } }