public static int LoadProperty(object baseObject, MemoryStream stream, PropertyInfo prop, Collection<Exception> errors)
{
int retVal = 0;
if (stream != null)
{
retVal = Convert.ToInt32(stream.Position);
try
{
bool exclude = false;
ArtemisTypeAttribute artyType = null;
if (prop != null)
{
foreach (object a in prop.GetCustomAttributes(false))
{
if (a.GetType() == typeof(ArtemisExcludedAttribute))
{
exclude = true;
}
if (artyType == null)
{
artyType = a as ArtemisTypeAttribute;
}
}
if (!exclude)
{
Type CastType = prop.PropertyType;
if (artyType != null)
{
CastType = artyType.ArtemisProtocolType;
}
Type toType = prop.PropertyType;
if (prop.PropertyType == typeof(IPackage))
{
object[] parms = { stream, retVal };
Type[] constructorSignature = { typeof(MemoryStream), typeof(int) };
ConstructorInfo constructor = prop.PropertyType.GetConstructor(constructorSignature);
object obj = constructor.Invoke(parms);
prop.SetValue(baseObject, obj, null);
IPackage pck = obj as IPackage;
if (pck != null)
{
retVal = Convert.ToInt32(pck.GetRawData().Length);
}
}
else
{
if (prop.PropertyType.IsEnum)
{
toType = prop.PropertyType.GetEnumUnderlyingType();
}
object intermediateObject = null;
if (stream != null)
{
KeyValuePair<int, object> item = GetIntermediateType(CastType, retVal, stream);
intermediateObject = item.Value;
retVal = item.Key; //length.
if (CastType == typeof(string))
{
if (retVal < stream.Length)
{
byte[] buffer = new byte[stream.Length - stream.Position];
stream.Read(buffer, 0, Convert.ToInt32(stream.Length - stream.Position));
//WelcomePacket
string itemString = System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, buffer.Length);
prop.SetValue(baseObject, itemString, null);
retVal = itemString.Length;
}
}
SetProperty(toType, baseObject, intermediateObject, prop);
}
}
}
}
}
catch (Exception e)
{
if (errors != null)
{
errors.Add(e);
}
}
}
return retVal;
}