Exemplo n.º 1
0
        public void SetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, LocalBuilder memberValue)
        {
            generator.Ldarg(targetObject);
            generator.Load(memberName);
            generator.Ldloc(memberValue);

            // Type originalType = memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType;
            if (memberValue.LocalType.IsValueType)
            {
                generator.Box(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType);
            }
            //���ֵΪ�գ����ܵ���ToString
            generator.Load(null);
            generator.If(Cmp.NotEqualTo);
            generator.Ldloc(memberValue);

            // Type originalType = memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType;
            if (memberValue.LocalType.IsValueType)
            {
                generator.Box(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType);
            }
            generator.Call(typeof(object).GetMethod("ToString", Type.EmptyTypes));
            generator.Else();
            generator.LoadDefaultValue(memberValue.LocalType);
            if (memberValue.LocalType.IsValueType)
            {
                generator.Box(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType);
            }
            generator.EndIf();
            generator.Call(typeof(NameValueCollection).GetMethod("Add", new Type[] { typeof(string), typeof(string) }));
        }
Exemplo n.º 2
0
        public override ConvertHandler GetConvertHandler(Type inputType, Type outputType, object inputObject, object outputObject, Dictionary<string, string> mappingNames, List<string> ignoreList, Dictionary<int, string> mappingOrders, bool mappingSpecifiedOnly)
        {
            Dictionary<string, string> distToSrc = new Dictionary<string, string>();
            Dictionary<string, MemberInfo> members = new Dictionary<string, MemberInfo>();
            foreach (string sourceName in mappingNames.Keys)
            {
                MemberInfo sourceMember = ConvertorGeneratorHelper.GetMemberInfo(inputType, sourceName);
                Check.Require(sourceMember != null, string.Format("member named {0} could not be found in {1}", sourceName, outputType.FullName));
                distToSrc.Add(mappingNames[sourceName], sourceName);
                members.Add(mappingNames[sourceName], sourceMember);
            }
            if (!mappingSpecifiedOnly)
            {
                Dictionary<string, MemberInfo> sourceMembers = ConvertorGeneratorHelper.GetMembers(inputType);
                foreach (string sourceName in sourceMembers.Keys)
                {
                    if (!ignoreList.Contains(sourceName) && !distToSrc.ContainsKey(sourceName))
                    {
                        distToSrc.Add(sourceName, sourceName);
                        members.Add(sourceName, sourceMembers[sourceName]);
                    }
                }
            }

            CodeGenerator gen = new CodeGenerator();
            gen.BeginMethod("m" + Guid.NewGuid().ToString("N"), typeof(ConvertHandler));

            ArgBuilder inputObjectArg = new ArgBuilder(0, inputType);
            ArgBuilder outputObjectArg = new ArgBuilder(1, outputType);

            int currentCount = 0;
            int memberCount = members.Count;
            string[] keys = new string[memberCount];
            distToSrc.Keys.CopyTo(keys, 0);
            List<string> keyList = new List<string>(keys);
            MemberSetterGenerator.BeginSetMembers(gen, outputObjectArg);
            while (currentCount < memberCount)
            {
                currentCount++;
                string targetName = ConvertorGeneratorHelper.GetCurrentKey(mappingOrders, currentCount, keyList).Replace("_", "");
                if (string.IsNullOrEmpty(targetName))
                {
                    continue;
                }
                string sourceName = distToSrc[targetName].Replace("_","");
                if (MemberGetterGenerator.ContainsMember(sourceName, inputType, inputObject) && MemberSetterGenerator.ContainsMember(targetName, outputType, outputObject))
                {
                    Type targetType = ConvertorGeneratorHelper.GetMemberType(members[targetName]);

                    LocalBuilder memberValue = gen.DeclareLocal(targetType, "memberValue");

                    MemberGetterGenerator.GetMemberValue(gen, inputObjectArg, sourceName, ref memberValue);

                    MemberSetterGenerator.SetMemberValue(gen, outputObjectArg, targetName, memberValue);
                }
            }
            gen.Ldarg(outputObjectArg);
            MemberSetterGenerator.EndSetMembers(gen, outputObjectArg);
            return (ConvertHandler)gen.EndMethod();
        }
