Пример #1
0
        public void ImplementsISerializable()
        {
            MySerializableClass proxy = (MySerializableClass)
                                        generator.CreateClassProxy(typeof(MySerializableClass), new StandardInterceptor());

            Assert.IsTrue(proxy is ISerializable);
        }
Пример #2
0
        public void ProxyGenerationOptionsRespectedOnDeserialization()
        {
            MethodFilterHook       hook    = new MethodFilterHook("(get_Current)|(GetExecutingObject)");
            ProxyGenerationOptions options = new ProxyGenerationOptions(hook);

            options.AddMixinInstance(new SerializableMixin());
            options.Selector = new SerializableInterceptorSelector();

            MySerializableClass proxy = (MySerializableClass)generator.CreateClassProxy(
                typeof(MySerializableClass),
                new Type[0],
                options,
                new StandardInterceptor());

            Assert.AreEqual(proxy.GetType(), proxy.GetType().GetMethod("get_Current").DeclaringType);
            Assert.AreNotEqual(proxy.GetType(), proxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
            Assert.AreEqual(proxy.GetType().BaseType, proxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
            ProxyGenerationOptions options2 =
                (ProxyGenerationOptions)proxy.GetType().GetField("proxyGenerationOptions").GetValue(null);

            Assert.IsNotNull(Array.Find(options2.MixinsAsArray(), delegate(object o) { return(o is SerializableMixin); }));
            Assert.IsNotNull(options2.Selector);

            MySerializableClass otherProxy = (MySerializableClass)SerializeAndDeserialize(proxy);

            Assert.AreEqual(otherProxy.GetType(), otherProxy.GetType().GetMethod("get_Current").DeclaringType);
            Assert.AreNotEqual(otherProxy.GetType(), otherProxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
            Assert.AreEqual(otherProxy.GetType().BaseType,
                            otherProxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
            options2 = (ProxyGenerationOptions)otherProxy.GetType().GetField("proxyGenerationOptions").GetValue(null);
            Assert.IsNotNull(Array.Find(options2.MixinsAsArray(), delegate(object o) { return(o is SerializableMixin); }));
            Assert.IsNotNull(options2.Selector);
        }
Пример #3
0
        public void CreateSerializable()
        {
            MySerializableClass proxy = (MySerializableClass)
                                        generator.CreateClassProxy(typeof(MySerializableClass), new StandardInterceptor());

            Assert.IsTrue(proxy.GetType().IsSerializable);
        }
Пример #4
0
        public void DeserializationWithSpecificModuleScope()
        {
            ProxyObjectReference.SetScope(generator.ProxyBuilder.ModuleScope);
            MySerializableClass first  = generator.CreateClassProxy <MySerializableClass>(new StandardInterceptor());
            MySerializableClass second = SerializeAndDeserialize(first);

            Assert.AreSame(first.GetType(), second.GetType());
        }
Пример #5
0
        public void CreateSerializable()
        {
            ProxyObjectReference.ResetScope();

            MySerializableClass proxy = (MySerializableClass)
                                        generator.CreateClassProxy(typeof(MySerializableClass), new StandardInterceptor());

            Assert.IsTrue(proxy.GetType().IsSerializable);
        }
Пример #6
0
        public void SimpleProxySerialization()
        {
            MySerializableClass proxy = (MySerializableClass)
                                        generator.CreateClassProxy(typeof(MySerializableClass), new StandardInterceptor());

            DateTime current = proxy.Current;

            MySerializableClass otherProxy = (MySerializableClass)SerializeAndDeserialize(proxy);

            Assert.AreEqual(current, otherProxy.Current);
        }
Пример #7
0
        public void ReusingModuleScopeFromProxyObjectReference()
        {
            ProxyGenerator generatorWithSpecificModuleScope =
                new ProxyGenerator(new DefaultProxyBuilder(ProxyObjectReference.ModuleScope));

            Assert.AreSame(generatorWithSpecificModuleScope.ProxyBuilder.ModuleScope, ProxyObjectReference.ModuleScope);
            MySerializableClass first =
                generatorWithSpecificModuleScope.CreateClassProxy <MySerializableClass>(new StandardInterceptor());
            MySerializableClass second = SerializeAndDeserialize(first);

            Assert.AreSame(first.GetType(), second.GetType());
        }
Пример #8
0
 public async Task SendingValidObject()
 {
     var anObject = new MySerializableClass()
     {
         MyIntAttribute    = 4,
         MyStringAttribute = "hello",
         MyListAttribute   = new List <string>()
         {
             "one", "two"
         }
     };
     ICollector collector = new  HTTPDataCollectorAPI.Collector("{Your_Workspace_Id}", "{Your_Workspace_Key}");
     await collector.Collect("TestLogType", anObject);
 }
Пример #9
0
        public void GetValue_Used_ShouldWork(int?intVal, string strVal)
        {
            BinaryFormatter     formatter = new BinaryFormatter();
            MySerializableClass original  = new MySerializableClass(intVal, strVal);
            MySerializableClass deserialized;

            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, original);
                stream.Position = 0;
                deserialized    = (MySerializableClass)formatter.Deserialize(stream);
            }

            deserialized.MyInt.Should().Be(intVal);
            deserialized.MyString.Should().Be(strVal);
        }
Пример #10
0
        public void MixinsAppliedOnDeserialization()
        {
            ProxyGenerationOptions options = new ProxyGenerationOptions();

            options.AddMixinInstance(new SerializableMixin());

            MySerializableClass proxy = (MySerializableClass)generator.CreateClassProxy(
                typeof(MySerializableClass),
                new Type[0],
                options,
                new StandardInterceptor());

            Assert.IsTrue(proxy is IMixedInterface);

            MySerializableClass otherProxy = (MySerializableClass)SerializeAndDeserialize(proxy);

            Assert.IsTrue(otherProxy is IMixedInterface);
        }
Пример #11
0
        public void IsSerializable_Test()
        {
            var obj1 = new[] { "1", "2" };

            Assert.IsTrue(obj1.IsSerializable());
            var obj2 = new List <string> {
                "1", "2"
            };

            Assert.IsTrue(obj2.IsSerializable());
            object obj3 = null;

            Assert.IsFalse(obj3.IsSerializable());
            var obj4 = new MyClass();

            Assert.IsFalse(obj4.IsSerializable());
            var obj5 = new MySerializableClass();

            Assert.IsTrue(obj5.IsSerializable());
        }
Пример #12
0
        public void MixinFieldsSetOnDeserialization_ClassProxy()
        {
            ProxyGenerationOptions options = new ProxyGenerationOptions();

            options.AddMixinInstance(new SerializableMixin());

            MySerializableClass proxy = (MySerializableClass)generator.CreateClassProxy(
                typeof(MySerializableClass),
                new Type[0],
                options,
                new StandardInterceptor());

            Assert.IsTrue(proxy is IMixedInterface);
            Assert.IsNotNull(((IMixedInterface)proxy).GetExecutingObject());
            Assert.IsTrue(((IMixedInterface)proxy).GetExecutingObject() is SerializableMixin);

            MySerializableClass otherProxy = SerializeAndDeserialize(proxy);

            Assert.IsTrue(otherProxy is IMixedInterface);
            Assert.IsNotNull(((IMixedInterface)otherProxy).GetExecutingObject());
            Assert.IsTrue(((IMixedInterface)otherProxy).GetExecutingObject() is SerializableMixin);
        }
Пример #13
0
        public static void Main(string[] args)
        {
            TestOne();
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddHagar(configuration =>
            {
                configuration.Serializers.Add(typeof(SubTypeSerializer));
                configuration.Serializers.Add(typeof(BaseTypeSerializer));
            });
            serviceCollection.AddSingleton <IGeneralizedCodec, DotNetSerializableCodec>();
            serviceCollection.AddSingleton <IGeneralizedCodec, JsonCodec>();

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var codecProvider = serviceProvider.GetRequiredService <CodecProvider>();
            var serializer    = codecProvider.GetCodec <SubType>();

            SerializerSession GetSession() => serviceProvider.GetRequiredService <SessionPool>().GetSession();

            var testString = "hello, hagar";

            Test(
                GetSession,
                serializer,
                new SubType
            {
                BaseTypeString = "base",
                String         = "sub",
                Int            = 2,
            });
            Test(
                GetSession,
                serializer,
                new SubType
            {
                BaseTypeString = "base",
                String         = "sub",
                Int            = int.MinValue,
            });

            // Tests for duplicates
            Test(
                GetSession,
                serializer,
                new SubType
            {
                BaseTypeString = testString,
                String         = testString,
                Int            = 10
            });
            Test(
                GetSession,
                serializer,
                new SubType
            {
                BaseTypeString = testString,
                String         = null,
                Int            = 1
            });
            Test(
                GetSession,
                serializer,
                new SubType
            {
                BaseTypeString = testString,
                String         = null,
                Int            = 1
            });
            TestSkip(
                GetSession,
                serializer,
                new SubType
            {
                BaseTypeString = testString,
                String         = null,
                Int            = 1
            });
            Test(
                GetSession,
                serializer,
                new SubType
            {
                BaseTypeString   = "HOHOHO",
                AddedLaterString = testString,
                String           = null,
                Int = 1,
                Ref = testString
            });

            var self = new SubType
            {
                BaseTypeString   = "HOHOHO",
                AddedLaterString = testString,
                String           = null,
                Int = 1
            };

            self.Ref = self;
            Test(GetSession, serializer, self);

            self.Ref = Guid.NewGuid();
            Test(GetSession, serializer, self);

            Test(
                GetSession,
                new AbstractTypeSerializer <object>(),
                new WhackyJsonType
            {
                Number = 7,
                String = "bananas!"
            });
            var mySerializable = new MySerializableClass
            {
                String  = "yolooo",
                Integer = 38,
                Self    = null,
            };

            Test(
                GetSession,
                new AbstractTypeSerializer <object>(),
                mySerializable
                );

            mySerializable.Self = mySerializable;
            Test(
                GetSession,
                new AbstractTypeSerializer <object>(),
                mySerializable
                );

            Exception exception = null;

            try
            {
                throw new ReferenceNotFoundException(typeof(int), 2401);
            }
            catch (Exception e)
            {
                exception = e;
            }

            Test(
                GetSession,
                new AbstractTypeSerializer <object>(),
                exception
                );

            Test(GetSession, new AbstractTypeSerializer <object>(), new LocalDate());

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Пример #14
0
 protected MySerializableClass(SerializationInfo info, StreamingContext context)
 {
     this.String  = info.GetString(nameof(this.String));
     this.Integer = info.GetInt32(nameof(this.Integer));
     this.Self    = (MySerializableClass)info.GetValue(nameof(this.Self), typeof(MySerializableClass));
 }
Пример #15
0
        public static void Main(string[] args)
        {
            var serviceProvider = new ServiceCollection()
                                  .AddHagar(builder => builder.AddAssembly(typeof(SomeClassWithSerialzers).Assembly))
                                  .BuildServiceProvider();

            SerializerSession GetSession() => serviceProvider.GetRequiredService <SessionPool>().GetSession();

            TestRpc().GetAwaiter().GetResult();
            //return;
            TestOne();

            Test(
                GetSession,
                new AbstractTypeSerializer <object>(),
                new WhackyJsonType
            {
                Number = 7,
                String = "bananas!"
            });
            var mySerializable = new MySerializableClass
            {
                String  = "yolooo",
                Integer = 38,
                Self    = null,
            };

            Test(
                GetSession,
                new AbstractTypeSerializer <object>(),
                mySerializable
                );

            mySerializable.Self = mySerializable;
            Test(
                GetSession,
                new AbstractTypeSerializer <object>(),
                mySerializable
                );

            Exception exception = null;

            try
            {
                throw new ReferenceNotFoundException(typeof(int), 2401);
            }
            catch (Exception e)
            {
                exception = e;
            }

            Test(
                GetSession,
                new AbstractTypeSerializer <object>(),
                exception
                );

            Test(GetSession, new AbstractTypeSerializer <object>(), new LocalDate());

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }