Beispiel #1
0
        private void AddAssemblies(IEnumerable <FileInfo> files)
        {
            var bufs = new List <MemoryStream>();

            foreach (var f in files)
            {
                var buf = new MemoryStream();
                using (var stream = f.OpenRead())
                {
                    while (true)
                    {
                        int b = stream.ReadByte();
                        if (b < 0)
                        {
                            break;
                        }
                        else
                        {
                            buf.WriteByte((byte)b);
                        }
                    }
                }

                buf.Position = 0;

                bufs.Add(buf);
            }

            System.Threading.ThreadPool.QueueUserWorkItem(delegate
            {
                foreach (var buf in bufs)
                {
                    try
                    {
                        var para = new Mi.Assemblies.ReaderParameters(Mi.Assemblies.ReadingMode.Immediate);
                        Mi.Assemblies.AssemblyDefinition asm;
                        para.AssemblyResolver = new Resolver(
                            (asmName, _p) =>
                            assembies.FirstOrDefault(
                                loaded =>
                                string.Equals(
                                    loaded.FullName,
                                    asmName.FullName,
                                    StringComparison.OrdinalIgnoreCase)));

                        asm = Mi.Assemblies.AssemblyDefinition.ReadAssembly(buf, para);

                        this.assembies.Add(asm);

                        this.Dispatcher.BeginInvoke(delegate
                        {
                            var asmNode = new TreeViewItem
                            {
                                Header = asm.Name.Name + " v" + asm.Name.Version
                            };
                            this.treeView1.Items.Add(asmNode);

                            var grouped =
                                from t in asm.MainModule.Types
                                orderby t.Namespace, t.Name
                            group t by t.Namespace;

                            foreach (var g in grouped)
                            {
                                var nsNode = new TreeViewItem
                                {
                                    Header = string.IsNullOrEmpty(g.Key) ? "<>" : g.Key
                                };

                                asmNode.Items.Add(nsNode);

                                foreach (var t in g)
                                {
                                    var tNode = new TreeViewItem
                                    {
                                        Header = t.Name,
                                        Tag    = t
                                    };

                                    nsNode.Items.Add(tNode);
                                }
                            }

                            openButton.IsEnabled = true;
                        });
                    }
                    catch (Exception error)
                    {
                        this.Dispatcher.BeginInvoke(delegate
                        {
                            openButton.IsEnabled = true;
                            var rn = new Run {
                                Text = error.ToString()
                            };
                            var pa = new Paragraph();
                            pa.Inlines.Add(rn);

                            codeTextBox.Blocks.Clear();
                            codeTextBox.Blocks.Add(pa);
                        });
                    }
                }
            });
        }
        private void AddAssemblies(IEnumerable<FileInfo> files)
        {
            var bufs = new List<MemoryStream>();
            foreach (var f in files)
            {
                var buf = new MemoryStream();
                using (var stream = f.OpenRead())
                {
                    while (true)
                    {
                        int b = stream.ReadByte();
                        if (b < 0)
                            break;
                        else
                            buf.WriteByte((byte)b);
                    }
                }

                buf.Position = 0;

                bufs.Add(buf);
            }

            System.Threading.ThreadPool.QueueUserWorkItem(delegate
            {
                foreach (var buf in bufs)
                {
                    try
                    {
                        var para = new Mi.Assemblies.ReaderParameters(Mi.Assemblies.ReadingMode.Immediate);
                        Mi.Assemblies.AssemblyDefinition asm;
                        para.AssemblyResolver = new Resolver(
                            (asmName, _p) =>
                                assembies.FirstOrDefault(
                                    loaded =>
                                        string.Equals(
                                        loaded.FullName,
                                        asmName.FullName,
                                        StringComparison.OrdinalIgnoreCase)));

                        asm = Mi.Assemblies.AssemblyDefinition.ReadAssembly(buf, para);

                        this.assembies.Add(asm);

                        this.Dispatcher.BeginInvoke(delegate
                        {
                            var asmNode = new TreeViewItem
                            {
                                Header = asm.Name.Name + " v" + asm.Name.Version
                            };
                            this.treeView1.Items.Add(asmNode);

                            var grouped =
                                from t in asm.MainModule.Types
                                orderby t.Namespace, t.Name
                                group t by t.Namespace;

                            foreach (var g in grouped)
                            {
                                var nsNode = new TreeViewItem
                                {
                                    Header = string.IsNullOrEmpty(g.Key) ? "<>" : g.Key
                                };

                                asmNode.Items.Add(nsNode);

                                foreach (var t in g)
                                {
                                    var tNode = new TreeViewItem
                                    {
                                        Header = t.Name,
                                        Tag = t
                                    };

                                    nsNode.Items.Add(tNode);
                                }
                            }

                            openButton.IsEnabled = true;
                        });
                    }
                    catch (Exception error)
                    {
                        this.Dispatcher.BeginInvoke(delegate
                        {
                            openButton.IsEnabled = true;
                            var rn = new Run { Text = error.ToString() };
                            var pa = new Paragraph();
                            pa.Inlines.Add(rn);

                            codeTextBox.Blocks.Clear();
                            codeTextBox.Blocks.Add(pa);
                        });
                    }
                }
            });
        }
 public static ModuleDefinition ReadModule(string fileName, ReaderParameters parameters)
 {
     using (var stream = GetFileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
         return(ReadModule(stream, parameters));
     }
 }