예제 #1
1
        private void Check(TypeModel model, SerializationContext ctx, int magicNumber, string caption)
        {
            try
            {
                CanHazFactory orig = new CanHazFactory {Foo = 123, Bar = 456}, clone;
                using(var ms = new MemoryStream())
                {
                    model.Serialize(ms, orig, ctx);
                    ms.Position = 0;
                    clone = (CanHazFactory) model.Deserialize(ms, null, typeof(CanHazFactory), ctx);
                }

                Assert.AreNotSame(orig, clone);

                Assert.AreEqual(123, orig.Foo, caption);
                Assert.AreEqual(456, orig.Bar, caption);
                Assert.AreEqual(0, orig.MagicNumber, caption);

                Assert.AreEqual(123, clone.Foo, caption);
                Assert.AreEqual(456, clone.Bar, caption);
                Assert.AreEqual(magicNumber, clone.MagicNumber, caption);

            } catch
            {
                Debug.WriteLine(caption);
                throw;
            }

        }
예제 #2
0
        public DoubleSerializer(ProtoBuf.Meta.TypeModel model)

        {
#if FEAT_IKVM
            expectedType = model.MapType(typeof(double));
#endif
        }
예제 #3
0
 internal XmlProtoSerializer(TypeModel model, int key)
 {
     if (model == null) throw new ArgumentNullException("model");
     if (key < 0) throw new ArgumentOutOfRangeException("key");
     this.model = model;
     this.key = key;
 }
예제 #4
0
 public static AttributeMap[] Create(TypeModel model, MemberInfo member, bool inherit)
 {
     #if FEAT_IKVM
     System.Collections.Generic.IList<CustomAttributeData> all = member.__GetCustomAttributes(model.MapType(typeof(Attribute)), inherit);
     AttributeMap[] result = new AttributeMap[all.Count];
     int index = 0;
     foreach (CustomAttributeData attrib in all)
     {
         result[index++] = new AttributeDataMap(attrib);
     }
     return result;
     #else
     #if WINRT
     Attribute[] all = System.Linq.Enumerable.ToArray(member.GetCustomAttributes(inherit));
     #else
     var all = member.GetCustomAttributes(inherit);
     #endif
     var result = new AttributeMap[all.Length];
     for (var i = 0; i < all.Length; i++)
     {
         result[i] = new ReflectionAttributeMap((Attribute) all[i]);
     }
     return result;
     #endif
 }
예제 #5
0
        public ArrayDecorator(TypeModel model, IProtoSerializer tail, int fieldNumber, bool writePacked, WireType packedWireType, Type arrayType, bool overwriteList, bool supportNull)
            : base(tail)
        {
            Helpers.DebugAssert(arrayType != null, "arrayType should be non-null");
            Helpers.DebugAssert(arrayType.IsArray && arrayType.GetArrayRank() == 1, "should be single-dimension array; " + arrayType.FullName);
            this.itemType = arrayType.GetElementType();
#if NO_GENERICS
            Type underlyingItemType = itemType;
#else
            Type underlyingItemType = supportNull ? itemType : (Helpers.GetUnderlyingType(itemType) ?? itemType);
#endif

            Helpers.DebugAssert(underlyingItemType == Tail.ExpectedType, "invalid tail");
            Helpers.DebugAssert(Tail.ExpectedType != model.MapType(typeof(byte)), "Should have used BlobSerializer");
            if ((writePacked || packedWireType != WireType.None) && fieldNumber <= 0) throw new ArgumentOutOfRangeException("fieldNumber");
            if (!ListDecorator.CanPack(packedWireType))
            {
                if (writePacked) throw new InvalidOperationException("Only simple data-types can use packed encoding");
                packedWireType = WireType.None;
            }       
            this.fieldNumber = fieldNumber;
            this.packedWireType = packedWireType;
            if (writePacked) options |= OPTIONS_WritePacked;
            if (overwriteList) options |= OPTIONS_OverwriteList;
            if (supportNull) options |= OPTIONS_SupportNull;
            this.arrayType = arrayType;
        }