Exemplo n.º 3
0
        public void EndSetMembers(CodeGenerator generator, ArgBuilder targetObject)
        {
            generator.Ldarg(targetObject);
            generator.Load(null);
            generator.If(Cmp.EqualTo);
            generator.Pop();
            LocalBuilder dataRow = generator.DeclareLocal(typeof(DataRow));
            generator.Ldloc(locDataTable);
            generator.Call(typeof(DataTable).GetMethod("NewRow"));
            generator.Stloc(dataRow);

            LocalBuilder locCurrent = generator.DeclareLocal(typeof(string));
            LocalBuilder locEnumerator = generator.DeclareLocal(typeof(Dictionary<string, object>.KeyCollection.Enumerator));

            generator.Ldloc(locMemberValues);
            generator.Call(typeof(Dictionary<string, object>).GetMethod("get_Keys"));
            generator.Call(typeof(Dictionary<string, object>.KeyCollection).GetMethod("GetEnumerator"));
            generator.Stloc(locEnumerator);
            MethodInfo getCurrentMethod = typeof(Dictionary<string, object>.KeyCollection.Enumerator).GetMethod("get_Current");
            MethodInfo moveNextMethod = typeof(Dictionary<string, object>.KeyCollection.Enumerator).GetMethod("MoveNext");
            generator.ForEach(locCurrent, typeof(string), typeof(Dictionary<string, object>.KeyCollection.Enumerator), locEnumerator, getCurrentMethod);

            generator.Ldloc(dataRow);
            generator.Ldloc(locCurrent);

            generator.Ldloc(locMemberValues);
            generator.Ldloc(locCurrent);
            generator.Call(typeof(Dictionary<string, object>).GetMethod("get_Item"));

            generator.Call(typeof(DataRow).GetMethod("set_Item", new Type[] { typeof(string), typeof(object) }));
            generator.EndForEach(moveNextMethod);
            generator.Ldloc(dataRow);
            generator.EndIf();
        }
Exemplo n.º 4
0
 public void BeginSetMembers(CodeGenerator generator, ArgBuilder targetObject)
 {
     locMemberValues = generator.DeclareLocal(typeof(Dictionary<string, object>), "memberValues");
     locDataTable = generator.DeclareLocal(typeof(DataTable), "dataTable");
     generator.New(typeof(Dictionary<string, object>).GetConstructor(Type.EmptyTypes));
     generator.Stloc(locMemberValues);
     generator.New(typeof(DataTable).GetConstructor(Type.EmptyTypes));
     generator.Stloc(locDataTable);
 }
Exemplo n.º 5
0
        public void GetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, ref LocalBuilder memberValue)
        {
            LocalBuilder locValue = generator.DeclareLocal(typeof(string));
            Type targetType = memberValue.LocalType;
            Type originalType = ConvertorGeneratorHelper.GetOriginalType(targetType);

            generator.Ldarg(targetObject);
            generator.Load(memberName);
            generator.Call(typeof(NameValueCollection).GetMethod("get_Item", new Type[] { typeof(string) }));
            generator.Stloc(locValue);

            if (targetType.IsAssignableFrom(typeof(string)))
            {
                generator.Ldloc(locValue);
                generator.Stloc(memberValue);
                return;
            }
            else
            {
                generator.Ldloc(locValue);
                generator.Call(typeof(string).GetMethod("IsNullOrEmpty"));
                generator.Load(true);
                generator.If(Cmp.NotEqualTo);
                if (targetType == typeof(Guid))
                {
                    generator.Ldloc(locValue);
                    generator.New(typeof(Guid).GetConstructor(new Type[] { typeof(string) }));
                    generator.Stloc(memberValue);
                }
                else
                {
                    MethodInfo parseMethod = targetType.GetMethod("Parse", new Type[] { typeof(string) });
                    if (parseMethod != null)
                    {
                        generator.Ldloc(locValue);
                        generator.Call(parseMethod);
                        generator.Stloc(memberValue);
                    }
                    else
                    {
                        generator.Ldloc(locValue);
                        generator.Load(originalType);
                        generator.Call(typeof(Convert).GetMethod("ChangeType", new Type[] { typeof(object), typeof(Type) }));
                        if (targetType.IsValueType)
                        {
                            generator.UnboxAny(targetType);
                        }
                        generator.Stloc(memberValue);
                    }
                }
                generator.Else();
                generator.LoadDefaultValue(targetType);
                generator.Stloc(memberValue);
                generator.EndIf();
            }
        }
