Exemplo n.º 1
0
        /// <summary>
        /// Converts the block into an object.
        /// </summary>
        /// <param name="type">The type of object to convert to.</param>
        /// <returns>The new object.</returns>
        public object ConvertTo(Type type)
        {
            if (type == null)
            {
                return(null);
            }

            object theObject = Activator.CreateInstance(type);

            for (int i = 0; i < this.Size; i++)
            {
                string      label  = this.GetLabel(i);
                ValueSetter setter = ValueSetter.GetValueSetter(theObject, label);

                if (setter.CanSetValue())
                {
                    setter.SetFormattedValue(this.GetValue(i));
                }
                else
                {
                    throw new Exception(string.Format("Could not set {0} on type {1}.", label, type.FullName));
                }
            }

            return(theObject);
        }
Exemplo n.º 2
0
        private object CreateItem(Type itemType, int rowIndex)
        {
            object item = Activator.CreateInstance(itemType);

            for (int columnIndex = 0; columnIndex < this.ColumnCount; columnIndex++)
            {
                string      header = this.GetHeader(columnIndex);
                ValueSetter setter = ValueSetter.GetValueSetter(item, header);

                if (setter.CanSetValue())
                {
                    setter.SetFormattedValue(this.GetValue(rowIndex, columnIndex));
                }
                else
                {
                    throw new Exception(string.Format("Could not set {0} on type {1}.", header, itemType.FullName));
                }
            }

            return(item);
        }