コード例 #1
0
        public static void AssertAreTheSame(object orig, object decoded)
        {
            PropertyInfo[] props = orig.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.GetCustomAttribute <NoSerializeAttribute>() != null)
                {
                    continue;
                }

                object origVal    = prop.GetValue(orig);
                object decodedVal = prop.GetValue(decoded);

                SerializableAttributeBase attr = prop.GetCustomAttribute <SerializableAttributeBase>();
                if (attr != null)
                {
                    Assert.True(attr.AreEqual(origVal, decodedVal),
                                string.Format("{0}.{1} does not match. Orig:{2}, Decoded:{3}", orig.GetType().Name, prop.Name, origVal, decodedVal));
                    continue;
                }

                Assert.True(Equals(origVal, decodedVal),
                            string.Format("{0}.{1} does not match. Orig:{2}, Decoded:{3}", orig.GetType().Name, prop.Name, origVal, decodedVal));
            }
        }
コード例 #2
0
        static string GenerateCommandHash(Type t)
        {
            var str = new StringBuilder();

            var cmdAttr = t.GetCustomAttribute <CommandNameAttribute>();

            str.Append($"{cmdAttr.Name},{cmdAttr.MinimumVersion},{cmdAttr.Length}:");

            foreach (PropertyInfo prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                var serAttr = prop.GetCustomAttribute <SerializeAttribute>();

                SerializableAttributeBase attr = prop.GetCustomAttributes().OfType <SerializableAttributeBase>().FirstOrDefault();
                if (attr != null)
                {
                    str.Append($"{prop.Name},{serAttr?.StartByte ?? 0},{attr.GetType().FullName},{attr.Size},{attr.GetHashString()}.");
                    continue;
                }

                if (serAttr == null)
                {
                    continue;
                }

                Assert.True(false, string.Format("Missing generator attribute for property: {0}", prop.Name));
            }

            using var sha = SHA256.Create();
            var hashbytes = sha.ComputeHash(Encoding.UTF8.GetBytes(str.ToString()));

            return(BitConverter.ToString(hashbytes));
        }
コード例 #3
0
        public static bool AreTheSame(object orig, object decoded, bool allowedNoProps)
        {
            int comparedProps = 0;

            PropertyInfo[] props = orig.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.GetCustomAttribute <NoSerializeAttribute>() != null)
                {
                    continue;
                }

                comparedProps++;

                object origVal    = prop.GetValue(orig);
                object decodedVal = prop.GetValue(decoded);

                SerializableAttributeBase attr = prop.GetCustomAttribute <SerializableAttributeBase>();
                if (attr != null)
                {
                    if (!attr.AreEqual(origVal, decodedVal))
                    {
                        return(false);
                    }

                    continue;
                }

                Type t = prop.PropertyType;
                if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Dictionary <,>))
                {
                    dynamic origDict    = Convert.ChangeType(origVal, t);
                    dynamic decodedDict = Convert.ChangeType(decodedVal, t);

                    var origKeys = new List <object>();
                    foreach (dynamic v in origDict)
                    {
                        origKeys.Add(v.Key);
                    }

                    var decodedKeys = new List <object>();
                    foreach (dynamic v in decodedDict)
                    {
                        decodedKeys.Add(v.Key);
                    }

                    origKeys.Sort();
                    decodedKeys.Sort();
                    if (!origKeys.SequenceEqual(decodedKeys))
                    {
                        return(false);
                    }

                    var origVals    = new List <object>();
                    var decodedVals = new List <object>();

                    foreach (dynamic k in origKeys)
                    {
                        origVals.Add(origDict[k]);
                        decodedVals.Add(decodedDict[k]);
                    }

                    if (!origVals.SequenceEqual(decodedVals))
                    {
                        return(false);
                    }

                    continue;
                }

                if (origVal is IEnumerable enumVal)
                {
                    List <object> origList    = enumVal.OfType <object>().ToList();
                    List <object> decodedList = ((IEnumerable)decodedVal).OfType <object>().ToList();
                    if (!origList.SequenceEqual(decodedList))
                    {
                        return(false);
                    }

                    continue;
                }

                if (!Equals(origVal, decodedVal))
                {
                    return(false);
                }
            }

            return(allowedNoProps || comparedProps > 0);
        }