Beispiel #1
0
 public static object ReplaceAllMembers(object from, Type ofType, MemberInfo name, ObjectMemberReplacer replacerFunc, HashSet<object> exceptFor, HashSet<MissingItemInfo> missing)
 {
     if (from == null) return from;
     var fromType = from.GetType();
     if (fromType == ofType)
     {
         var oo = replacerFunc(name, from, missing);
         return oo;
     }
     Type fromType0 = fromType;
     while (fromType0.IsArray) fromType0 = fromType0.GetElementType();
     if (fromType0 == typeof(string) || fromType0 == typeof(byte[]) || typeof(IConvertible).IsAssignableFrom(fromType0))
         return from;
     if (from is IDictionary)
     {
         var ic = from as IDictionary;
         foreach (var k0 in LockInfo.CopyOf<object>(ic.Keys))
         {
             var k = k0;
             var ko = ReplaceAllMembers(k, ofType, k == null ? null : k.GetType(), replacerFunc, exceptFor, missing);                   
             object o = ic[k];
             var oo = ReplaceAllMembers(o, ofType, o == null ? null : o.GetType(), replacerFunc, exceptFor, missing);
             bool keyChanged = false;
             if (!ReferenceEquals(k, ko))
             {
                 keyChanged = true;
                 ic.Remove(k);
                 k = ko;
             }
             if (ReferenceEquals(oo, o) && !keyChanged) continue;
             ic[k] = oo;
         }
         return from;
     }
     if (from is IList)
     {
         var ic = from as IList;
         lock (ic) for (int i = 0; i < ic.Count; i++)
         {
             object o = ic[i];
             var oo = ReplaceAllMembers(o, ofType, name, replacerFunc, exceptFor, missing);
             if (ReferenceEquals(oo, o)) continue;
             ic[i] = oo;
         }
         return from;
     }
     if (exceptFor.Contains(from)) return from;
     exceptFor.Add(from);
     const BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
     foreach (var info in fromType.GetFields(bf))
     {
         object o = info.GetValue(from);
         var oo = ReplaceAllMembers(o, ofType, info, replacerFunc, exceptFor, missing);
         if (ReferenceEquals(oo, o)) continue;
         info.SetValue(from, oo);
     }
     foreach (var info in fromType.GetProperties(bf))
     {
         object o = info.GetValue(from, null);
         var oo = ReplaceAllMembers(o, ofType, info, replacerFunc, exceptFor, missing);
         if (ReferenceEquals(oo, o)) continue;
         info.SetValue(from, oo, null);
     }
     return from;
 }
Beispiel #2
0
        public static object ReplaceAllMembers(object from, Type ofType, MemberInfo name, ObjectMemberReplacer replacerFunc, HashSet <object> exceptFor, HashSet <MissingItemInfo> missing)
        {
            if (from == null)
            {
                return(from);
            }
            var fromType = from.GetType();

            if (fromType == ofType)
            {
                var oo = replacerFunc(name, from, missing);
                return(oo);
            }
            Type fromType0 = fromType;

            while (fromType0.IsArray)
            {
                fromType0 = fromType0.GetElementType();
            }
            if (fromType0 == typeof(string) || fromType0 == typeof(byte[]) || typeof(IConvertible).IsAssignableFrom(fromType0))
            {
                return(from);
            }
            if (from is IDictionary)
            {
                var ic = from as IDictionary;
                foreach (var k0 in LockInfo.CopyOf <object>(ic.Keys))
                {
                    var    k          = k0;
                    var    ko         = ReplaceAllMembers(k, ofType, k == null ? null : k.GetType(), replacerFunc, exceptFor, missing);
                    object o          = ic[k];
                    var    oo         = ReplaceAllMembers(o, ofType, o == null ? null : o.GetType(), replacerFunc, exceptFor, missing);
                    bool   keyChanged = false;
                    if (!ReferenceEquals(k, ko))
                    {
                        keyChanged = true;
                        ic.Remove(k);
                        k = ko;
                    }
                    if (ReferenceEquals(oo, o) && !keyChanged)
                    {
                        continue;
                    }
                    ic[k] = oo;
                }
                return(from);
            }
            if (from is IList)
            {
                var ic = from as IList;
                lock (ic) for (int i = 0; i < ic.Count; i++)
                    {
                        object o  = ic[i];
                        var    oo = ReplaceAllMembers(o, ofType, name, replacerFunc, exceptFor, missing);
                        if (ReferenceEquals(oo, o))
                        {
                            continue;
                        }
                        ic[i] = oo;
                    }
                return(from);
            }
            if (exceptFor.Contains(from))
            {
                return(from);
            }
            exceptFor.Add(from);
            const BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

            foreach (var info in fromType.GetFields(bf))
            {
                object o  = info.GetValue(from);
                var    oo = ReplaceAllMembers(o, ofType, info, replacerFunc, exceptFor, missing);
                if (ReferenceEquals(oo, o))
                {
                    continue;
                }
                info.SetValue(from, oo);
            }
            foreach (var info in fromType.GetProperties(bf))
            {
                object o  = info.GetValue(from, null);
                var    oo = ReplaceAllMembers(o, ofType, info, replacerFunc, exceptFor, missing);
                if (ReferenceEquals(oo, o))
                {
                    continue;
                }
                info.SetValue(from, oo, null);
            }
            return(from);
        }
Beispiel #3
0
 public static object ReplaceAllMembers(object from, Type ofType, ObjectMemberReplacer replacerFunc, HashSet<MissingItemInfo> missing)
 {
     return ReplaceAllMembers(from, ofType, ofType, replacerFunc, new HashSet<object>(), missing);
 }
Beispiel #4
0
 public static object ReplaceAllMembers(object from, Type ofType, ObjectMemberReplacer replacerFunc, HashSet <MissingItemInfo> missing)
 {
     return(ReplaceAllMembers(from, ofType, ofType, replacerFunc, new HashSet <object>(), missing));
 }