Exemplo n.º 6
0
 public void SetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, LocalBuilder memberValue)
 {
     generator.Ldarg(targetObject);
     generator.Load(memberName);
     generator.Ldloc(memberValue);
     if (memberValue.LocalType.IsValueType)
     {
         generator.Box(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType);
     }
     generator.Call(typeof(IDictionary).GetMethod("Add"));
 }
Exemplo n.º 7
0
        public void SetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, LocalBuilder memberValue)
        {
            generator.Ldarg(targetObject);
            if (memberValue.LocalType.FullName.StartsWith("System.Nullable`1["))
            {
                generator.Ldloc(memberValue);
                generator.Load(null);
                generator.If(Cmp.EqualTo);
                generator.LoadMember(typeof(DBNull).GetField("Value"));
                generator.Stloc(memberValue);
                generator.EndIf();
            }
            generator.Load(null);

            generator.If(Cmp.EqualTo);
            PropertyInfo columns = typeof(DataTable).GetProperty("Columns");
            generator.Ldloc(locDataTable);
            generator.LoadMember(columns);
            generator.Load(memberName);
            generator.Ldtoken(CommonUtils.GetOriginalTypeOfNullableType(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType));
            generator.Call(typeof(Type).GetMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) }));
            generator.Call(typeof(DataColumnCollection).GetMethod("Add", new Type[] { typeof(string), typeof(Type) }));
            generator.Pop();

            generator.Ldloc(locMemberValues);
            generator.Load(memberName);
            generator.Ldloc(memberValue);
            if (memberValue.LocalType.IsValueType)
            {
                generator.Box(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType);
            }
            generator.Call(typeof(Dictionary<string, object>).GetMethod("Add"));
            generator.Else();
            generator.Ldarg(targetObject);
            generator.Load(memberName);
            generator.Ldloc(memberValue);
            if (memberValue.LocalType.IsValueType)
            {
                generator.Box(memberValue.LocalType);
            }
            generator.Call(typeof(DataRow).GetMethod("set_Item", new Type[] { typeof(string), typeof(object) }));
            generator.EndIf();
        }
 internal JsonFormatClassWriterDelegate GenerateClassWriter(ClassDataContract classContract)
 {
     _ilg = new CodeGenerator();
     bool memberAccessFlag = classContract.RequiresMemberAccessForWrite(null, JsonGlobals.JsonSerializationPatterns);
     try
     {
         BeginMethod(_ilg, "Write" + DataContract.SanitizeTypeName(classContract.StableName.Name) + "ToJson", typeof(JsonFormatClassWriterDelegate), memberAccessFlag);
     }
     catch (SecurityException securityException)
     {
         if (memberAccessFlag)
         {
             classContract.RequiresMemberAccessForWrite(securityException, JsonGlobals.JsonSerializationPatterns);
         }
         else
         {
             throw;
         }
     }
     InitArgs(classContract.UnderlyingType);
     _memberNamesArg = _ilg.GetArg(4);
     WriteClass(classContract);
     return (JsonFormatClassWriterDelegate)_ilg.EndMethod();
 }
            private void InitArgs(Type objType)
            {
                _xmlWriterArg = _ilg.GetArg(0);
                _contextArg = _ilg.GetArg(2);
                _dataContractArg = _ilg.GetArg(3);

                _objectLocal = _ilg.DeclareLocal(objType, "objSerialized");
                ArgBuilder objectArg = _ilg.GetArg(1);
                _ilg.Load(objectArg);

                // Copy the data from the DataTimeOffset object passed in to the DateTimeOffsetAdapter.
                // DateTimeOffsetAdapter is used here for serialization purposes to bypass the ISerializable implementation
                // on DateTimeOffset; which does not work in partial trust.

                if (objType == Globals.TypeOfDateTimeOffsetAdapter)
                {
                    _ilg.ConvertValue(objectArg.ArgType, Globals.TypeOfDateTimeOffset);
                    _ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetAdapterMethod);
                }
                //Copy the KeyValuePair<K,T> to a KeyValuePairAdapter<K,T>. 
                else if (objType.GetTypeInfo().IsGenericType && objType.GetGenericTypeDefinition() == Globals.TypeOfKeyValuePairAdapter)
                {
                    ClassDataContract dc = (ClassDataContract)DataContract.GetDataContract(objType);
                    _ilg.ConvertValue(objectArg.ArgType, Globals.TypeOfKeyValuePair.MakeGenericType(dc.KeyValuePairGenericArguments));
                    _ilg.New(dc.KeyValuePairAdapterConstructorInfo);
                }
                else
                {
                    _ilg.ConvertValue(objectArg.ArgType, objType);
                }
                _ilg.Stloc(_objectLocal);
            }
