123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
-
- using System;
- using System.Collections.Generic;
- //using System.Linq;
- using System.Text;
- namespace ECOM.ServiceBus
- {
- internal class FastMessageEncoder
- {
- internal class Proxy
- {
- FastMessageEncoder m_Encoder = new FastMessageEncoder ();
- public Proxy Encode<T>(string Key, T Value)
- {
- m_Encoder [Key] = Value;
- return this;
- }
- public override string ToString()
- {
- return m_Encoder.ToString ();
- }
- static public implicit operator string(Proxy proxy)
- {
- return proxy.ToString ();
- }
- }
- public static Proxy Encode<T>(string Key, T Value)
- {
- Proxy proxy = new Proxy ();
- proxy.Encode (Key, Value);
- return proxy;
- }
- public FastMessageEncoder()
- {
- m_arMessage = new List<string> ();
- }
- public void Set(string Key, string strValue)
- {
- StringBuilder Value = new StringBuilder (strValue);
- Value.Replace ("\\", "\\\\");
- Value.Replace ("/", "\\/");
- Value.Replace ("\"", "\\\"");
- StringBuilder build = new StringBuilder ();
- build.Append ('\r').Append ('\n').Append (' ').Append (' ');
- build.Append ('"').Append (Key).Append ('"');
- build.Append (' ').Append (':').Append (' ');
- build.Append ('"').Append (Value).Append ('"');
- m_arMessage.Add (build.ToString ());
- }
- public void Set(string Key, int Value)
- {
- StringBuilder build = new StringBuilder ();
- build.Append ('\r').Append ('\n').Append (' ').Append (' ');
- build.Append ('"').Append (Key).Append ('"');
- build.Append (' ').Append (':').Append (' ');
- build.Append (Value);
- m_arMessage.Add (build.ToString ());
- }
- public void Set(string Key, double Value)
- {
- StringBuilder build = new StringBuilder ();
- build.Append ('\r').Append ('\n').Append (' ').Append (' ');
- build.Append ('"').Append (Key).Append ('"');
- build.Append (' ').Append (':').Append (' ');
- build.Append (Value);
- m_arMessage.Add (build.ToString ());
- }
- public void Set(string Key, bool Value)
- {
- StringBuilder build = new StringBuilder ();
- build.Append ('\r').Append ('\n').Append (' ').Append (' ');
- build.Append ('"').Append (Key).Append ('"');
- build.Append (' ').Append (':').Append (' ');
- build.Append (Value.ToString ());
- m_arMessage.Add (build.ToString ());
- }
- public void Set(string Key, string [] arValue)
- {
- StringBuilder tValue = new StringBuilder ();
- foreach (string strValue in arValue)
- {
- StringBuilder Value = new StringBuilder (strValue);
- Value.Replace ("\\", "\\\\");
- Value.Replace ("/", "\\/");
- Value.Replace ("\"", "\\\"");
- if (tValue.Length > 0)
- {
- tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
- tValue.Append (',').Append (' ');
- }
- else
- {
- tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
- tValue.Append (' ').Append (' ');
- }
- tValue.Append ('\"').Append (Value).Append ('"');
- }
- StringBuilder build = new StringBuilder ();
- build.Append ('\r').Append ('\n').Append (' ').Append (' ');
- build.Append ('"').Append (Key).Append ('"');
- build.Append (' ').Append (':').Append (' ');
- build.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
- m_arMessage.Add (build.ToString ());
- }
- public void Set(string Key, List<string> arValue)
- {
- StringBuilder tValue = new StringBuilder ();
- foreach (string strValue in arValue)
- {
- StringBuilder Value = new StringBuilder (strValue);
- Value.Replace ("\\", "\\\\");
- Value.Replace ("/", "\\/");
- Value.Replace ("\"", "\\\"");
- if (tValue.Length > 0)
- {
- tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
- tValue.Append (',').Append (' ');
- }
- else
- {
- tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
- tValue.Append (' ').Append (' ');
- }
- tValue.Append ('\"').Append (Value).Append ('"');
- }
- StringBuilder build = new StringBuilder ();
- build.Append ('\r').Append ('\n').Append (' ').Append (' ');
- build.Append ('"').Append (Key).Append ('"');
- build.Append (' ').Append (':').Append (' ');
- build.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
- m_arMessage.Add (build.ToString ());
- }
- public void Set(string Key, List<FastMessageEncoder> arValue)
- {
- StringBuilder tValue = new StringBuilder ();
- foreach (FastMessageEncoder strValue in arValue)
- {
- StringBuilder Value = new StringBuilder (strValue.ToString ());
- Value.Replace ("\\", "\\\\");
- Value.Replace ("/", "\\/");
- Value.Replace ("\"", "\\\"");
- if (tValue.Length > 0)
- {
- tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
- tValue.Append (',').Append (' ');
- }
- else
- {
- tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
- tValue.Append (' ').Append (' ');
- }
- tValue.Append ('\"').Append (Value).Append ('"');
- }
- StringBuilder build = new StringBuilder ();
- build.Append ('\r').Append ('\n').Append (' ').Append (' ');
- build.Append ('"').Append (Key).Append ('"');
- build.Append (' ').Append (':').Append (' ');
- build.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
- m_arMessage.Add (build.ToString ());
- }
- public string ToStringOfElement()
- {
- StringBuilder build = new StringBuilder ();
- for (int Index = 0; Index < m_arMessage.Count; Index++)
- {
- string msg = m_arMessage [Index];
- if (Index > 0)
- build.Append (',');
- build.Append (msg);
- }
- return build.ToString ();
- }
- public override string ToString()
- {
- StringBuilder build = new StringBuilder ();
- build.Append ('\r').Append ('\n').Append ('{');
- build.Append (ToStringOfElement ());
- build.Append ('\r').Append ('\n').Append ('}').Append ('\r').Append ('\n');
- return build.ToString ();
- }
- static Type TypeOf_int = typeof (int);
- static Type TypeOf_uint = typeof (uint);
- static Type TypeOf_UInt16 = typeof (UInt16);
- static Type TypeOf_long = typeof (long);
- static Type TypeOf_ulong = typeof (ulong);
- static Type TypeOf_double = typeof (double);
- static Type TypeOf_float = typeof (float);
- static Type TypeOf_bool = typeof (bool);
- static Type TypeOf_string = typeof (string);
- static Type TypeOf_list_string = typeof (List<string>);
- static Type TypeOf_string_array = typeof (string []);
- public object this [string Key]
- {
- set
- {
- Type type = value.GetType ();
- if (type == TypeOf_int)
- {
- int toInt = (int) value;
- Set (Key, toInt);
- return;
- }
- if (type == TypeOf_uint)
- {
- uint toUint = (uint) value;
- int toInt = (int) toUint;
- Set (Key, toInt);
- return;
- }
- if (type == TypeOf_UInt16)
- {
- UInt16 toUint16 = (UInt16) value;
- int toInt = (int) toUint16;
- Set (Key, toInt);
- return;
- }
- if (type == TypeOf_long)
- {
- long toLong = (long) value;
- int toInt = (int) toLong;
- Set (Key, toInt);
- return;
- }
- if (type == TypeOf_ulong)
- {
- ulong toUlong = (ulong) value;
- int toInt = (int) toUlong;
- Set (Key, toInt);
- return;
- }
- if (type == TypeOf_double)
- {
- double toDouble = (double) value;
- Set (Key, toDouble);
- return;
- }
- if (type == TypeOf_float)
- {
- double toDouble = (float) value;
- Set (Key, toDouble);
- return;
- }
- if (type == TypeOf_bool)
- {
- bool toBool = (bool) value;
- Set (Key, toBool);
- return;
- }
- if (type == TypeOf_string)
- {
- Set (Key, value.ToString ());
- return;
- }
- if (type == TypeOf_list_string)
- {
- List<string> arString = (List<string>) value;
- Set (Key, arString);
- return;
- }
- if (type == TypeOf_string_array)
- {
- string [] arString = (string []) value;
- Set (Key, arString);
- return;
- }
- throw (new ArgumentException ("Unknown data type of " + type.ToString ()));
- }
- }
- protected List<string> m_arMessage;
- }
- }
|