ArtemisComm.Utility.SetRawData C# (CSharp) Method

SetRawData() public static method

public static SetRawData ( this package ) : MemoryStream
package this
return System.IO.MemoryStream
        public static MemoryStream SetRawData(this IPackage package)
        {
            MemoryStream stream = null;
            MemoryStream returnStream = null;
            try
            {
                stream = new MemoryStream();
                if (package != null)
                {
                    foreach (PropertyInfo pi in package.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                    {
                        ArtemisTypeAttribute artyAttrib = null;
                        bool exclude = false;
                        foreach (object a in pi.GetCustomAttributes(false))
                        {
                            if (a.GetType() == typeof(ArtemisExcludedAttribute))
                            {
                                exclude = true;
                            }
                            if (artyAttrib == null)
                            {
                                artyAttrib = a as ArtemisTypeAttribute;
                            }
                        }
                        if (!exclude)
                        {
                            Type castType = pi.PropertyType;
                            Type ArtyType = castType;
                            if (artyAttrib != null)
                            {
                                ArtyType = artyAttrib.ArtemisProtocolType;
                            }
                            if (ArtyType.IsEnum)
                            {
                                ArtyType = ArtyType.GetEnumUnderlyingType();
                            }
                            if (castType == typeof(IPackage))
                            {
                                IPackage pck = (IPackage)pi.GetValue(package, null);
                                pck.GetRawData().WriteTo(stream);
                            }
                            else
                            {
                                if (castType.IsEnum)
                                {
                                    castType = castType.GetEnumUnderlyingType();
                                }
                                
                                object item = null;
                                if (castType == typeof(byte))
                                {
                                    item = (byte)pi.GetValue(package, null);
                                }
                                else if (castType == typeof(bool))
                                {
                                    item = (bool)pi.GetValue(package, null);
                                }
                                else if (castType == typeof(short))
                                {
                                    item = (short)pi.GetValue(package, null);
                                }
                                else if (castType == typeof(int))
                                {
                                    item = (int)pi.GetValue(package, null);
                                }
                                else if (castType == typeof(float))
                                {
                                    item = (float)pi.GetValue(package, null);
                                }
                                else if (castType == typeof(long))
                                {
                                    item = (long)pi.GetValue(package, null);
                                }
                                else if (castType == typeof(ArtemisString))
                                {
                                    item = (ArtemisString)pi.GetValue(package, null);
                                }
                                else if (castType == typeof(string))
                                {
                                    item = (string)pi.GetValue(package, null);
                                }
                                WriteToStream(ArtyType, item, stream);
                            }
                        }
                    }
                }
                returnStream = stream;
                stream = null;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
            return returnStream;
        }