Exemplo n.º 10
0
 private CodeGenerator GenerateCollectionReaderHelper(CollectionDataContract collectionContract, bool isGetOnlyCollection)
 {
     _ilg = new CodeGenerator();
     bool memberAccessFlag = collectionContract.RequiresMemberAccessForRead(null);
     try
     {
         if (isGetOnlyCollection)
         {
             BeginMethod(_ilg, "Read" + DataContract.SanitizeTypeName(collectionContract.StableName.Name) + "FromJson" + "IsGetOnly", typeof(JsonFormatGetOnlyCollectionReaderDelegate), memberAccessFlag);
         }
         else
         {
             BeginMethod(_ilg, "Read" + DataContract.SanitizeTypeName(collectionContract.StableName.Name) + "FromJson", typeof(JsonFormatCollectionReaderDelegate), memberAccessFlag);
         }
     }
     catch (SecurityException securityException)
     {
         if (memberAccessFlag)
         {
             collectionContract.RequiresMemberAccessForRead(securityException);
         }
         else
         {
             throw;
         }
     }
     InitArgs();
     _collectionContractArg = _ilg.GetArg(4);
     return _ilg;
 }
Exemplo n.º 11
0
 public void SetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, LocalBuilder memberValue)
 {
     MemberInfo memberInfo = ConvertorGeneratorHelper.GetMemberInfo(targetObject.ArgType, memberName);
     if (memberInfo != null)
     {
         generator.Ldarg(targetObject);
         generator.Ldloc(memberValue);
         generator.StoreMember(memberInfo);
     }
 }
