// EmitToClientConversion and EmitToServerConversion need to be kept in sync with the Utility type.
        private static void EmitToServerConversion(ILGenerator methodGenerator, Type sourceType, Type targetType)
        {
            if (BinaryTypeUtility.IsTypeBinary(targetType) && sourceType == typeof(byte[]))
            {
                Label continueLabel = methodGenerator.DefineLabel();
                Label notNullLabel  = methodGenerator.DefineLabel();

                // Check if the value is null.
                methodGenerator.Emit(OpCodes.Dup);
                methodGenerator.Emit(OpCodes.Brtrue, notNullLabel);

                // If it is, replace it with an "untyped" null.
                methodGenerator.Emit(OpCodes.Pop);
                methodGenerator.Emit(OpCodes.Ldnull);
                methodGenerator.Emit(OpCodes.Br, continueLabel);

                // Otherwise, call new Binary(byte[]).
                ConstructorInfo toBinaryConstructor = targetType.GetConstructor(new Type[] { typeof(byte[]) });
                methodGenerator.MarkLabel(notNullLabel);
                methodGenerator.Emit(OpCodes.Newobj, toBinaryConstructor);

                methodGenerator.MarkLabel(continueLabel);
            }
        }
Exemplo n.º 2
0
        internal static Type TranslateType(Type type)
        {
            if (BinaryTypeUtility.IsTypeBinary(type))
            {
                return(typeof(byte[]));
            }

            // Don't translate array types.
            if (!type.IsArray && TypeUtility.IsPredefinedListType(type))
            {
                return(typeof(IEnumerable <>).MakeGenericType(TypeUtility.GetElementType(type)));
            }

            if (TypeUtility.IsPredefinedDictionaryType(type))
            {
                Type[] genericArgs = GetDictionaryGenericArgumentTypes(type).Select(t => TranslateType(t)).ToArray();

                // TODO: translate to IDictionary<,> when we can properly support
                //       IDictonary<,> parameters in a serialized changeset.
                return(typeof(Dictionary <,>).MakeGenericType(genericArgs));
            }

            return(type);
        }
        // EmitToClientConversion and EmitToServerConversion need to be kept in sync with the Utility type.
        private static void EmitToClientConversion(ILGenerator methodGenerator, Type sourceType, Type targetType)
        {
            if (targetType == typeof(byte[]) && BinaryTypeUtility.IsTypeBinary(sourceType))
            {
                Label continueLabel = methodGenerator.DefineLabel();
                Label notNullLabel  = methodGenerator.DefineLabel();

                // Check if the value is null.
                methodGenerator.Emit(OpCodes.Dup);
                methodGenerator.Emit(OpCodes.Brtrue, notNullLabel);

                // If it is, replace it with an "untyped" null.
                methodGenerator.Emit(OpCodes.Pop);
                methodGenerator.Emit(OpCodes.Ldnull);
                methodGenerator.Emit(OpCodes.Br, continueLabel);

                // Otherwise, call ToArray().
                MethodInfo toArrayMethod = sourceType.GetMethod("ToArray", Type.EmptyTypes);
                methodGenerator.MarkLabel(notNullLabel);
                methodGenerator.Emit(OpCodes.Call, toArrayMethod);

                methodGenerator.MarkLabel(continueLabel);
            }
        }
Exemplo n.º 4
0
        public void BinarySupport()
        {
            Assert.IsFalse(BinaryTypeUtility.IsTypeBinary(typeof(int)),
                           "Int32 isn't a Binary.");
            Assert.IsFalse(BinaryTypeUtility.IsTypeBinary(typeof(string)),
                           "String isn't a Binary.");
            Assert.IsTrue(BinaryTypeUtility.IsTypeBinary(typeof(Binary)),
                          "Binary is a Binary!");

            Assert.AreEqual(typeof(byte[]), SerializationUtility.GetClientType(typeof(Binary)),
                            "The client type for Binary is byte[].");

            byte[] bytes  = new byte[] { 10, 20, 30 };
            Binary binary = new Binary(bytes);

            Assert.IsTrue(bytes.SequenceEqual((IEnumerable <byte>)SerializationUtility.GetClientValue(typeof(byte[]), binary)),
                          "Client Binary values should be equal.");
            Assert.IsTrue(bytes.SequenceEqual((IEnumerable <byte>)SerializationUtility.GetClientValue(typeof(byte[]), bytes)),
                          "Client byte[] values should be equal.");
            Assert.AreEqual(binary, SerializationUtility.GetServerValue(typeof(Binary), binary),
                            "Server Binary values should be equal.");
            Assert.AreEqual(binary, SerializationUtility.GetServerValue(typeof(Binary), bytes),
                            "Server byte[] values should be equal.");
        }