예제 #6
0
 public NetObjectSerializer(TypeModel model, Type type, int key, BclHelpers.NetObjectOptions options)
 {
     bool dynamicType = (options & BclHelpers.NetObjectOptions.DynamicType) != 0;
     this.key = dynamicType ? -1 : key;
     this.type = dynamicType ? model.MapType(typeof (object)) : type;
     this.options = options;
 }
		public void DeepCloneMaintainReference(TypeModel model)
		{
			var collection = new List<ItemClass>() { new ItemClass() { Message = "Hello" } };
			var itemClass = new ItemClass() { Message = "Haha" };

			var obj = new ComplexTestOfMaintainedReference()
			{
				Collection1 = collection,
				Collection2 = collection,
				Item1 = itemClass,
				Item2 = itemClass,
				Primitive1 = 1,
				Primitive2 = 2,
				String1 = "Test"
			};

			var clone = (ComplexTestOfMaintainedReference)model.DeepClone(obj);

			Assert.AreEqual(obj.Collection1.Count, clone.Collection1.Count);
			Assert.AreEqual(obj.Collection1[0].Message, clone.Collection1[0].Message);

			Assert.AreEqual(obj.Collection2.Count, clone.Collection2.Count);
			Assert.AreEqual(obj.Collection2[0].Message, clone.Collection2[0].Message);

			Assert.IsTrue(object.ReferenceEquals(clone.Collection1, clone.Collection2), "Collection reference");

			Assert.AreEqual(obj.Item1.Message, clone.Item1.Message);
			Assert.AreEqual(obj.Item2.Message, clone.Item2.Message);

			Assert.IsTrue(object.ReferenceEquals(clone.Item1, clone.Item2), "Item reference");

			Assert.AreEqual(obj.Primitive1, clone.Primitive1);
			Assert.AreEqual(obj.Primitive2, clone.Primitive2);
			Assert.AreEqual(obj.String1, clone.String1);
		}
        public void Callback(object value, TypeModel.CallbackType callbackType)
        {
            if (callbacks != null) InvokeCallback(callbacks[callbackType], value);
            IProtoTypeSerializer ser = (IProtoTypeSerializer)GetMoreSpecificSerializer(value);
            if (ser != null) ser.Callback(value, callbackType);

        }
예제 #9
0
        public static AttributeMap[] Create(TypeModel model, Type type, bool inherit)
        {
#if FEAT_IKVM
            Type attribType = model.MapType(typeof(System.Attribute));
            System.Collections.Generic.IList<CustomAttributeData> all = type.__GetCustomAttributes(attribType, inherit);
            AttributeMap[] result = new AttributeMap[all.Count];
            int index = 0;
            foreach (CustomAttributeData attrib in all)
            {
                result[index++] = new AttributeDataMap(attrib);
            }
            return result;
#else
#if WINRT || COREFX
            Attribute[] all = System.Linq.Enumerable.ToArray(type.GetTypeInfo().GetCustomAttributes(inherit));
#else
            object[] all = type.GetCustomAttributes(inherit);
#endif
            AttributeMap[] result = new AttributeMap[all.Length];
            for(int i = 0 ; i < all.Length ; i++)
            {
                result[i] = new ReflectionAttributeMap((Attribute)all[i]);
            }
            return result;
#endif
        }
예제 #10
0
        public static ParseableSerializer TryCreate(Type type, TypeModel model)
        {
            if (type == null) throw new ArgumentNullException("type");
#if WINRT || PORTABLE || COREFX
            MethodInfo method = null;
            
#if WINRT || COREFX
            foreach (MethodInfo tmp in type.GetTypeInfo().GetDeclaredMethods("Parse"))
#else
            foreach (MethodInfo tmp in type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly))
#endif
            {
                ParameterInfo[] p;
                if (tmp.Name == "Parse" && tmp.IsPublic && tmp.IsStatic && tmp.DeclaringType == type && (p = tmp.GetParameters()) != null && p.Length == 1 && p[0].ParameterType == typeof(string))
                {
                    method = tmp;
                    break;
                }
            }
#else
            MethodInfo method = type.GetMethod("Parse",
                BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly,
                null, new Type[] { model.MapType(typeof(string)) }, null);
#endif
            if (method != null && method.ReturnType == type)
            {
                if (Helpers.IsValueType(type))
                {
                    MethodInfo toString = GetCustomToString(type);
                    if (toString == null || toString.ReturnType != model.MapType(typeof(string))) return null; // need custom ToString, fools
                }
                return new ParseableSerializer(method);
            }
            return null;
        }