Exemplo n.º 12
0
        public void GetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, ref LocalBuilder memberValue)
        {
            Type inputType = targetObject.ArgType;
            MemberInfo memberInfo = ConvertorGeneratorHelper.GetMemberInfo(inputType, memberName);
            if (memberInfo == null)
            {
                return;
            }
            Type srcMemberType = ConvertorGeneratorHelper.GetMemberType(memberInfo);
            Type destMemberType = memberValue.LocalType;
            if (memberValue.LocalType.IsAssignableFrom(srcMemberType))
            {
                generator.Ldarg(targetObject);
                generator.LoadMember(memberInfo);
                generator.Stloc(memberValue);
                return;
            }

            Type converTargetType = ConvertorGeneratorHelper.GetOriginalType(destMemberType);

            Type convertSourceType = ConvertorGeneratorHelper.GetOriginalType(srcMemberType);

            //ת������
            if (typeof(IConvertible).IsAssignableFrom(convertSourceType))
            {
                generator.Ldarg(targetObject);
                generator.LoadMember(memberInfo);
                if (convertSourceType.IsValueType)
                {
                    generator.Box(srcMemberType);
                }
                //�Ƿ�Ϊ��
                generator.Load(null);
                generator.If(Cmp.NotEqualTo);
                //�����Ϊ�գ���������ת��
                generator.Ldarg(targetObject);
                generator.LoadMember(memberInfo);
                if (convertSourceType.IsValueType)
                {
                    generator.Box(srcMemberType);
                }
                generator.Load(converTargetType);
                generator.Call(typeof(Convert).GetMethod("ChangeType", new Type[] { typeof(object), typeof(Type) }));
                if (destMemberType.IsValueType)
                {
                    generator.UnboxAny(destMemberType);
                }
                generator.Stloc(memberValue);
                //���Ϊ�գ������Ĭ��ֵ
                generator.Else();
                generator.LoadDefaultValue(destMemberType);
                generator.Stloc(memberValue);
                generator.EndIf();
            }
        }
 CodeGenerator GenerateCollectionReaderHelper(CollectionDataContract collectionContract, bool isGetOnlyCollection)
 {
     ilg = new CodeGenerator();
     bool memberAccessFlag = collectionContract.RequiresMemberAccessForRead(null);
     try
     {
         if (isGetOnlyCollection)
         {
             BeginMethod(ilg, "Read" + collectionContract.StableName.Name + "FromJson" + "IsGetOnly", typeof(JsonFormatGetOnlyCollectionReaderDelegate), memberAccessFlag);
         }
         else
         {
             BeginMethod(ilg, "Read" + collectionContract.StableName.Name + "FromJson", typeof(JsonFormatCollectionReaderDelegate), memberAccessFlag);
         }
     }
     catch (SecurityException securityException)
     {
         if (memberAccessFlag && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
         {
             collectionContract.RequiresMemberAccessForRead(securityException);
         }
         else
         {
             throw;
         }
     }
     InitArgs();
     DemandMemberAccessPermission(memberAccessFlag);
     collectionContractArg = ilg.GetArg(4);
     return ilg;
 }
 void InitArgs()
 {
     xmlReaderArg = ilg.GetArg(0);
     contextArg = ilg.GetArg(1);
     emptyDictionaryStringArg = ilg.GetArg(2);
     memberNamesArg = ilg.GetArg(3);
 }
Exemplo n.º 15
0
        public void GetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, ref LocalBuilder memberValue)
        {
            MethodInfo getMethod;
            bool mustBeUnBox = false;

            switch (Type.GetTypeCode(memberValue.LocalType))
            {
                case TypeCode.Boolean:
                    getMethod = typeof(IDataRecord).GetMethod("GetBoolean");
                    break;
                case TypeCode.Byte:
                    getMethod = typeof(IDataRecord).GetMethod("GetByte");
                    break;
                case TypeCode.Char:
                    getMethod = typeof(IDataRecord).GetMethod("GetChar");
                    break;
                case TypeCode.DateTime:
                    getMethod = typeof(IDataRecord).GetMethod("GetDateTime");
                    break;
                case TypeCode.Decimal:
                    getMethod = typeof(IDataRecord).GetMethod("GetDecimal");
                    break;
                //Teddy 2008-2-2: commented the lines below, because for some database, return data type of float or real could always be System.Single or System.Double
                //case TypeCode.Double:
                //    getMethod = typeof(IDataRecord).GetMethod("GetDouble");
                //    break;
                //case TypeCode.Single:
                //    getMethod = typeof(IDataRecord).GetMethod("GetFloat");
                //    break;
                case TypeCode.Int16:
                    getMethod = typeof(IDataRecord).GetMethod("GetInt16");
                    break;
                case TypeCode.Int32:
                    getMethod = typeof(IDataRecord).GetMethod("GetInt32");
                    break;
                case TypeCode.Int64:
                    getMethod = typeof(IDataRecord).GetMethod("GetInt64");
                    break;
                case TypeCode.String:
                    getMethod = typeof(IDataRecord).GetMethod("GetString");
                    break;
                default:
                    getMethod = typeof(IDataRecord).GetMethod("GetValue");
                    mustBeUnBox = true;
                    break;
            }
            if (memberValue.LocalType == typeof(Guid))
            {
                getMethod = typeof(IDataRecord).GetMethod("GetGuid");
                mustBeUnBox = false;
            }
            if (memberValue.LocalType.IsEnum)
            {
                getMethod = typeof(IDataRecord).GetMethod("GetValue");
                mustBeUnBox = true;
            }

            generator.Ldarg(targetObject);
            generator.Load(memberName);
            generator.Call(typeof(IDataRecord).GetMethod("GetOrdinal"));
            LocalBuilder fieldOrdinalLoc = generator.DeclareLocal(typeof(int));
            generator.Stloc(fieldOrdinalLoc);
            generator.Ldarg(targetObject);
            generator.Ldloc(fieldOrdinalLoc);
            generator.Call(typeof(IDataRecord).GetMethod("IsDBNull"));
            generator.IfNot();
            generator.Ldarg(targetObject);
            generator.Ldloc(fieldOrdinalLoc);
            generator.Call(getMethod);

            //Teddy 2008-2-2: added the  code section below to convert the object to the memberValue.LocalType
            if (!memberValue.LocalType.IsEnum && getMethod == typeof(IDataRecord).GetMethod("GetValue"))
            {
                generator.Load(Type.GetTypeCode(memberValue.LocalType));
                generator.Call(typeof(Convert).GetMethod("ChangeType", BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(object), typeof(TypeCode) }, null));
            }

            if (memberValue.LocalType.IsValueType && mustBeUnBox)
            {
                generator.UnboxAny(memberValue.LocalType);
            }
            generator.Else();
            generator.LoadDefaultValue(memberValue.LocalType);
            generator.EndIf();
            generator.Stloc(memberValue);
        }
 void IsStartElement(ArgBuilder nameArg, ArgBuilder nsArg)
 {
     ilg.Call(xmlReaderArg, JsonFormatGeneratorStatics.IsStartElementMethod2, nameArg, nsArg);
 }
