in shims/amqpnetlite/src/amqp_types_test/Sender/Sender.cs [223:401]
public void Encode()
{
if (baseValue is Array)
{
// List
if (! String.Equals(baseType, "list", StringComparison.OrdinalIgnoreCase))
{
throw new ApplicationException(String.Format(
"Sender asked to encode a {0} but received a list: {1}", baseType, baseValue.ToString()));
}
valueDirect = new Amqp.Types.List();
foreach (object item in (Array)baseValue)
{
MessageValue itemValue = MessageValue.CreateAutoType(item);
itemValue.Encode();
((Amqp.Types.List)valueDirect).Add(itemValue.ToObject());
}
}
if (baseValue is ArrayList)
{
// List
if (! String.Equals(baseType, "list", StringComparison.OrdinalIgnoreCase))
{
throw new ApplicationException(String.Format(
"Sender asked to encode a {0} but received a list: {1}", baseType, baseValue.ToString()));
}
valueDirect = new Amqp.Types.List();
foreach (object item in (ArrayList)baseValue)
{
MessageValue itemValue = MessageValue.CreateAutoType(item);
itemValue.Encode();
((Amqp.Types.List)valueDirect).Add(itemValue.ToObject());
}
}
else if (baseValue is Dictionary<string, object>)
{
// Map
if (!String.Equals(baseType, "map", StringComparison.OrdinalIgnoreCase))
{
throw new ApplicationException(String.Format(
"Sender asked to encode a {0} but received a map: {1}", baseType, baseValue.ToString()));
}
valueDirect = new Amqp.Types.Map();
Dictionary<string, object> myDict = new Dictionary<string, object>();
myDict = (Dictionary<string, object>)baseValue;
foreach (var key in myDict.Keys)
{
MessageValue itemValue = MessageValue.CreateAutoType(myDict[key]);
((Amqp.Types.Map)valueDirect)[key] = itemValue.ToObject();
}
}
else if (baseValue is String)
{
string value;
UInt64 valueUInt64;
Int64 valueInt64;
switch (baseType)
{
case "null":
valueDirect = null;
break;
case "boolean":
value = (string)baseValue;
bool mybool = String.Equals(value, "true", StringComparison.OrdinalIgnoreCase);
valueDirect = mybool;
break;
case "ubyte":
value = (string)baseValue;
valueUInt64 = EncodeUInt(value);
Byte mybyte = (Byte)(valueUInt64 & 0xff);
valueDirect = mybyte;
break;
case "ushort":
value = (string)baseValue;
valueUInt64 = EncodeUInt(value);
UInt16 myuint16 = (UInt16)(valueUInt64 & 0xffff);
valueDirect = myuint16;
break;
case "uint":
value = (string)baseValue;
valueUInt64 = EncodeUInt(value);
UInt32 myuint32 = (UInt32)(valueUInt64 & 0xffffffff);
valueDirect = myuint32;
break;
case "ulong":
value = (string)baseValue;
valueUInt64 = EncodeUInt(value);
valueDirect = valueUInt64;
break;
case "byte":
value = (string)baseValue;
valueInt64 = EncodeInt(value);
SByte mysbyte = (SByte)(valueInt64 & 0xff);
valueDirect = mysbyte;
break;
case "short":
value = (string)baseValue;
valueInt64 = EncodeInt(value);
Int16 myint16 = (Int16)(valueInt64 & 0xffff);
valueDirect = myint16;
break;
case "int":
value = (string)baseValue;
valueInt64 = EncodeInt(value);
Int32 myint32 = (Int32)(valueInt64 & 0xffffffff);
valueDirect = myint32;
break;
case "long":
value = (string)baseValue;
valueInt64 = EncodeInt(value);
valueDirect = valueInt64;
break;
case "float":
value = StripLeading0x((string)baseValue);
UInt32 num32 = UInt32.Parse(value, System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] floatVals = BitConverter.GetBytes(num32);
float flt = BitConverter.ToSingle(floatVals, 0);
valueDirect = flt;
break;
case "double":
value = StripLeading0x((string)baseValue);
UInt64 num64 = UInt64.Parse(value, System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] doubleVals = BitConverter.GetBytes(num64);
double dbl = BitConverter.ToDouble(doubleVals, 0);
valueDirect = dbl;
break;
case "timestamp":
// epochTicks is the number of 100uSec ticks between 01/01/0001
// and 01/01/1970. Used to adjust between DateTime and unix epoch.
const long epochTicks = 621355968000000000;
value = StripLeading0x((string)baseValue);
Int64 dtticks = Int64.Parse(value, System.Globalization.NumberStyles.AllowHexSpecifier);
dtticks *= TimeSpan.TicksPerMillisecond;
dtticks += epochTicks;
DateTime dt = new DateTime(dtticks, DateTimeKind.Utc);
valueDirect = dt;
break;
case "uuid":
value = (string)baseValue;
Guid guid = new Guid(value);
valueDirect = guid;
break;
case "binary":
// TODO: fix this
value = (string)baseValue;
byte[] binval = Convert.FromBase64String(value);
valueDirect = binval;
break;
case "string":
valueDirect = (string)baseValue;
break;
case "symbol":
Symbol sym = new Symbol((string)baseValue);
valueDirect = sym;
break;
case "list":
throw new ApplicationException(String.Format(
"Sender asked to encode a list but received a string: {0}", baseValue));
case "map":
throw new ApplicationException(String.Format(
"Sender asked to encode a map but received a string: {0}", baseValue));
case "decimal32":
case "decimal64":
case "decimal128":
throw new ApplicationException(String.Format(
"AMQP.Net Lite does not support AMQP decimal type: {0}", baseType));
default:
throw new ApplicationException(String.Format(
"Sender can not encode base type: {0}", baseType));
}
}
else
{
throw new ApplicationException(String.Format(
"Sender can not encode object type {0}", baseValue.GetType().Name));
}
encoded = true;
}