コード例 #1
0
        /// <summary>
        /// Set the user-defined options (or replace the default settings with the user-defined values)
        /// </summary>
        /// <param name="options"></param>
        public virtual void SetOptions(ConverterOptionList options)
        {
            if (options == null || options.Count == 0)
            {
                return;
            }

            #region Cast the value element into its underlying type
            var pis = new List <PropertyInfo>(this.Options.GetType().GetProperties());

            foreach (ConverterOption item in options)
            {
                var mypis = pis.FindAll(pi => pi.Name == item.Key);
                if (mypis.Count != 1)
                {
                    throw new ArgumentException(string.Format("Expected one property to match key of '{0}'; but, found {2}", item.Key, item.Value, mypis.Count));
                }
                Type t = mypis[0].PropertyType.UnderlyingSystemType;
                if (item.Value.GetType() != t)
                {
                    item.Value = Convert.ChangeType(item.Value, t);
                }
            }
            #endregion


            DynamicCode.SetProperty(this.Options, options.ToHashTable());
        }
コード例 #2
0
 public static void Export(DataSet ds, string filename, ConverterOptionList options)
 {
     try
     {
         IFileConverter converter = GetConverter(filename, options);
         converter.Export(ds, filename);
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         DebuggerTool.AddData(ex, "filename", filename);
         DebuggerTool.AddData(ex, "options", options);
         throw;
     }
 }
コード例 #3
0
 public static DataSet Import(string filename, ConverterOptionList options)
 {
     try
     {
         IFileConverter converter = GetConverter(filename, options);
         return(converter.Import(filename));
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         DebuggerTool.AddData(ex, "filename", filename);
         DebuggerTool.AddData(ex, "options", options);
         throw;
     }
 }
コード例 #4
0
        /// <summary>
        /// Get the converter engine for filename
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private static IFileConverter GetConverter(string filename, ConverterOptionList options)
        {
            try
            {
                // return the extension name (w/o the dot)
                string ext = Path.GetExtension(filename).Substring(1);
                if (string.IsNullOrEmpty(ext))
                {
                    throw new ArgumentException("File does not have an extension: " + filename);
                }

                var type = converters.GetType(ext);
                if (type == null)
                {
                    throw new ArgumentException("Extension not supported: " + ext);
                }

                var importOptionsType = converters.GetImportOptionsType(ext);

                var converterBase = Activator.CreateInstance(type);
                if (converterBase == null)
                {
                    throw new ApplicationException("Cannot create a new instance of this type: " + type.FullName);
                }

                var converter = (IFileConverter)converterBase;
                if (converter == null)
                {
                    throw new ApplicationException("This converter does not implement IFileConverter: " + type.FullName);
                }

                converter.SetOptions(options);

                return(converter);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw;
            }
        }