예제 #11
0
 public NetObjectSerializer(TypeModel model, Type type, int key, BclHelpers.NetObjectOptions options)
 {
     bool flag = (byte)(options & BclHelpers.NetObjectOptions.DynamicType) != 0;
     this.key = ((!flag) ? key : -1);
     this.type = ((!flag) ? type : model.MapType(typeof(object)));
     this.options = options;
 }
		public void DeepClone_ClassFieldMaintainsReference(TypeModel model)
		{
			var item = new ItemClass() { Message = "Bob yo" };

			var container = new ContainerItemClass()
			{
				Item1 = item,
				Item2 = item
			};

			var clone = (ContainerItemClass)model.DeepClone(container);

			Assert.IsTrue(object.ReferenceEquals(container.Item1, container.Item2));
			Assert.IsTrue(object.ReferenceEquals(clone.Item1, clone.Item2));

			Assert.AreEqual(container.Item1.Message, clone.Item1.Message);
			Assert.AreEqual(container.Item2.Message, clone.Item2.Message);

			container.Item1.Message = "New original";
			clone.Item1.Message = "New clone";

			Assert.AreEqual(container.Item1.Message, container.Item2.Message);
			Assert.AreEqual(clone.Item1.Message, clone.Item2.Message);

			container.Item2.Message = "New original 2";
			clone.Item2.Message = "New clone 2";

			Assert.AreEqual(container.Item1.Message, container.Item2.Message);
			Assert.AreEqual(clone.Item1.Message, clone.Item2.Message);
		}
		public void DeepClone_DictionaryStringToObjectContainer(TypeModel model)
		{
			var dictionary = new Dictionary<string, YellowPage>()
			                 	{
			                 		{ "A", new YellowPage() { Message = "Ahahaha" } },
			                 		{ "B", new YellowPage() { Message = "Blahblah" } },
			                 		{ "C", new YellowPage() { Message = "Caca" } },
			                 	};

			var yellowPages = new YellowPages()
			{
				Collection = dictionary,
				RefLink = dictionary
			};

			yellowPages.Collection.Add("D", new YellowPage() { Message = "Dannnnnnng!" });

			var cloneYellowPages = (YellowPages)model.DeepClone(yellowPages);

			Assert.AreEqual(yellowPages.Collection.Count, yellowPages.RefLink.Count);

			foreach (var key in yellowPages.Collection.Keys)
			{
				Assert.AreEqual(yellowPages.Collection[key].Message, cloneYellowPages.Collection[key].Message);
			}

			cloneYellowPages.Collection.Add("E", new YellowPage() { Message = "Ello without H" });
			Assert.AreEqual(cloneYellowPages.Collection.Count, cloneYellowPages.RefLink.Count);
		}
예제 #14
0
        public static AttributeMap[] Create(TypeModel model, Assembly assembly)
        {

#if FEAT_IKVM
            const bool inherit = false;
            System.Collections.Generic.IList<CustomAttributeData> all = assembly.__GetCustomAttributes(model.MapType(typeof(Attribute)), inherit);
            AttributeMap[] result = new AttributeMap[all.Count];
            int index = 0;
            foreach (CustomAttributeData attrib in all)
            {
                result[index++] = new AttributeDataMap(attrib);
            }
            return result;
#else
#if WINRT || COREFX
            Attribute[] all = System.Linq.Enumerable.ToArray(assembly.GetCustomAttributes());
#else
            const bool inherit = false;
            object[] all = assembly.GetCustomAttributes(inherit);
#endif
            AttributeMap[] result = new AttributeMap[all.Length];
            for(int i = 0 ; i < all.Length ; i++)
            {
                result[i] = new ReflectionAttributeMap((Attribute)all[i]);
            }
            return result;
#endif
        }
		public void DeepClone_ClassWithFields(TypeModel model)
		{
			var classWithFields =
				new ClassWithFields(
					12,
					"lalala",
					new Guid("e0947b41-437a-4bda-8a47-16451417f307"),
					new ItemClass() { Message = "hello" },
					new List<int>() { 23, 45 },
					EnumTest.Item2,
					0.00000m,
					1.2345678m);

			var clone = (ClassWithFields)model.DeepClone(classWithFields);

			Assert.AreEqual(classWithFields.Guid, clone.Guid);
			Assert.AreEqual(classWithFields.Integer, clone.Integer);
			Assert.AreEqual(classWithFields.ItemClass.Message, clone.ItemClass.Message);
			Assert.AreEqual(classWithFields.List.Count, clone.List.Count);
			Assert.AreEqual(classWithFields.List[0], clone.List[0]);
			Assert.AreEqual(classWithFields.List[1], clone.List[1]);
			Assert.AreEqual(classWithFields.S, clone.S);
			Assert.AreEqual(classWithFields.EnumTest, clone.EnumTest);
			Assert.AreEqual(classWithFields._dec1, clone._dec1);
			Assert.AreEqual(classWithFields._dec2, clone._dec2);
		}
예제 #16
0
        static int GetKey(TypeModel model, ref Type type, out bool isList)
        {
            if (model != null && type != null)
            {
                int key = model.GetKey(ref type);
                if (key >= 0)
                {
                    isList = false;
                    return key;
                }
                Type itemType = TypeModel.GetListItemType(model, type);
                if (itemType != null)
                {
                    key = model.GetKey(ref itemType);
                    if (key >= 0)
                    {
                        isList = true;
                        return key;
                    }
                }
            }

            isList = false;
            return -1;
            
        }
예제 #17
0
        void Check(TypeModel model)
        {
            
            var obj = Tuple.Create(
                123, new[] { Tuple.Create(1, 2, 3, 4, 5, 6, 7, new List<Tuple<float, float>> { Tuple.Create(1F,2F) }), Tuple.Create(9, 10, 11, 12, 13, 14, 15, new List<Tuple<float, float>> { Tuple.Create(3F,4F) }) }, true);

            var clone = (Tuple<int, Tuple<int, int, int, int, int, int, int, Tuple<List<Tuple<float, float>>>>[], bool>)model.DeepClone(obj);

            Assert.AreEqual(123, clone.Item1);
            Assert.AreEqual(2, clone.Item2.Length);
            Assert.AreEqual(1, clone.Item2[0].Item1);
            Assert.AreEqual(2, clone.Item2[0].Item2);
            Assert.AreEqual(3, clone.Item2[0].Item3);
            Assert.AreEqual(4, clone.Item2[0].Item4);
            Assert.AreEqual(5, clone.Item2[0].Item5);
            Assert.AreEqual(6, clone.Item2[0].Item6);
            Assert.AreEqual(7, clone.Item2[0].Item7);
            Assert.AreEqual(Tuple.Create(1F,2F), clone.Item2[0].Rest.Item1.Single());
            Assert.AreEqual(9, clone.Item2[1].Item1);
            Assert.AreEqual(10, clone.Item2[1].Item2);
            Assert.AreEqual(11, clone.Item2[1].Item3);
            Assert.AreEqual(12, clone.Item2[1].Item4);
            Assert.AreEqual(13, clone.Item2[1].Item5);
            Assert.AreEqual(14, clone.Item2[1].Item6);
            Assert.AreEqual(15, clone.Item2[1].Item7);
            Assert.AreEqual(Tuple.Create(3F, 4F), clone.Item2[1].Rest.Item1.Single());
            Assert.AreEqual(true, clone.Item3);
        }
예제 #18
0
        public Int64Serializer(ProtoBuf.Meta.TypeModel model)

        {
#if FEAT_IKVM
            expectedType = model.MapType(typeof(long));
#endif
        }
		public void DeepClone_DictionaryOfClassRef_AsReference(TypeModel model)
		{
			var itemClass = new ItemClass() { Message = "hello"};

			var dictionary = new DictionaryOfClass()
			{
				Collection = new Dictionary<int, ItemClass>
				{
				    { 1, itemClass },   
					{ 2, itemClass }
				}
			};

			var clone = (DictionaryOfClass)model.DeepClone(dictionary);

			Assert.AreEqual(dictionary.Collection.Count, clone.Collection.Count);

			foreach (var key in dictionary.Collection.Keys)
			{
				Assert.AreEqual(dictionary.Collection[key].Message, clone.Collection[key].Message);
			}

			Assert.IsTrue(object.ReferenceEquals(dictionary.Collection[1], dictionary.Collection[2]), "Original reference failed");
			Assert.IsTrue(object.ReferenceEquals(clone.Collection[1], clone.Collection[2]), "Clone reference not maintained");
		}
