diff --git a/Lidgren.Network/NetIncomingMessage.Read.cs b/Lidgren.Network/NetIncomingMessage.Read.cs
index d58dcab..bc7a0de 100644
--- a/Lidgren.Network/NetIncomingMessage.Read.cs
+++ b/Lidgren.Network/NetIncomingMessage.Read.cs
@@ -474,7 +474,7 @@ namespace Lidgren.Network
}
///
- /// Reads all public and private declared instance fields of the object in declaration order using reflection
+ /// Reads all public and private declared instance fields of the object in alphabetical order using reflection
///
public void ReadAllFields(object target)
{
@@ -482,7 +482,7 @@ namespace Lidgren.Network
}
///
- /// Reads all fields with the specified binding of the object in declaration order using reflection
+ /// Reads all fields with the specified binding of the object in alphabetical order using reflection
///
public void ReadAllFields(object target, BindingFlags flags)
{
@@ -491,6 +491,8 @@ namespace Lidgren.Network
Type tp = target.GetType();
FieldInfo[] fields = tp.GetFields(flags);
+ SortMembersList(fields);
+
foreach (FieldInfo fi in fields)
{
object value;
@@ -509,7 +511,7 @@ namespace Lidgren.Network
}
///
- /// Reads all public and private declared instance fields of the object in declaration order using reflection
+ /// Reads all public and private declared instance fields of the object in alphabetical order using reflection
///
public void ReadAllProperties(object target)
{
@@ -517,7 +519,7 @@ namespace Lidgren.Network
}
///
- /// Reads all fields with the specified binding of the object in declaration order using reflection
+ /// Reads all fields with the specified binding of the object in alphabetical order using reflection
///
public void ReadAllProperties(object target, BindingFlags flags)
{
@@ -526,6 +528,7 @@ namespace Lidgren.Network
Type tp = target.GetType();
PropertyInfo[] fields = tp.GetProperties(flags);
+ SortMembersList(fields);
foreach (PropertyInfo fi in fields)
{
object value;
@@ -543,6 +546,44 @@ namespace Lidgren.Network
}
}
}
+
+ // shell sort
+ internal static void SortMembersList(MemberInfo[] list)
+ {
+ int h;
+ int j;
+ MemberInfo tmp;
+
+ h = 1;
+ while (h * 3 + 1 <= list.Length)
+ h = 3 * h + 1;
+
+ while (h > 0)
+ {
+ for (int i = h - 1; i < list.Length; i++)
+ {
+ tmp = list[i];
+ j = i;
+ while (true)
+ {
+ if (j >= h)
+ {
+ if (list[j - h].Name.CompareTo(tmp.Name) > 0)
+ {
+ list[j] = list[j - h];
+ j -= h;
+ }
+ else
+ break;
+ }
+ else
+ break;
+ }
+
+ list[j] = tmp;
+ }
+ h /= 3;
+ }
+ }
}
}
-
diff --git a/Lidgren.Network/NetOutgoingMessage.Write.cs b/Lidgren.Network/NetOutgoingMessage.Write.cs
index b084937..474a8cb 100644
--- a/Lidgren.Network/NetOutgoingMessage.Write.cs
+++ b/Lidgren.Network/NetOutgoingMessage.Write.cs
@@ -537,7 +537,7 @@ namespace Lidgren.Network
}
///
- /// Writes all public and private declared instance fields of the object in declaration order using reflection
+ /// Writes all public and private declared instance fields of the object in alphabetical order using reflection
///
public void WriteAllFields(object ob)
{
@@ -545,7 +545,7 @@ namespace Lidgren.Network
}
///
- /// Writes all fields with specified binding in declaration order using reflection
+ /// Writes all fields with specified binding in alphabetical order using reflection
///
public void WriteAllFields(object ob, BindingFlags flags)
{
@@ -554,6 +554,8 @@ namespace Lidgren.Network
Type tp = ob.GetType();
FieldInfo[] fields = tp.GetFields(flags);
+ NetIncomingMessage.SortMembersList(fields);
+
foreach (FieldInfo fi in fields)
{
object value = fi.GetValue(ob);
@@ -568,7 +570,7 @@ namespace Lidgren.Network
}
///
- /// Writes all public and private declared instance properties of the object in declaration order using reflection
+ /// Writes all public and private declared instance properties of the object in alphabetical order using reflection
///
public void WriteAllProperties(object ob)
{
@@ -576,7 +578,7 @@ namespace Lidgren.Network
}
///
- /// Writes all properties with specified binding in declaration order using reflection
+ /// Writes all properties with specified binding in alphabetical order using reflection
///
public void WriteAllProperties(object ob, BindingFlags flags)
{
@@ -585,6 +587,8 @@ namespace Lidgren.Network
Type tp = ob.GetType();
PropertyInfo[] fields = tp.GetProperties(flags);
+ NetIncomingMessage.SortMembersList(fields);
+
foreach (PropertyInfo fi in fields)
{
MethodInfo getMethod = fi.GetGetMethod((flags & BindingFlags.NonPublic) == BindingFlags.NonPublic);
diff --git a/UnitTests/ReadWriteTests.cs b/UnitTests/ReadWriteTests.cs
index 4e28b7b..dd02824 100644
--- a/UnitTests/ReadWriteTests.cs
+++ b/UnitTests/ReadWriteTests.cs
@@ -70,6 +70,35 @@ namespace UnitTests
if (msg.LengthBits != tmp.LengthBits * 2)
throw new NetException("NetOutgoingMessage.Write(NetOutgoingMessage) failed!");
+
+ tmp = peer.CreateMessage();
+
+ Test test = new Test();
+ test.Number = 42;
+ test.Name = "Hallon";
+ test.Age = 8.2f;
+
+ tmp.WriteAllFields(test);
+
+ data = tmp.PeekDataBuffer();
+
+ inc = (NetIncomingMessage)Activator.CreateInstance(typeof(NetIncomingMessage), true);
+ typeof(NetIncomingMessage).GetField("m_data", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(inc, data);
+ typeof(NetIncomingMessage).GetField("m_bitLength", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(inc, tmp.LengthBits);
+
+ Test readTest = new Test();
+ inc.ReadAllFields(readTest);
+
+ NetException.Assert(readTest.Number == 42);
+ NetException.Assert(readTest.Name == "Hallon");
+ NetException.Assert(readTest.Age == 8.2f);
}
}
+
+ public class Test
+ {
+ public int Number;
+ public float Age;
+ public string Name;
+ }
}