Beispiel #1
0
        private Dictionary<string, object> GetContents(IAnNaSpreadSheetParser10 parser)
        {
            parser.OpenFile(openFileDialog.FileName);

            var type = typeof(ISheet);
            var types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(p => !p.IsAbstract
                            && type.IsAssignableFrom(p)
                            && p != typeof(ISheet)).ToList();

            var everything = new Dictionary<string, object>();
            foreach (var t in types)
            {

                bool isTyped = t.GetInterfaces().Any(x =>
                  x.IsGenericType &&
                  x.GetGenericTypeDefinition() == typeof(ITypedSheetWithBulkData<>));

                var instance = Activator.CreateInstance(t);
                object contents = null;
                if (isTyped)
                {
                    MethodInfo method = typeof(IAnNaSpreadSheetParser10).GetMethods().First(m => m.Name == nameof(parser.GetSheetBulkData) && m.GetParameters().Count() == 1);
                    MethodInfo generic = method.MakeGenericMethod(
                                                    t.GetInterfaces()
                                                    .Where(i => i.IsGenericType
                                                        && i.GetGenericTypeDefinition() == typeof(ITypedSheetWithBulkData<>))
                                                    .Single().GetGenericArguments()
                                                    .Single());

                    contents = generic.Invoke(parser, new object[] { instance });
                }
                else
                {
                    contents = parser.GetSheetBulkData(instance as ISheetWithBulkData);
                }

                if (contents != null)
                {
                    everything[t.Name] = contents;
                }
            }

            return everything;
        }
Beispiel #2
0
 private void OpenFile(IAnNaSpreadSheetParser10 parser)
 {
     var result = openFileDialog.ShowDialog();
     if (!File.Exists(openFileDialog.FileName))
     {
         MessageBox.Show(@"File not found", @"File not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     else
     {
         object everything1 = GetContents(parser);
         var settings = new JsonSerializerSettings();
         settings.Formatting = Formatting.Indented;
         txtSerializedContents.Text = JsonConvert.SerializeObject(everything1);
     }
 }