Exemple #1
0
        private static void EmitDeserializeUserType(DataReaderMetadata dataReaderMetadata, FieldMap fieldMap, ILGenerator il, Type type, string[] columnNames, Type[] columnTypes)
        {
            var constructor = type.GetConstructor(Type.EmptyTypes);

            if (constructor == null)
            {
                ThrowException.NoDefaultConstructor();
            }

            var sample = Activator.CreateInstance(type);

            il.Emit(OpCodes.Newobj, constructor);

            var uniqueNames = new HashSet <string>();

            for (var ordinal = 0; ordinal < columnNames.Length; ordinal++)
            {
                var propertyInfo = type.GetProperty(columnNames[ordinal], BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                if (propertyInfo == null)
                {
                    continue;
                }

                var getValueMethod = dataReaderMetadata.GetGetValueMethodFromType(columnTypes[ordinal]);

                if (propertyInfo.CanWrite && propertyInfo.GetSetMethod() != null)
                {
                    var propertyIsNullInitialized           = propertyInfo.CanRead && propertyInfo.GetValue(sample) == null;
                    var hasRequiredAttribute                = PropertyHasRequiredAttribute(propertyInfo);
                    var dataReaderPropertyValueDeserializer = new DataReaderValueDeserializer(dataReaderMetadata, getValueMethod, propertyInfo, ordinal);
                    dataReaderPropertyValueDeserializer.EmitDeserializeProperty(il, fieldMap, propertyIsNullInitialized, hasRequiredAttribute);

                    if (uniqueNames.Add(propertyInfo.Name) == false)
                    {
                        ThrowException.DuplicateFieldNames(propertyInfo.Name);
                    }

                    continue;
                }

                var fieldInfo = type.GetField("<" + propertyInfo.Name + ">k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);
                if (fieldInfo != null)
                {
                    var fieldIsNullInitialized           = fieldInfo.GetValue(sample) == null;
                    var hasRequiredAttribute             = PropertyHasRequiredAttribute(propertyInfo);
                    var dataReaderFieldValueDeserializer = new DataReaderValueDeserializer(dataReaderMetadata, getValueMethod, fieldInfo, ordinal);
                    dataReaderFieldValueDeserializer.EmitDeserializeProperty(il, fieldMap, fieldIsNullInitialized, hasRequiredAttribute);

                    if (uniqueNames.Add(propertyInfo.Name) == false)
                    {
                        ThrowException.DuplicateFieldNames(propertyInfo.Name);
                    }
                }
            }
        }
Exemple #2
0
        private static void EmitDeserializeClrType(DataReaderMetadata dataReaderMetadata, FieldMap fieldMap, ILGenerator il, Type sourceType, Type type)
        {
            var getValueMethod = dataReaderMetadata.GetGetValueMethodFromType(sourceType);

            new DataReaderValueDeserializer(dataReaderMetadata, getValueMethod, type, 0).EmitDeserializeClrType(il, fieldMap, false);
        }