예제 #20
0
 /// <summary>
 /// Create a new ProtoOperationBehavior instance
 /// </summary>
 public ProtoOperationBehavior(OperationDescription operation)
     : base(operation)
 {
     #if !NO_RUNTIME
     model = RuntimeTypeModel.Default;
     #endif
 }
예제 #21
0
 /// <summary>
 /// Releases resources used by the reader, but importantly <b>does not</b> Dispose the 
 /// underlying stream; in many typical use-cases the stream is used for different
 /// processes, so it is assumed that the consumer will Dispose their stream separately.
 /// </summary>
 public void Dispose()
 {
     // importantly, this does **not** own the stream, and does not dispose it
     source = null;
     model = null;
     BufferPool.ReleaseBufferToPool(ref ioBuffer);
 }
예제 #22
0
        void ExecuteTest(TypeModel model, string test)
        {
            A a = new A { flags = new List<string> { "abc", "def" } }, c;

            Assert.IsNotNull(a.flags.Count, test);
            Assert.AreEqual(2, a.flags.Count, test);
            Assert.AreEqual("abc", a.flags[0], test);
            Assert.AreEqual("def", a.flags[1], test);

            B b;
            using (var ms = new MemoryStream())
            {
                model.Serialize(ms, a);
                ms.Position = 0;
                b = (B)model.Deserialize(ms, null, typeof(B));
            }
            Assert.IsNotNull(b.flags.Count, test);
            Assert.AreEqual(2, b.flags.Count, test);
            Assert.AreEqual("abc", b.flags[0], test);
            Assert.AreEqual("def", b.flags[1], test);

            using (var ms = new MemoryStream())
            {
                model.Serialize(ms, b);
                ms.Position = 0;
                c = (A)model.Deserialize(ms, null, typeof(A));
            }
            Assert.IsNotNull(c.flags.Count, test);
            Assert.AreEqual(2, c.flags.Count, test);
            Assert.AreEqual("abc", c.flags[0], test);
            Assert.AreEqual("def", c.flags[1], test);
        }
		public void DeepClone_ClassWithFieldsRef_AsReference(TypeModel model)
		{
			var classWithFields = new ClassWithFieldsRef();
			var item = new ItemClass() { Message = "Hi there!" };
			classWithFields._item1 = item;
			classWithFields._item2 = item;
			var list = new List<int> { 2, 4, 5 };
			classWithFields._list1 = list;
			classWithFields._list2 = list;

			var clone = (ClassWithFieldsRef)model.DeepClone(classWithFields);

			Assert.AreEqual(classWithFields._item1.Message, clone._item1.Message);
			Assert.AreEqual(classWithFields._item2.Message, clone._item2.Message);

			Assert.AreEqual(classWithFields._list1.Count, clone._list1.Count);
			for (int i = 0; i < classWithFields._list1.Count; i++)
			{
				Assert.AreEqual(classWithFields._list1[i], clone._list1[i]);
			}

			Assert.AreEqual(classWithFields._list2.Count, clone._list2.Count);
			for (int i = 0; i < classWithFields._list2.Count; i++)
			{
				Assert.AreEqual(classWithFields._list2[i], clone._list2[i]);
			}

			Assert.IsTrue(object.ReferenceEquals(classWithFields._item1, classWithFields._item2));
			Assert.IsTrue(object.ReferenceEquals(classWithFields._list1, classWithFields._list2));
			Assert.IsTrue(object.ReferenceEquals(clone._item1, clone._item2));
			Assert.IsTrue(object.ReferenceEquals(clone._list1, clone._list2));
		}
		public void DeepClone_FruitBag(TypeModel model)
		{
			var fruitBag = new FruitBag()
			{
				Apple = new Apple()
				{
					Age = 12,
					StickerMessage = "13",
					Taste = 14
				},
				Color = 15,
				Pear = new Pear()
				{
					Age = 16,
					StickerMessage = "17",
					Hello = 18,
					Taste = 19
				}
			};

			var cloneFruitBag = (FruitBag)model.DeepClone(fruitBag);

			Assert.AreEqual(fruitBag.Apple.Age, cloneFruitBag.Apple.Age);
			Assert.AreEqual(fruitBag.Apple.StickerMessage, cloneFruitBag.Apple.StickerMessage);
			Assert.AreEqual(fruitBag.Apple.Taste, cloneFruitBag.Apple.Taste);

			Assert.AreEqual(fruitBag.Color, cloneFruitBag.Color);

			Assert.AreEqual(fruitBag.Pear.Age, cloneFruitBag.Pear.Age);
			Assert.AreEqual(fruitBag.Pear.StickerMessage, cloneFruitBag.Pear.StickerMessage);
			Assert.AreEqual(fruitBag.Pear.Hello, cloneFruitBag.Pear.Hello);
			Assert.AreEqual(fruitBag.Pear.Taste, cloneFruitBag.Pear.Taste);
		}
