29 |
using System.Reflection; |
using System.Reflection; |
30 |
using System.Collections; |
using System.Collections; |
31 |
using System.Runtime.Serialization; |
using System.Runtime.Serialization; |
32 |
|
using System.Runtime.Serialization.Formatters.Binary; |
33 |
|
|
34 |
/* |
/* |
35 |
* unsupported features: |
* unsupported features: |
441 |
private static BinaryValueReader sArrayFiller32bReader = new ArrayFiller32bReader(); |
private static BinaryValueReader sArrayFiller32bReader = new ArrayFiller32bReader(); |
442 |
private static BinaryValueReader sBoxedPrimitiveTypeValue = new BoxedPrimitiveTypeValue(); |
private static BinaryValueReader sBoxedPrimitiveTypeValue = new BoxedPrimitiveTypeValue(); |
443 |
|
|
444 |
|
private static TypeSpecification sBooleanSpec = new TypeSpecification(BinaryPrimitiveTypeCode.Boolean); |
445 |
|
private static TypeSpecification sByteSpec = new TypeSpecification(BinaryPrimitiveTypeCode.Byte); |
446 |
|
private static TypeSpecification sCharSpec = new TypeSpecification(BinaryPrimitiveTypeCode.Char); |
447 |
|
private static TypeSpecification sDecimalSpec = new TypeSpecification(BinaryPrimitiveTypeCode.Decimal); |
448 |
|
private static TypeSpecification sDoubleSpec = new TypeSpecification(BinaryPrimitiveTypeCode.Double); |
449 |
|
private static TypeSpecification sInt16Spec = new TypeSpecification(BinaryPrimitiveTypeCode.Int16); |
450 |
|
private static TypeSpecification sInt32Spec = new TypeSpecification(BinaryPrimitiveTypeCode.Int32); |
451 |
|
private static TypeSpecification sInt64Spec = new TypeSpecification(BinaryPrimitiveTypeCode.Int64); |
452 |
|
private static TypeSpecification sSByteSpec = new TypeSpecification(BinaryPrimitiveTypeCode.SByte); |
453 |
|
private static TypeSpecification sSingleSpec = new TypeSpecification(BinaryPrimitiveTypeCode.Single); |
454 |
|
private static TypeSpecification sTimeSpanSpec = new TypeSpecification(BinaryPrimitiveTypeCode.TimeSpan); |
455 |
|
private static TypeSpecification sDateTimeSpec = new TypeSpecification(BinaryPrimitiveTypeCode.DateTime); |
456 |
|
private static TypeSpecification sUInt16Spec = new TypeSpecification(BinaryPrimitiveTypeCode.UInt16); |
457 |
|
private static TypeSpecification sUInt32Spec = new TypeSpecification(BinaryPrimitiveTypeCode.UInt32); |
458 |
|
private static TypeSpecification sUInt64Spec = new TypeSpecification(BinaryPrimitiveTypeCode.UInt64); |
459 |
|
private static TypeSpecification sStringSpec = new TypeSpecification(BinaryPrimitiveTypeCode.String); |
460 |
|
|
461 |
|
private static TypeSpecification sStringSpecObject = new TypeSpecification("System.String"); |
462 |
|
private static TypeSpecification sObjectSpec = new TypeSpecification("System.Object"); |
463 |
|
private static TypeSpecification sStringArraySpec = new TypeSpecification("System.String[]"); |
464 |
|
private static TypeSpecification sObjectArraySpec = new TypeSpecification("System.Object[]"); |
465 |
|
|
466 |
public static BinaryValueReader GetReader(BinaryElementType type) |
public static BinaryValueReader GetReader(BinaryElementType type) |
467 |
{ |
{ |
468 |
switch(type) |
switch(type) |
551 |
} |
} |
552 |
} |
} |
553 |
|
|
554 |
public static Type GetPrimitiveType(BinaryPrimitiveTypeCode typeCode) |
public static void ReadPrimitiveTypeArray(DeserializationContext context, BinaryPrimitiveTypeCode typeCode, Array array) |
555 |
|
{ |
556 |
|
int count = array.GetLength(0); |
557 |
|
switch(typeCode) |
558 |
|
{ |
559 |
|
case BinaryPrimitiveTypeCode.Boolean: |
560 |
|
bool[] boolArray = (bool[]) array; |
561 |
|
for(int i = 0; i < count; i++) { |
562 |
|
boolArray[i] = context.Reader.ReadBoolean(); |
563 |
|
} |
564 |
|
break; |
565 |
|
|
566 |
|
case BinaryPrimitiveTypeCode.Byte: |
567 |
|
byte[] byteArray = (byte[]) array; |
568 |
|
for(int i = 0; i < count; i++) { |
569 |
|
byteArray[i] = context.Reader.ReadByte(); |
570 |
|
} |
571 |
|
break; |
572 |
|
|
573 |
|
case BinaryPrimitiveTypeCode.Char: |
574 |
|
char[] charArray = (char[]) array; |
575 |
|
for(int i = 0; i < count; i++) { |
576 |
|
charArray[i] = context.Reader.ReadChar(); |
577 |
|
} |
578 |
|
break; |
579 |
|
|
580 |
|
case BinaryPrimitiveTypeCode.Decimal: |
581 |
|
decimal[] decimalArray = (decimal[]) array; |
582 |
|
for(int i = 0; i < count; i++) { |
583 |
|
decimalArray[i] = Decimal.Parse(context.Reader.ReadString()); |
584 |
|
} |
585 |
|
break; |
586 |
|
|
587 |
|
case BinaryPrimitiveTypeCode.Double: |
588 |
|
double[] doubleArray = (double[]) array; |
589 |
|
for(int i = 0; i < count; i++) { |
590 |
|
doubleArray[i] = context.Reader.ReadDouble(); |
591 |
|
} |
592 |
|
break; |
593 |
|
|
594 |
|
case BinaryPrimitiveTypeCode.Int16: |
595 |
|
short[] shortArray = (short[]) array; |
596 |
|
for(int i = 0; i < count; i++) { |
597 |
|
shortArray[i] = context.Reader.ReadInt16(); |
598 |
|
} |
599 |
|
break; |
600 |
|
|
601 |
|
case BinaryPrimitiveTypeCode.Int32: |
602 |
|
int[] intArray = (int[]) array; |
603 |
|
for(int i = 0; i < count; i++) { |
604 |
|
intArray[i] = context.Reader.ReadInt32(); |
605 |
|
} |
606 |
|
break; |
607 |
|
|
608 |
|
case BinaryPrimitiveTypeCode.Int64: |
609 |
|
long[] longArray = (long[]) array; |
610 |
|
for(int i = 0; i < count; i++) { |
611 |
|
longArray[i] = context.Reader.ReadInt64(); |
612 |
|
} |
613 |
|
break; |
614 |
|
|
615 |
|
case BinaryPrimitiveTypeCode.SByte: |
616 |
|
sbyte[] sbyteArray = (sbyte[]) array; |
617 |
|
for(int i = 0; i < count; i++) { |
618 |
|
sbyteArray[i] = context.Reader.ReadSByte(); |
619 |
|
} |
620 |
|
break; |
621 |
|
|
622 |
|
case BinaryPrimitiveTypeCode.Single: |
623 |
|
float[] singleArray = (float[]) array; |
624 |
|
for(int i = 0; i < count; i++) { |
625 |
|
singleArray[i] = context.Reader.ReadChar(); |
626 |
|
} |
627 |
|
break; |
628 |
|
|
629 |
|
case BinaryPrimitiveTypeCode.TimeSpan: |
630 |
|
TimeSpan[] tsArray = (TimeSpan[]) array; |
631 |
|
for(int i = 0; i < count; i++) { |
632 |
|
tsArray[i] = new TimeSpan(context.Reader.ReadInt64()); |
633 |
|
} |
634 |
|
break; |
635 |
|
|
636 |
|
case BinaryPrimitiveTypeCode.DateTime: |
637 |
|
DateTime[] dtArray = (DateTime[]) array; |
638 |
|
for(int i = 0; i < count; i++) { |
639 |
|
dtArray[i] = new DateTime(context.Reader.ReadInt64()); |
640 |
|
} |
641 |
|
break; |
642 |
|
|
643 |
|
case BinaryPrimitiveTypeCode.UInt16: |
644 |
|
ushort[] ushortArray = (ushort[]) array; |
645 |
|
for(int i = 0; i < count; i++) { |
646 |
|
ushortArray[i] = context.Reader.ReadUInt16(); |
647 |
|
|
648 |
|
} |
649 |
|
break; |
650 |
|
|
651 |
|
case BinaryPrimitiveTypeCode.UInt32: |
652 |
|
uint[] uintArray = (uint[]) array; |
653 |
|
for(int i = 0; i < count; i++) { |
654 |
|
uintArray[i] = context.Reader.ReadUInt32(); |
655 |
|
} |
656 |
|
break; |
657 |
|
|
658 |
|
case BinaryPrimitiveTypeCode.UInt64: |
659 |
|
ulong[] ulongArray = (ulong[]) array; |
660 |
|
for(int i = 0; i < count; i++) { |
661 |
|
ulongArray[i] = context.Reader.ReadUInt64(); |
662 |
|
} |
663 |
|
break; |
664 |
|
|
665 |
|
case BinaryPrimitiveTypeCode.String: |
666 |
|
string[] stringArray = (string[]) array; |
667 |
|
for(int i = 0; i < count; i++) { |
668 |
|
stringArray[i] = context.Reader.ReadString(); |
669 |
|
} |
670 |
|
break; |
671 |
|
|
672 |
|
default: |
673 |
|
throw new SerializationException("unknown primitive type code:"+typeCode); |
674 |
|
} |
675 |
|
} |
676 |
|
|
677 |
|
public static Type GetPrimitiveType(BinaryPrimitiveTypeCode typeCode) |
678 |
{ |
{ |
679 |
switch(typeCode) |
switch(typeCode) |
680 |
{ |
{ |
748 |
{ |
{ |
749 |
case BinaryTypeTag.PrimitiveType: |
case BinaryTypeTag.PrimitiveType: |
750 |
case BinaryTypeTag.ArrayOfPrimitiveType: |
case BinaryTypeTag.ArrayOfPrimitiveType: |
751 |
return new TypeSpecification((BinaryPrimitiveTypeCode) context.Reader.ReadByte()); |
BinaryPrimitiveTypeCode typeCode = (BinaryPrimitiveTypeCode) context.Reader.ReadByte(); |
752 |
|
switch(typeCode) |
753 |
|
{ |
754 |
|
case BinaryPrimitiveTypeCode.Boolean: |
755 |
|
return sBooleanSpec; |
756 |
|
case BinaryPrimitiveTypeCode.Byte: |
757 |
|
return sByteSpec; |
758 |
|
case BinaryPrimitiveTypeCode.Char: |
759 |
|
return sCharSpec; |
760 |
|
case BinaryPrimitiveTypeCode.Decimal: |
761 |
|
return sDecimalSpec; |
762 |
|
case BinaryPrimitiveTypeCode.Double: |
763 |
|
return sDoubleSpec; |
764 |
|
case BinaryPrimitiveTypeCode.Int16: |
765 |
|
return sInt16Spec; |
766 |
|
case BinaryPrimitiveTypeCode.Int32: |
767 |
|
return sInt32Spec; |
768 |
|
case BinaryPrimitiveTypeCode.Int64: |
769 |
|
return sInt64Spec; |
770 |
|
case BinaryPrimitiveTypeCode.SByte: |
771 |
|
return sSByteSpec; |
772 |
|
case BinaryPrimitiveTypeCode.Single: |
773 |
|
return sSingleSpec; |
774 |
|
case BinaryPrimitiveTypeCode.TimeSpan: |
775 |
|
return sTimeSpanSpec; |
776 |
|
case BinaryPrimitiveTypeCode.DateTime: |
777 |
|
return sDateTimeSpec; |
778 |
|
case BinaryPrimitiveTypeCode.UInt16: |
779 |
|
return sUInt16Spec; |
780 |
|
case BinaryPrimitiveTypeCode.UInt32: |
781 |
|
return sUInt32Spec; |
782 |
|
case BinaryPrimitiveTypeCode.UInt64: |
783 |
|
return sUInt64Spec; |
784 |
|
case BinaryPrimitiveTypeCode.String: |
785 |
|
return sStringArraySpec; |
786 |
|
|
787 |
|
default: |
788 |
|
throw new SerializationException("unknown primitive type code:"+typeCode); |
789 |
|
} |
790 |
|
throw new SerializationException("unknown primitive type code:"+typeCode); |
791 |
|
|
792 |
case BinaryTypeTag.RuntimeType: |
case BinaryTypeTag.RuntimeType: |
793 |
return new TypeSpecification(context.Reader.ReadString()); |
return new TypeSpecification(context.Reader.ReadString()); |
798 |
return new TypeSpecification(typeName, assId); |
return new TypeSpecification(typeName, assId); |
799 |
|
|
800 |
case BinaryTypeTag.String: |
case BinaryTypeTag.String: |
801 |
return new TypeSpecification("System.String"); |
return sStringSpecObject; |
802 |
|
|
803 |
case BinaryTypeTag.ObjectType: |
case BinaryTypeTag.ObjectType: |
804 |
return new TypeSpecification("System.Object"); |
return sObjectSpec; |
805 |
|
|
806 |
case BinaryTypeTag.ArrayOfString: |
case BinaryTypeTag.ArrayOfString: |
807 |
return new TypeSpecification("System.String[]"); |
return sStringArraySpec; |
808 |
|
|
809 |
case BinaryTypeTag.ArrayOfObject: |
case BinaryTypeTag.ArrayOfObject: |
810 |
return new TypeSpecification("System.Object[]"); |
return sObjectArraySpec; |
811 |
|
|
812 |
default: |
default: |
813 |
return null; |
return null; |
893 |
// this is a primitive array |
// this is a primitive array |
894 |
convertedType = GetPrimitiveType((BinaryPrimitiveTypeCode) type); |
convertedType = GetPrimitiveType((BinaryPrimitiveTypeCode) type); |
895 |
array = Array.CreateInstance(convertedType, (int) count); |
array = Array.CreateInstance(convertedType, (int) count); |
896 |
for(uint i = 0; i < count; i++) |
ReadPrimitiveTypeArray(context, (BinaryPrimitiveTypeCode) type, array); |
|
{ |
|
|
Object val; |
|
|
val = ReadPrimitiveType(context, (BinaryPrimitiveTypeCode) type); |
|
|
array.SetValue(val, i); |
|
|
} |
|
897 |
} |
} |
898 |
else if(type is Type) |
else if(type is Type) |
899 |
{ |
{ |