IAmfItemWriter GetAmfWriter(AmfWriterMap writerMap, Type type) { IAmfItemWriter amfWriter; // Use the writer specified within our dictionary, if it exists. if (writerMap.TryGetValue(type, out amfWriter)) { return(amfWriter); } // Try the lookup again but with the base type (so we can serialize enums and arrays, // for example). if (type.BaseType != null && writerMap.TryGetValue(type.BaseType, out amfWriter)) { return(amfWriter); } // No writer exists. Create and cache the default one so we don't need to go through this // expensive lookup again. lock (writerMap) { // Check inside lock since type may have been added since our initial check if (writerMap.TryGetValue(type, out amfWriter)) { return(amfWriter); } amfWriter = writerMap.DefaultWriter; writerMap.Add(type, amfWriter); return(amfWriter); } }
private static IAmfItemWriter GetAmfWriter(AmfWriterMap writerMap, Type type) { IAmfItemWriter amfItemWriter; if (writerMap.TryGetValue(type, out amfItemWriter) || type.BaseType != (Type)null && writerMap.TryGetValue(type.BaseType, out amfItemWriter)) { return(amfItemWriter); } lock (writerMap) { if (writerMap.TryGetValue(type, out amfItemWriter)) { return(amfItemWriter); } IAmfItemWriter local_0_1 = writerMap.DefaultWriter; writerMap.Add(type, local_0_1); return(local_0_1); } }
AmfWriter() { var smallIntTypes = new[] { typeof(SByte), typeof(Byte), typeof(Int16), typeof(UInt16), typeof(Int32), typeof(UInt32) }; var bigOrFloatingTypes = new[] { typeof(Int64), typeof(UInt64), typeof(Single), typeof(Double), typeof(Decimal) }; Amf0Writers = new AmfWriterMap(new Amf0ObjectWriter()) { { typeof(Array), new Amf0ArrayWriter() }, { typeof(AsObject), new Amf0AsObjectWriter() }, { typeof(bool), new Amf0BooleanWriter() }, { typeof(char), new Amf0CharWriter() }, { typeof(DateTime), new Amf0DateTimeWriter() }, { typeof(Enum), new Amf0EnumWriter() }, { typeof(Guid), new Amf0GuidWriter() }, { typeof(string), new Amf0StringWriter() }, { typeof(XDocument), new Amf0XDocumentWriter() }, { typeof(XElement), new Amf0XElementWriter() }, }; var amf0NumberWriter = new Amf0NumberWriter(); foreach (var type in smallIntTypes.Concat(bigOrFloatingTypes)) { Amf0Writers.Add(type, amf0NumberWriter); } Amf3Writers = new AmfWriterMap(new Amf3ObjectWriter()) { { typeof(Array), new Amf3ArrayWriter() }, { typeof(AsObject), new Amf3AsObjectWriter() }, { typeof(bool), new Amf3BooleanWriter() }, { typeof(ByteArray), new Amf3ByteArrayWriter() }, { typeof(char), new Amf3CharWriter() }, { typeof(DateTime), new Amf3DateTimeWriter() }, { typeof(Enum), new Amf3EnumWriter() }, { typeof(Guid), new Amf3GuidWriter() }, { typeof(string), new Amf3StringWriter() }, { typeof(XDocument), new Amf3XDocumentWriter() }, { typeof(XElement), new Amf3XElementWriter() }, { typeof(byte[]), new Amf3NativeByteArrayWriter() }, // `IDictionary`s are handled in the object writer }; var amf3IntWriter = new Amf3IntWriter(); foreach (var type in smallIntTypes) { Amf3Writers.Add(type, amf3IntWriter); } var amf3FloatingWriter = new Amf3DoubleWriter(); foreach (var type in bigOrFloatingTypes) { Amf3Writers.Add(type, amf3FloatingWriter); } }
static AmfWriter() { Type[] typeArray1 = new Type[6] { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint) }; Type[] typeArray2 = new Type[5] { typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(Decimal) }; AmfWriterMap amfWriterMap1 = new AmfWriterMap((IAmfItemWriter) new Amf0ObjectWriter()); Type key1 = typeof(Array); Amf0ArrayWriter amf0ArrayWriter = new Amf0ArrayWriter(); amfWriterMap1.Add(key1, (IAmfItemWriter)amf0ArrayWriter); Type key2 = typeof(AsObject); Amf0AsObjectWriter amf0AsObjectWriter = new Amf0AsObjectWriter(); amfWriterMap1.Add(key2, (IAmfItemWriter)amf0AsObjectWriter); Type key3 = typeof(bool); Amf0BooleanWriter amf0BooleanWriter = new Amf0BooleanWriter(); amfWriterMap1.Add(key3, (IAmfItemWriter)amf0BooleanWriter); Type key4 = typeof(char); Amf0CharWriter amf0CharWriter = new Amf0CharWriter(); amfWriterMap1.Add(key4, (IAmfItemWriter)amf0CharWriter); Type key5 = typeof(DateTime); Amf0DateTimeWriter amf0DateTimeWriter = new Amf0DateTimeWriter(); amfWriterMap1.Add(key5, (IAmfItemWriter)amf0DateTimeWriter); Type key6 = typeof(Enum); Amf0EnumWriter amf0EnumWriter = new Amf0EnumWriter(); amfWriterMap1.Add(key6, (IAmfItemWriter)amf0EnumWriter); Type key7 = typeof(Guid); Amf0GuidWriter amf0GuidWriter = new Amf0GuidWriter(); amfWriterMap1.Add(key7, (IAmfItemWriter)amf0GuidWriter); Type key8 = typeof(string); Amf0StringWriter amf0StringWriter = new Amf0StringWriter(); amfWriterMap1.Add(key8, (IAmfItemWriter)amf0StringWriter); Type key9 = typeof(XDocument); Amf0XDocumentWriter amf0XdocumentWriter = new Amf0XDocumentWriter(); amfWriterMap1.Add(key9, (IAmfItemWriter)amf0XdocumentWriter); Type key10 = typeof(XElement); Amf0XElementWriter amf0XelementWriter = new Amf0XElementWriter(); amfWriterMap1.Add(key10, (IAmfItemWriter)amf0XelementWriter); AmfWriter.Amf0Writers = amfWriterMap1; Amf0NumberWriter amf0NumberWriter = new Amf0NumberWriter(); foreach (Type key11 in ((IEnumerable <Type>)typeArray1).Concat <Type>((IEnumerable <Type>)typeArray2)) { AmfWriter.Amf0Writers.Add(key11, (IAmfItemWriter)amf0NumberWriter); } AmfWriterMap amfWriterMap2 = new AmfWriterMap((IAmfItemWriter) new Amf3ObjectWriter()); Type key12 = typeof(Array); Amf3ArrayWriter amf3ArrayWriter = new Amf3ArrayWriter(); amfWriterMap2.Add(key12, (IAmfItemWriter)amf3ArrayWriter); Type key13 = typeof(AsObject); Amf3AsObjectWriter amf3AsObjectWriter = new Amf3AsObjectWriter(); amfWriterMap2.Add(key13, (IAmfItemWriter)amf3AsObjectWriter); Type key14 = typeof(bool); Amf3BooleanWriter amf3BooleanWriter = new Amf3BooleanWriter(); amfWriterMap2.Add(key14, (IAmfItemWriter)amf3BooleanWriter); Type key15 = typeof(ByteArray); Amf3ByteArrayWriter amf3ByteArrayWriter = new Amf3ByteArrayWriter(); amfWriterMap2.Add(key15, (IAmfItemWriter)amf3ByteArrayWriter); Type key16 = typeof(char); Amf3CharWriter amf3CharWriter = new Amf3CharWriter(); amfWriterMap2.Add(key16, (IAmfItemWriter)amf3CharWriter); Type key17 = typeof(DateTime); Amf3DateTimeWriter amf3DateTimeWriter = new Amf3DateTimeWriter(); amfWriterMap2.Add(key17, (IAmfItemWriter)amf3DateTimeWriter); Type key18 = typeof(Enum); Amf3EnumWriter amf3EnumWriter = new Amf3EnumWriter(); amfWriterMap2.Add(key18, (IAmfItemWriter)amf3EnumWriter); Type key19 = typeof(Guid); Amf3GuidWriter amf3GuidWriter = new Amf3GuidWriter(); amfWriterMap2.Add(key19, (IAmfItemWriter)amf3GuidWriter); Type key20 = typeof(string); Amf3StringWriter amf3StringWriter = new Amf3StringWriter(); amfWriterMap2.Add(key20, (IAmfItemWriter)amf3StringWriter); Type key21 = typeof(XDocument); Amf3XDocumentWriter amf3XdocumentWriter = new Amf3XDocumentWriter(); amfWriterMap2.Add(key21, (IAmfItemWriter)amf3XdocumentWriter); Type key22 = typeof(XElement); Amf3XElementWriter amf3XelementWriter = new Amf3XElementWriter(); amfWriterMap2.Add(key22, (IAmfItemWriter)amf3XelementWriter); Type key23 = typeof(byte[]); Amf3NativeByteArrayWriter nativeByteArrayWriter = new Amf3NativeByteArrayWriter(); amfWriterMap2.Add(key23, (IAmfItemWriter)nativeByteArrayWriter); AmfWriter.Amf3Writers = amfWriterMap2; Amf3IntWriter amf3IntWriter = new Amf3IntWriter(); foreach (Type key11 in typeArray1) { AmfWriter.Amf3Writers.Add(key11, (IAmfItemWriter)amf3IntWriter); } Amf3DoubleWriter amf3DoubleWriter = new Amf3DoubleWriter(); foreach (Type key11 in typeArray2) { AmfWriter.Amf3Writers.Add(key11, (IAmfItemWriter)amf3DoubleWriter); } }