Exemplo n.º 17
0
 public void EndSetMembers(CodeGenerator generator, ArgBuilder targetObject)
 {
 }
Exemplo n.º 18
0
        private static MethodTarget MakeByRefReducedMethodTarget(MethodTracker method, List<Parameter> parameters, ArgBuilder instanceBuilder, List<ArgBuilder> argBuilders)
        {
            List<Parameter> newParameters = new List<Parameter>();
            foreach (Parameter param in parameters) {
                ByRefParameter p = param as ByRefParameter;
                if (p != null) {
                    if (!p.IsOutOnly) {
                        newParameters.Add(p.MakeInParameter());
                    }
                } else {
                    newParameters.Add(param);
                }
            }

            List<int> returnArgs = new List<int>();
            if (CompilerHelpers.GetReturnType(method.Method) != typeof(void)) {
                returnArgs.Add(-1);
            }
            int index = 0;
            int outParams = 0;
            List<ArgBuilder> newArgBuilders = new List<ArgBuilder>();
            foreach (ArgBuilder ab in argBuilders) {
                ReferenceArgBuilder rab = ab as ReferenceArgBuilder;
                if (rab != null) {
                    returnArgs.Add(index);
                    if (((ByRefParameter)rab.parameter).IsOutOnly) {
                        newArgBuilders.Add(new NullArgBuilder());
                        outParams++;
                    } else {
                        newArgBuilders.Add(new SimpleArgBuilder(rab.index - outParams, ((ByRefParameter)rab.parameter).MakeInParameter()));
                    }
                } else {
                    SimpleArgBuilder asb = ab as SimpleArgBuilder;
                    if (asb != null) {
                        newArgBuilders.Add(new SimpleArgBuilder(asb.index - outParams, asb.parameter));
                    } else {
                        newArgBuilders.Add(ab);
                    }
                }
                index++;
            }

            return new MethodTarget(method, newParameters, instanceBuilder, newArgBuilders,
                new ByRefReturnBuilder(CompilerHelpers.GetReturnType(method.Method), returnArgs));
        }
            void InitArgs(Type objType)
            {
                xmlWriterArg = ilg.GetArg(0);
                contextArg = ilg.GetArg(2);
                dataContractArg = ilg.GetArg(3);

                objectLocal = ilg.DeclareLocal(objType, "objSerialized");
                ArgBuilder objectArg = ilg.GetArg(1);
                ilg.Load(objectArg);

                // Copy the data from the DataTimeOffset object passed in to the DateTimeOffsetAdapter.
                // DateTimeOffsetAdapter is used here for serialization purposes to bypass the ISerializable implementation
                // on DateTimeOffset; which does not work in partial trust.

                if (objType == Globals.TypeOfDateTimeOffsetAdapter)
                {
                    ilg.ConvertValue(objectArg.ArgType, Globals.TypeOfDateTimeOffset);
                    ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetAdapterMethod);
                }
                else
                {
                    ilg.ConvertValue(objectArg.ArgType, objType);
                }
                ilg.Stloc(objectLocal);
            }