예제 #25
0
 public ArrayDecorator(TypeModel model, IProtoSerializer tail, int fieldNumber, bool writePacked, WireType packedWireType, Type arrayType, bool overwriteList, bool supportNull)
     : base(tail)
 {
     this.itemType = arrayType.GetElementType();
     Type arg_3E_0 = (!supportNull) ? (Helpers.GetUnderlyingType(this.itemType) ?? this.itemType) : this.itemType;
     if ((writePacked || packedWireType != WireType.None) && fieldNumber <= 0)
     {
         throw new ArgumentOutOfRangeException("fieldNumber");
     }
     if (!ListDecorator.CanPack(packedWireType))
     {
         if (writePacked)
         {
             throw new InvalidOperationException("Only simple data-types can use packed encoding");
         }
         packedWireType = WireType.None;
     }
     this.fieldNumber = fieldNumber;
     this.packedWireType = packedWireType;
     if (writePacked)
     {
         this.options |= 1;
     }
     if (overwriteList)
     {
         this.options |= 2;
     }
     if (supportNull)
     {
         this.options |= 4;
     }
     this.arrayType = arrayType;
 }
예제 #26
0
            public Serializer()
            {
                _runtimeTypeModel = ProtoBuf.Meta.TypeModel.Create();

                var typeModel = ProtoBuf.Meta.TypeModel.Create();

                _compiledTypeModel = typeModel.Compile();
            }
예제 #27
0
 public SurrogateSerializer(TypeModel model, Type forType, Type declaredType, IProtoTypeSerializer rootTail)
 {
     this.forType = forType;
     this.declaredType = declaredType;
     this.rootTail = rootTail;
     this.toTail = this.GetConversion(model, true);
     this.fromTail = this.GetConversion(model, false);
 }
 /// <summary>
 /// Attempt to create a new serializer for the given model and type
 /// </summary>
 /// <returns>A new serializer instance if the type is recognised by the model; null otherwise</returns>
 public static XmlProtoSerializer TryCreate(TypeModel model, Type type)
 {
     if (model == null) throw new ArgumentNullException("model");
     if (type == null) throw new ArgumentNullException("type");
     int key = GetKey(model, type);
     if (key < 0) return null;
     return new XmlProtoSerializer(model, key);
 }
 internal ImmutableCollectionDecorator(TypeModel model, Type declaredType, Type concreteType, IProtoSerializer tail, int fieldNumber, bool writePacked, WireType packedWireType, bool returnList, bool overwriteList, bool supportNull, MethodInfo builderFactory, MethodInfo add, MethodInfo addRange, MethodInfo finish)
     : base(model, declaredType, concreteType, tail, fieldNumber, writePacked, packedWireType, returnList, overwriteList, supportNull)
 {
     this.builderFactory = builderFactory;
     this.add = add;
     this.addRange = addRange;
     this.finish = finish;
 }
예제 #30
0
 static void Execute(TypeModel model, string caption)
 {
     var orig = CreateData();
     var clone = (CanHazConcurrent)model.DeepClone(orig);
     Assert.AreNotEqual(clone, orig);
     
     TestData(clone, caption);
 }
예제 #31
0
        private static void TestMember(TypeModel model)
        {
            var value = new Foo {Bar = new E[] {E.V0, E.V1, E.V2}};

            Assert.IsTrue(Program.CheckBytes(value, model, 0x18, 0x03, 0x18, 0x04, 0x18, 0x05));
            var clone = (Foo) model.DeepClone(value);
            Assert.AreEqual("V0,V1,V2", string.Join(",", clone.Bar), "clone");
        }
