Пример #1
0
        public ClassContentReader(
            IContentReaderCollection contentReaderCollection,
            IXmlAttributeInterpreter xmlAttributeInterpreter,
            OnDeserializeConfiguration onDeserializeConfiguration)
        {
            this.onDeserializeConfiguration = onDeserializeConfiguration;
            emitConstruction = ReadHelpers.EmitConstruction <T>();
            var infos =
                typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty);

            foreach (var propertyInfo in infos)
            {
                if (PropertyIsBad(propertyInfo))
                {
                    continue;
                }
                if (propertyInfo.IsDefined(typeof(XmlAttributeAttribute), false))
                {
                    var name = xmlAttributeInterpreter.GetXmlNodeName(propertyInfo);
                    attributeMap.Add(name, ReadHelpers.BuildSetter <T>(propertyInfo, contentReaderCollection));
                }
                else
                {
                    var name = xmlAttributeInterpreter.GetXmlNodeName(propertyInfo);
                    propertiesMap.Add(name, ReadHelpers.BuildSetter <T>(propertyInfo, contentReaderCollection));
                }
            }
        }
Пример #2
0
        private static IContentReader <T> TryCreateForArray <T>(IContentReaderCollection contentReaderCollection)
        {
            var type = typeof(T);

            if (!type.IsArray)
            {
                return(null);
            }
            var elementType = type.GetElementType();

            if (elementType != null)
            {
                if (elementType.IsArray)
                {
                    throw new NotSupportedException("массив массивов");
                }
                var arrayRank = type.GetArrayRank();
                if (arrayRank > 1)
                {
                    throw new NotSupportedException("многомерный массив");
                }
            }
            var genericType       = typeof(ArrayContentReader <>).MakeGenericType(elementType);
            var publicConstructor = genericType.GetPublicConstructor(typeof(IContentReaderCollection));
            var invoke            = publicConstructor.Invoke(new[] { contentReaderCollection });

            return((IContentReader <T>)invoke);
        }
Пример #3
0
        private static IContentReader <T> TryCreateForByteArray <T>(IContentReaderCollection contentReaderCollection)
        {
            var type = typeof(T);

            if (type != typeof(byte[]))
            {
                return(null);
            }
            return((IContentReader <T>) new ByteArrayContentReader());
        }
Пример #4
0
        private static IContentReader <T> TryCreateForList <T>(IContentReaderCollection contentReaderCollection)
        {
            var type = typeof(T);

            if (!type.IsList())
            {
                return(null);
            }
            var elementType       = type.GetListType();
            var genericType       = typeof(ListContentReader <>).MakeGenericType(elementType);
            var publicConstructor = genericType.GetPublicConstructor(typeof(IContentReaderCollection));
            var invoke            = publicConstructor.Invoke(new[] { contentReaderCollection });

            return((IContentReader <T>)invoke);
        }
Пример #5
0
        private static IContentReader <T> TryCreateForDict <T>(IContentReaderCollection contentReaderCollection)
        {
            var type = typeof(T);

            if (!type.IsDictionary())
            {
                return(null);
            }
            var keyType           = type.GetDictionaryKeyType();
            var valueType         = type.GetDictionaryValueType();
            var genericType       = typeof(DictionaryContentReader <,>).MakeGenericType(keyType, valueType);
            var publicConstructor = genericType.GetPublicConstructor(typeof(IContentReaderCollection));
            var invoke            = publicConstructor.Invoke(new[] { contentReaderCollection });

            return((IContentReader <T>)invoke);
        }
 public ReportReader(IContentReaderCollection contentReaderCollection)
 {
     this.contentReaderCollection = contentReaderCollection;
 }
 public ArrayContentReader(IContentReaderCollection contentReaderCollection)
 {
     itemReader = contentReaderCollection.Get <T>();
 }
Пример #8
0
 public DictionaryContentReader(IContentReaderCollection contentReaderCollection)
 {
     arrayContentReader = new ArrayContentReader <DictionaryKeyValuePair <TKey, TValue> >(contentReaderCollection);
 }
Пример #9
0
 public ListContentReader(IContentReaderCollection contentReaderCollection)
 {
     arrayContentReader = new ArrayContentReader <T>(contentReaderCollection);
 }
Пример #10
0
 public NullableContentReader(IContentReaderCollection contentReaderCollection)
 {
     contentReader = contentReaderCollection.Get <T>();
 }
Пример #11
0
        //TODO не работает если T - невидим из своей сборки
        public static IContentPropertySetter <T> BuildSetter <T>(PropertyInfo propertyInfo, IContentReaderCollection contentWriterCollection)
        {
            lock (lockObject)
            {
                var typeBuilder =
                    moduleBuilder.DefineType(
                        typeof(T).Name + "." + propertyInfo.Name + "_" + Interlocked.Increment(ref count),
                        TypeAttributes.Public | TypeAttributes.Class, null,
                        new[] { typeof(IContentPropertySetter <T>) });

                var concreteReader     = typeof(IContentReader <>).MakeGenericType(propertyInfo.PropertyType);
                var readMethod         = concreteReader.GetMethod("Read");
                var field              = typeBuilder.DefineField("contentReader", concreteReader, FieldAttributes.Private);
                var constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public,
                                                                       CallingConventions.Standard,
                                                                       new[]
                {
                    typeof(
                        IContentReaderCollection
                        )
                });

                var methodInfo  = typeof(IContentReaderCollection).GetMethod("Get");
                var getMethod   = methodInfo.MakeGenericMethod(propertyInfo.PropertyType);
                var ilGenerator = constructorBuilder.GetILGenerator();
                ilGenerator.Emit(OpCodes.Ldarg_0); //this
                ilGenerator.Emit(OpCodes.Ldarg_1); //collection
                ilGenerator.Emit(OpCodes.Callvirt, getMethod);
                ilGenerator.Emit(OpCodes.Stfld, field);
                ilGenerator.Emit(OpCodes.Ret);

                var setMethod = propertyInfo.GetSetMethod();
                if (setMethod == null)
                {
                    throw new MissingMemberException(propertyInfo.ReflectedType.FullName, propertyInfo.Name + ".set()");
                }

                var methodBuilder = typeBuilder.DefineMethod("SetProperty",
                                                             MethodAttributes.Public |
                                                             MethodAttributes.Virtual |
                                                             MethodAttributes.NewSlot,
                                                             typeof(void),
                                                             new[] { typeof(T), typeof(IReader) });
                ilGenerator = methodBuilder.GetILGenerator();
                ilGenerator.Emit(OpCodes.Ldarg_1); //target
                ilGenerator.Emit(OpCodes.Ldarg_0);
                ilGenerator.Emit(OpCodes.Ldfld, field);
                ilGenerator.Emit(OpCodes.Ldarg_2);
                ilGenerator.Emit(OpCodes.Callvirt, readMethod);
                ilGenerator.Emit(OpCodes.Callvirt, setMethod);
                ilGenerator.Emit(OpCodes.Ret);

                var type = typeBuilder.CreateTypeInfo();
                var publicConstructor = type.GetPublicConstructor(typeof(IContentReaderCollection));
                var result            = publicConstructor.Invoke(new[] { contentWriterCollection });
                return((IContentPropertySetter <T>)result);
            }
        }