Exemplo n.º 20
0
        public void GetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, ref LocalBuilder memberValue)
        {
            LocalBuilder tempLocal = generator.DeclareLocal(typeof(object), "dataColumn");
            Type targetType = memberValue.LocalType;
            Type originalType = ConvertorGeneratorHelper.GetOriginalType(targetType);

            generator.Ldarg(targetObject);
            generator.Load(memberName);
            generator.Call(typeof(DataRow).GetMethod("get_Item", new Type[] { typeof(string) }));
            generator.Stloc(tempLocal);

            generator.Ldloc(tempLocal);
            generator.LoadMember(typeof(DBNull).GetField("Value", BindingFlags.Static | BindingFlags.Public));
            generator.If(Cmp.EqualTo);
            generator.LoadDefaultValue(targetType);
            generator.Stloc(memberValue);
            generator.Else();
            generator.Ldloc(tempLocal);

            generator.InternalILGenerator.Emit(OpCodes.Isinst, targetType);
            generator.InternalILGenerator.Emit(OpCodes.Ldnull);
            generator.InternalILGenerator.Emit(OpCodes.Cgt_Un);
            generator.Load(true);
            generator.If(Cmp.EqualTo);
            generator.Ldloc(tempLocal);
            if (targetType.IsValueType)
            {
                generator.UnboxAny(targetType);
            }
            generator.Stloc(memberValue);
            generator.Else();
            if (targetType == typeof(Guid))
            {
                generator.Ldloc(tempLocal);
                generator.InternalILGenerator.Emit(OpCodes.Isinst, typeof(string));
                generator.InternalILGenerator.Emit(OpCodes.Ldnull);
                generator.InternalILGenerator.Emit(OpCodes.Cgt_Un);
                generator.Load(true);
                generator.If(Cmp.EqualTo);
                generator.Ldloc(tempLocal);
                generator.New(typeof(Guid).GetConstructor(new Type[] { typeof(string) }));
                generator.Stloc(memberValue);
            }
            else
            {
                generator.Ldloc(tempLocal);
                generator.InternalILGenerator.Emit(OpCodes.Isinst, typeof(IConvertible));
                generator.InternalILGenerator.Emit(OpCodes.Ldnull);
                generator.InternalILGenerator.Emit(OpCodes.Cgt_Un);
                generator.Load(true);
                generator.If(Cmp.EqualTo);
                generator.Ldloc(tempLocal);
                generator.Load(originalType);
                generator.Call(typeof(Convert).GetMethod("ChangeType", new Type[] { typeof(object), typeof(Type) }));
                if (targetType.IsValueType)
                {
                    generator.UnboxAny(targetType);
                }
                generator.Stloc(memberValue);
            }
            generator.Else();
            generator.LoadDefaultValue(targetType);
            generator.Stloc(memberValue);
            generator.EndIf();
            generator.EndIf();
            generator.EndIf();
        }
Exemplo n.º 21
0
            public MethodTarget(MethodTracker method, List<Parameter> parameters, ArgBuilder instanceBuilder, List<ArgBuilder> argBuilders, ReturnBuilder returnBuilder)
            {
                this.method = method;
                this.parameters = parameters;
                this.instanceBuilder = instanceBuilder;
                this.argBuilders = argBuilders;
                this.returnBuilder = returnBuilder;

                parameters.TrimExcess();
                argBuilders.TrimExcess();
            }
 internal JsonFormatClassWriterDelegate GenerateClassWriter(ClassDataContract classContract)
 {
     ilg = new CodeGenerator();
     bool memberAccessFlag = classContract.RequiresMemberAccessForWrite(null);
     try
     {
         BeginMethod(ilg, "Write" + classContract.StableName.Name + "ToJson", typeof(JsonFormatClassWriterDelegate), memberAccessFlag);
     }
     catch (SecurityException securityException)
     {
         if (memberAccessFlag && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
         {
             classContract.RequiresMemberAccessForWrite(securityException);
         }
         else
         {
             throw;
         }
     }
     InitArgs(classContract.UnderlyingType);
     memberNamesArg = ilg.GetArg(4);
     DemandSerializationFormatterPermission(classContract);
     DemandMemberAccessPermission(memberAccessFlag);
     if (classContract.IsReadOnlyContract)
     {
         ThrowIfCannotSerializeReadOnlyTypes(classContract);
     }
     WriteClass(classContract);
     return (JsonFormatClassWriterDelegate)ilg.EndMethod();
 }
Exemplo n.º 23
0
 private void InitArgs()
 {
     _xmlReaderArg = _ilg.GetArg(0);
     _contextArg = _ilg.GetArg(1);
     _emptyDictionaryStringArg = _ilg.GetArg(2);
     _memberNamesArg = _ilg.GetArg(3);
 }