예제 #32
0
 public PropertyDecorator(TypeModel model, Type forType, PropertyInfo property, IProtoSerializer tail)
     : base(tail)
 {
     this.forType = forType;
     this.property = property;
     PropertyDecorator.SanityCheck(model, property, tail, out this.readOptionsWriteValue, true, true);
     this.shadowSetter = PropertyDecorator.GetShadowSetter(model, property);
 }
예제 #33
0
 public void Callback(object value, TypeModel.CallbackType callbackType, SerializationContext context)
 {
     IProtoTypeSerializer protoTypeSerializer = this.Tail as IProtoTypeSerializer;
     if (protoTypeSerializer != null)
     {
         protoTypeSerializer.Callback(value, callbackType, context);
     }
 }
예제 #34
0
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public DataStream(IEnumerable <TItem> items, Type runtimeType = null, TypeModel typeModel = null)
        {
            this._enumerator  = items.GetEnumerator();
            this._runtimeType = runtimeType;
            if (runtimeType != null && !typeof(TItem).IsAssignableFrom(runtimeType))
            {
                throw new ArgumentOutOfRangeException(nameof(runtimeType), $"The type must be a subtype of {typeof(TItem)}.");
            }

            this._typeModel = typeModel ?? ProtoBuf.Meta.RuntimeTypeModel.Default;
        }
예제 #35
0
 public DoubleSerializer(ProtoBuf.Meta.TypeModel model)
 {
 }
예제 #36
0
        internal static Type GetListItemType(TypeModel model, Type listType)
        {
            if (listType == model.MapType(typeof(string)) || listType.IsArray || !model.MapType(typeof(IEnumerable)).IsAssignableFrom(listType))
            {
                return(null);
            }
            BasicList basicList = new BasicList();

            MethodInfo[] methods = listType.GetMethods();
            foreach (MethodInfo methodInfo in methods)
            {
                if (!methodInfo.IsStatic && !(methodInfo.Name != "Add"))
                {
                    ParameterInfo[] parameters = methodInfo.GetParameters();
                    Type            parameterType;
                    if (parameters.Length == 1 && !basicList.Contains(parameterType = parameters[0].ParameterType))
                    {
                        basicList.Add(parameterType);
                    }
                }
            }
            string name = listType.Name;

            if (name == null || (name.IndexOf("Queue") < 0 && name.IndexOf("Stack") < 0))
            {
                TestEnumerableListPatterns(model, basicList, listType);
                Type[] interfaces = listType.GetInterfaces();
                foreach (Type iType in interfaces)
                {
                    TestEnumerableListPatterns(model, basicList, iType);
                }
            }
            PropertyInfo[] properties = listType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (PropertyInfo propertyInfo in properties)
            {
                if (!(propertyInfo.Name != "Item") && !basicList.Contains(propertyInfo.PropertyType))
                {
                    ParameterInfo[] indexParameters = propertyInfo.GetIndexParameters();
                    if (indexParameters.Length == 1 && indexParameters[0].ParameterType == model.MapType(typeof(int)))
                    {
                        basicList.Add(propertyInfo.PropertyType);
                    }
                }
            }
            switch (basicList.Count)
            {
            case 0:
                return(null);

            case 1:
                return((Type)basicList[0]);

            case 2:
                if (CheckDictionaryAccessors(model, (Type)basicList[0], (Type)basicList[1]))
                {
                    return((Type)basicList[0]);
                }
                if (CheckDictionaryAccessors(model, (Type)basicList[1], (Type)basicList[0]))
                {
                    return((Type)basicList[1]);
                }
                break;
            }
            return(null);
        }
