示例#1
0
        internal static void CopyTo <T>(ITrinitySetting <T> source, ITrinitySetting <T> destination) where T : class, ITrinitySetting <T>
        {
            try
            {
                Type type = typeof(T);
                Logger.Log(TrinityLogLevel.Verbose, LogCategory.Configuration, "Starting CopyTo Object {0}", type.Name);
                foreach (PropertyInfo prop in type.GetProperties(BindingFlags.SetProperty | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
                {
                    try
                    {
                        if (Attribute.IsDefined(prop, typeof(IgnoreDataMemberAttribute)))
                        {
                            continue;
                        }

                        if (prop.PropertyType.IsValueType || prop.PropertyType == typeof(string))
                        {
                            prop.SetValue(destination, prop.GetValue(source, null), null);
                        }
                        else
                        {
                            object destinationValue = prop.GetValue(destination, null);
                            object sourceValue      = prop.GetValue(source, null);

                            if (sourceValue == null || destinationValue == null)
                            {
                                continue;
                            }

                            MethodBase method = prop.PropertyType.GetMethod("CopyTo", new[] { prop.PropertyType });
                            if (method != null)
                            {
                                method.Invoke(sourceValue, new[] { destinationValue });
                            }
                            //else if (sourceValue != null && destinationValue != null)
                            //{
                            //    MethodBase method = prop.PropertyType.GetMethod("Clone", null);
                            //    if (method != null)
                            //    {
                            //        prop.SetValue(destination, method.Invoke(sourceValue, null), null);
                            //    }
                            //}
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(TrinityLogLevel.Error, LogCategory.UserInformation, "Error while CopyTo Setting {0} : {1} Property: {2}", typeof(T).Name, ex.Message, prop.Name);
                    }
                }
                Logger.Log(TrinityLogLevel.Verbose, LogCategory.Configuration, "End CopyTo Object {0}", type.Name);
            }
            catch (Exception ex)
            {
                Logger.Log(TrinityLogLevel.Error, LogCategory.UserInformation, "Error while CopyTo Setting {1} : {0}", ex.Message, typeof(T).Name);
            }
        }
示例#2
0
 internal static void LoadDefaults <T>(ITrinitySetting <T> setting) where T : ITrinitySetting <T>
 {
     foreach (var p in setting.GetType().GetProperties())
     {
         foreach (var dv in p.GetCustomAttributes(true).OfType <DefaultValueAttribute>())
         {
             p.SetValue(setting, dv.Value);
         }
     }
 }
示例#3
0
        internal static void CopyTo <T>(ITrinitySetting <T> source, ITrinitySetting <T> destination, IEnumerable <string> ignorePropertyNames = null) where T : class, ITrinitySetting <T>
        {
            try
            {
                Type type = typeof(T);
                Core.Logger.Verbose(LogCategory.Configuration, "Starting CopyTo Object {0}", type.Name);
                foreach (PropertyInfo prop in type.GetProperties(BindingFlags.SetProperty | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
                {
                    try
                    {
                        if (Attribute.IsDefined(prop, typeof(IgnoreDataMemberAttribute)))
                        {
                            continue;
                        }

                        if (prop.PropertyType.IsValueType || prop.PropertyType == typeof(string))
                        {
                            prop.SetValue(destination, prop.GetValue(source, null), null);
                        }
                        else
                        {
                            object destinationValue = prop.GetValue(destination, null);
                            object sourceValue      = prop.GetValue(source, null);

                            if (sourceValue == null || destinationValue == null)
                            {
                                continue;
                            }

                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                MethodBase method = prop.PropertyType.GetMethod("CopyTo", new[] { prop.PropertyType });
                                method?.Invoke(sourceValue, new[] { destinationValue });
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        Core.Logger.Error(LogCategory.None, "Error while CopyTo Setting {0} : {1} Property: {2} {3}", typeof(T).Name, ex.Message, prop.Name, ex);
                    }
                }
                Core.Logger.Verbose(LogCategory.Configuration, "End CopyTo Object {0}", type.Name);
            }
            catch (Exception ex)
            {
                Core.Logger.Error(LogCategory.None, "Error while CopyTo Setting {1} : {0} {2}", ex.Message, typeof(T).Name, ex);
            }
        }
示例#4
0
        internal static void Reset <T>(ITrinitySetting <T> setting) where T : class, ITrinitySetting <T>
        {
            try
            {
                Type type = typeof(T);
                Core.Logger.Verbose(LogCategory.Configuration, "Starting Reset Object {0}", type.Name);
                foreach (PropertyInfo prop in type.GetProperties(BindingFlags.SetProperty | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
                {
                    if (Attribute.IsDefined(prop, typeof(IgnoreDataMemberAttribute)))
                    {
                        continue;
                    }

                    if (prop.PropertyType.IsValueType || prop.PropertyType == typeof(string))
                    {
                        Attribute[] decorators = prop.GetCustomAttributes(typeof(DefaultValueAttribute), true) as Attribute[];
                        if (decorators != null && decorators.Length > 0)
                        {
                            DefaultValueAttribute defaultValue = decorators[0] as DefaultValueAttribute;
                            if (defaultValue != null)
                            {
                                prop.SetValue(setting, defaultValue.Value, null);
                            }
                        }
                    }
                    else
                    {
                        object value = prop.GetValue(setting, null);
                        if (value != null)
                        {
                            MethodBase method = prop.PropertyType.GetMethod("Reset");
                            if (method != null)
                            {
                                method.Invoke(value, new object[] { });
                            }
                        }
                    }
                }

                OnReset();

                Core.Logger.Verbose(LogCategory.Configuration, "End Reset Object {0}", type.Name);
            }
            catch (Exception ex)
            {
                Core.Logger.Error(LogCategory.None, "Error while Reset Setting {1} : {0}", ex.Message, typeof(T).Name);
            }
        }
示例#5
0
 internal static void CopyTo <T>(ITrinitySetting <T> source, ITrinitySetting <T> destination) where T : class, ITrinitySetting <T>
 {
     try
     {
         Type type = typeof(T);
         DbHelper.Log(TrinityLogLevel.Verbose, LogCategory.Configuration, "Starting CopyTo Object {0}", type.Name);
         foreach (PropertyInfo prop in type.GetProperties(BindingFlags.SetProperty | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
         {
             if (prop.PropertyType.IsValueType || prop.PropertyType == typeof(string))
             {
                 prop.SetValue(destination, prop.GetValue(source, null), null);
             }
             else
             {
                 object destinationValue = prop.GetValue(destination, null);
                 object sourceValue      = prop.GetValue(source, null);
                 if (destinationValue != null)
                 {
                     MethodBase method = prop.PropertyType.GetMethod("CopyTo", new[] { prop.PropertyType });
                     if (method != null)
                     {
                         method.Invoke(sourceValue, new[] { destinationValue });
                     }
                 }
                 else if (destinationValue != null)
                 {
                     MethodBase method = prop.PropertyType.GetMethod("Clone", null);
                     if (method != null)
                     {
                         prop.SetValue(destination, method.Invoke(sourceValue, null), null);
                     }
                 }
             }
         }
         DbHelper.Log(TrinityLogLevel.Verbose, LogCategory.Configuration, "End CopyTo Object {0}", type.Name);
     }
     catch (Exception ex)
     {
         DbHelper.Log(TrinityLogLevel.Error, LogCategory.UserInformation, "Error while CopyTo Setting {1} : {0}", ex.Message, typeof(T).Name);
     }
 }
示例#6
0
 internal static T Clone <T>(ITrinitySetting <T> setting) where T : class, ITrinitySetting <T>
 {
     try
     {
         Core.Logger.Verbose(LogCategory.Configuration, "Starting Clone Object {0}", typeof(T).Name);
         using (MemoryStream ms = new MemoryStream())
         {
             DataContractSerializer serializer = new DataContractSerializer(typeof(T));
             serializer.WriteObject(ms, setting);
             return((T)serializer.ReadObject(ms));
         }
     }
     catch (Exception ex)
     {
         Core.Logger.Error(LogCategory.None, "Error while Clone Setting {1} : {0}", ex.Message, typeof(T).Name);
         return(null);
     }
     finally
     {
         Core.Logger.Verbose(LogCategory.Configuration, "End Clone Object {0}", typeof(T).Name);
     }
 }
示例#7
0
 internal static T Clone <T>(ITrinitySetting <T> setting) where T : class, ITrinitySetting <T>
 {
     try
     {
         Logger.Log(TrinityLogLevel.Verbose, LogCategory.Configuration, "Starting Clone Object {0}", typeof(T).Name);
         using (MemoryStream ms = new MemoryStream())
         {
             DataContractSerializer serializer = new DataContractSerializer(typeof(T));
             serializer.WriteObject(ms, setting);
             //ms.Seek(0, SeekOrigin.Begin);
             return((T)serializer.ReadObject(ms));
         }
     }
     catch (Exception ex)
     {
         Logger.Log(TrinityLogLevel.Error, LogCategory.UserInformation, "Error while Clone Setting {1} : {0}", ex.Message, typeof(T).Name);
         return(null);
     }
     finally
     {
         Logger.Log(TrinityLogLevel.Verbose, LogCategory.Configuration, "End Clone Object {0}", typeof(T).Name);
     }
 }