public void Construction_TypeDescriptor_ResultIsDocumentAreEqual()
        {
            CsvDocumentAttribute document = new CsvDocumentAttribute();
            TypeDescriptor       actual   = new TypeDescriptor(document, new ItemDescriptor[0]);

            Assert.AreEqual(document, actual.Document);
        }
Beispiel #2
0
        /// <summary>
        /// This method tries to parse assigned type <typeparamref name="TInstance"/>
        /// and returns an appropriate type descriptor in case of success.
        /// </summary>
        /// <remarks>
        /// Task of this method is to filter attribute information from given type by
        /// using reflection and by applying current filter flags.
        /// </remarks>
        /// <typeparam name="TInstance">
        /// The type to be parsed.
        /// </typeparam>
        /// <returns>
        /// An instance of class <see cref="TypeDescriptor"/>.
        /// </returns>
        /// <exception cref="NotSupportedException">
        /// This exception is thrown in case of column ordering rules have been
        /// violated.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// This exception might be thrown in any other case, for example type
        /// parsing has caused an unexpected and/or an unhandled issue. In such
        /// a case the inner exception contains more details.
        /// </exception>
        internal static TypeDescriptor LoadDescriptor <TInstance>() where TInstance : class
        {
            try
            {
                CsvDocumentAttribute  document = TypeProcessor.GetDocumentAttribute(typeof(TInstance)) ?? new CsvDocumentAttribute();
                List <ItemDescriptor> settings = new List <ItemDescriptor>();

                PropertyInfo[] properties = typeof(TInstance).GetProperties(TypeProcessor.flags);

                if (properties != null)
                {
                    foreach (PropertyInfo property in properties)
                    {
                        IEnumerable <Attribute> attributes = property.GetCustomAttributes();

                        if (attributes != null)
                        {
                            foreach (Attribute current in attributes)
                            {
                                if (current is CsvColumnAttribute)
                                {
                                    settings.Add(new ItemDescriptor(current as CsvColumnAttribute, property));
                                    break;
                                }
                            }
                        }
                    }
                }

                return(new TypeDescriptor(document, TypeProcessor.ConfirmAndReorganize(settings)));
            }
            catch (NotSupportedException)
            {
                throw;
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Unable to load CSV descriptor for type {typeof(TInstance).Name}. See inner exception for more details.", exception);
            }
        }
 /// <summary>
 /// The parameterized class constructor.
 /// </summary>
 /// <remarks>
 /// This constructor initializes an instance of this class with the
 /// characteristics of a CSV definition class.
 /// </remarks>
 /// <param name="document">
 /// An instance of class <see cref="CsvDocumentAttribute"/> that describes
 /// the characteristics of a CSV document.
 /// </param>
 /// <param name="settings">
 /// A list of instances of class <see cref="ItemDescriptor"/> that contains
 /// the characteristics of all affected CSV columns.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// This exception is thrown if one of the parameters is &lt;null&gt;.
 /// </exception>
 public TypeDescriptor(CsvDocumentAttribute document, IEnumerable <ItemDescriptor> settings)
     : base()
 {
     this.Document = document ?? throw new ArgumentNullException(nameof(document));
     this.Settings = settings ?? throw new ArgumentNullException(nameof(settings));
 }