예제 #37
0
        private bool TryDeserializeList(TypeModel model, ProtoReader reader, DataFormat format, int tag, Type listType, Type itemType, ref object value)
        {
            bool       isList;
            MethodInfo methodInfo = ResolveListAdd(model, listType, itemType, out isList);

            if (methodInfo == null)
            {
                throw new NotSupportedException("Unknown list variant: " + listType.FullName);
            }
            bool   result = false;
            object value2 = null;
            IList  list   = value as IList;

            object[]  array     = isList ? null : new object[1];
            BasicList basicList = listType.IsArray ? new BasicList() : null;

            while (TryDeserializeAuxiliaryType(reader, format, tag, itemType, ref value2, skipOtherFields: true, asListItem: true, autoCreate: true, insideList: true))
            {
                result = true;
                if (value == null && basicList == null)
                {
                    value = CreateListInstance(listType, itemType);
                    list  = (value as IList);
                }
                if (list != null)
                {
                    list.Add(value2);
                }
                else if (basicList != null)
                {
                    basicList.Add(value2);
                }
                else
                {
                    array[0] = value2;
                    methodInfo.Invoke(value, array);
                }
                value2 = null;
            }
            if (basicList != null)
            {
                if (value != null)
                {
                    if (basicList.Count != 0)
                    {
                        Array array2 = (Array)value;
                        Array array3 = Array.CreateInstance(itemType, array2.Length + basicList.Count);
                        Array.Copy(array2, array3, array2.Length);
                        basicList.CopyTo(array3, array2.Length);
                        value = array3;
                    }
                }
                else
                {
                    Array array3 = Array.CreateInstance(itemType, basicList.Count);
                    basicList.CopyTo(array3, 0);
                    value = array3;
                }
            }
            return(result);
        }
 public StringSerializer(ProtoBuf.Meta.TypeModel model)
 {
 }
예제 #39
0
 static public int Deserialize(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (matchType(l, argc, 2, typeof(ProtoBuf.ProtoReader), typeof(System.Object), typeof(System.Type)))
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             ProtoBuf.ProtoReader    a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             var ret = self.Deserialize(a1, a2, a3);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         else if (matchType(l, argc, 2, typeof(System.IO.Stream), typeof(System.Object), typeof(System.Type)))
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             var ret = self.Deserialize(a1, a2, a3);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         else if (matchType(l, argc, 2, typeof(System.IO.Stream), typeof(System.Object), typeof(System.Type), typeof(int)))
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             System.Int32 a4;
             checkType(l, 5, out a4);
             var ret = self.Deserialize(a1, a2, a3, a4);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         else if (matchType(l, argc, 2, typeof(System.IO.Stream), typeof(System.Object), typeof(System.Type), typeof(ProtoBuf.SerializationContext)))
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             ProtoBuf.SerializationContext a4;
             checkType(l, 5, out a4);
             var ret = self.Deserialize(a1, a2, a3, a4);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         else if (argc == 6)
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             System.Int32 a4;
             checkType(l, 5, out a4);
             ProtoBuf.SerializationContext a5;
             checkType(l, 6, out a5);
             var ret = self.Deserialize(a1, a2, a3, a4, a5);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
예제 #40
0
 static public int DeserializeWithLengthPrefix(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (argc == 6)
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             ProtoBuf.PrefixStyle a4;
             checkEnum(l, 5, out a4);
             System.Int32 a5;
             checkType(l, 6, out a5);
             var ret = self.DeserializeWithLengthPrefix(a1, a2, a3, a4, a5);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         else if (argc == 7)
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             ProtoBuf.PrefixStyle a4;
             checkEnum(l, 5, out a4);
             System.Int32 a5;
             checkType(l, 6, out a5);
             ProtoBuf.Serializer.TypeResolver a6;
             LuaDelegation.checkDelegate(l, 7, out a6);
             var ret = self.DeserializeWithLengthPrefix(a1, a2, a3, a4, a5, a6);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         else if (argc == 8)
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             ProtoBuf.PrefixStyle a4;
             checkEnum(l, 5, out a4);
             System.Int32 a5;
             checkType(l, 6, out a5);
             ProtoBuf.Serializer.TypeResolver a6;
             LuaDelegation.checkDelegate(l, 7, out a6);
             System.Int32 a7;
             var          ret = self.DeserializeWithLengthPrefix(a1, a2, a3, a4, a5, a6, out a7);
             pushValue(l, true);
             pushValue(l, ret);
             pushValue(l, a7);
             return(3);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 public DecimalSerializer(ProtoBuf.Meta.TypeModel model)
 {
 }
 public Int64Serializer(ProtoBuf.Meta.TypeModel model)
 {
 }
예제 #43
0
 public DeserializeItemsIterator(TypeModel model, Stream source, PrefixStyle style, int expectedField, SerializationContext context)
     : base(model, source, model.MapType(typeof(T)), style, expectedField, null, context)
 {
 }