⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reflectioninfoview.cs

📁 全功能c#编译器
💻 CS
📖 第 1 页 / 共 2 页
字号:
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Georg Brandl" email="g.brandl@gmx.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;

using ICSharpCode.SharpDevelop.Gui.HtmlControl;

using ICSharpCode.SharpDevelop.Services;
using SharpDevelop.Internal.Parser;
using ICSharpCode.Core.Services;

namespace ICSharpCode.SharpDevelop.Internal.Reflection
{
	public class ReflectionInfoView : UserControl
	{
		GradientLabel cap = new GradientLabel();
		Label typ = new Label();
		LinkLabel back = new LinkLabel();
		
		// old LinkLabel view
		//LinkLabel ll = new LinkLabel();
		
		// new HtmlControl view
		HtmlControl ht = new HtmlControl();
		Panel pan = new Panel();
		
		ReflectionTree tree;
		IParserService parserService;
		
		ArrayList references = new ArrayList();
		string cssPath;
		string imgPath;
		
		public ReflectionInfoView(ReflectionTree tree)
		{
			ResourceService resourceService = (ResourceService)ServiceManager.Services.GetService(typeof(ResourceService));
			this.tree = tree;
			
			imgPath = System.IO.Path.GetTempFileName() + ".png";
			cssPath = System.IO.Path.GetTempFileName() + ".css";
			
			Color col = SystemColors.Control;
			string color = "#" + col.R.ToString("X") + col.G.ToString("X") + col.B.ToString("X");
			
			StreamWriter sw = new StreamWriter(cssPath, false);
			sw.Write(@"body { margin: 0px; border: 0px; overflow: hidden; padding: 0px;
							  background-color: " + color + @"; 
							  background-image: url(" + imgPath + @"); 
							  background-position: bottom right; 
							  background-repeat: no-repeat }
					p { font: 8pt Tahoma }

					a { font: 8pt Tahoma; text-decoration: none; color: blue}
					a:visited { color: blue }
					a:active  { color: blue }
					a:hover   { color: red; text-decoration: underline }");
			sw.Close();
			
			cap.Location  = new Point(0, 0);
			cap.Size      = new Size(Width, 32);
			cap.Text      = tree.ress.GetString("ObjectBrowser.Welcome");
			cap.Font      = new Font("Tahoma", 14);
			cap.BackColor = SystemColors.ControlLight;
			cap.TextAlign = ContentAlignment.MiddleLeft;
			cap.Anchor    = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
						
			string backt  = tree.ress.GetString("ObjectBrowser.Back");
			back.Size     = new Size(40, 16);
			back.Location = new Point(Width - back.Width, 44);
			back.Text     = backt;
			back.TextAlign = ContentAlignment.TopRight;
			back.Anchor   = AnchorStyles.Top | AnchorStyles.Right;
			back.Links.Add(0, backt.Length);
			back.LinkClicked += new LinkLabelLinkClickedEventHandler(back_click);
			
			typ.Location  = new Point(0, 44);
			typ.Size      = new Size(Width - back.Width, 16);
			typ.Font      = new Font(Font, FontStyle.Bold);
			typ.Text      = tree.ress.GetString("ObjectBrowser.WelcomeText");
			typ.TextAlign = ContentAlignment.TopLeft;
			typ.Anchor    = cap.Anchor;
			/*			
			ll.Location   = new Point(0, 72);
			ll.Size       = new Size(Width, Height - ll.Top);
			ll.Anchor     = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
			ll.LinkClicked += new LinkLabelLinkClickedEventHandler(ll_click);
			ll.ImageAlign = ContentAlignment.BottomRight;
			ll.Text       = tree.ress.GetString("ObjectBrowser.Info.SelectNode");
			ll.Image      = CreateImage(resourceService.GetIcon("Icons.16x16.Class").ToBitmap());
			ll.LinkBehavior = LinkBehavior.HoverUnderline;
			ll.Links.Clear();
			*/
			ht = new HtmlControl();
			//ht.Size = new Size(20, 20);
			//ht.Location = new Point(20, 20);
			ht.BeforeNavigate += new BrowserNavigateEventHandler(HtmlControlBeforeNavigate);
			CreateImage(resourceService.GetIcon("Icons.16x16.Class").ToBitmap());
			ht.CascadingStyleSheet = cssPath;
			string html = RenderHead() + "<p>" + tree.ress.GetString("ObjectBrowser.Info.SelectNode") + RenderFoot();
			ht.Html = html;
			
			pan.Location   = new Point(0, 72);
			pan.DockPadding.Left = 10;
			pan.DockPadding.Bottom = 75;
			pan.Size       = new Size(Width, Height - ht.Top);
			pan.Anchor     = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
			pan.Controls.Add(ht);
			
			ht.Dock = DockStyle.Fill;
			
			Controls.AddRange(new Control[] {
				cap, typ, back, pan
			});
			
			Dock = DockStyle.Fill;
			tree.AfterSelect += new TreeViewEventHandler(SelectNode);
			
			parserService = (IParserService)ServiceManager.Services.GetService(typeof(IParserService));
		}
		
		~ReflectionInfoView() {
			System.IO.File.Delete(imgPath);
			System.IO.File.Delete(cssPath);
		}
		
		void HtmlControlBeforeNavigate(object sender, BrowserNavigateEventArgs e)
		{
			e.Cancel = true;
			
			try {
			
				string url = e.Url;
				int refnr  = Int32.Parse(url.Substring(5, url.Length - 6));
				object obj = references[refnr];
				
				if (obj is Type) {
					// Go To Type
					tree.GoToType((Type)obj);
					// try {
					// 	tree.SelectedNode.Expand();
					// } catch {}
				} else if (obj is AssemblyName) {
					// Open Assembly Reference
					tree.OpenAssemblyByName((AssemblyName)obj);
				} else if (obj is SaveResLink) {
					SaveResLink link = (SaveResLink)obj;
					tree.SaveResource(link.Asm, link.Name);
				} else if (obj is NamespaceLink) {
					NamespaceLink ns = (NamespaceLink)obj;
					tree.GoToNamespace(ns.Asm, ns.Name);
				}
			} catch { MessageBox.Show("Something failed following this link."); }
		}
		
		string RenderHead()
		{
			return "<div style='margin: 0px; width: 100%'><p>";
		}
		
		string RenderFoot()
		{
			return "</div>";
		}
		
		void SelectNode(object sender, TreeViewEventArgs e)
		{
			ReflectionNode node = (ReflectionNode)e.Node;
			cap.Text = node.Text;
			typ.Text = node.Type.ToString();
			
			references.Clear();
			CreateImage(tree.ImageList.Images[node.ImageIndex]);
			string html = RenderHead();
			
			try {
			
				switch(node.Type) {
					case ReflectionNodeType.Assembly:
						html += GetAssemblyInfo((Assembly)node.Attribute);
						break;
					case ReflectionNodeType.Library:
						html += GetLibInfo((Assembly)node.Attribute);
						break;
					case ReflectionNodeType.Reference:
						html += GetRefInfo((AssemblyName)node.Attribute);
						break;
					case ReflectionNodeType.Resource:
						html += GetResInfo((Assembly)node.Attribute, node.Text);
						break;
					case ReflectionNodeType.Module:
						html += GetModInfo((Module)node.Attribute);
						break;
					case ReflectionNodeType.Folder:
						html += GetFolderInfo(node.Text);
						break;
					case ReflectionNodeType.Namespace:
						html += GetNSInfo((Assembly)node.Attribute);
						break;
					case ReflectionNodeType.SubTypes:
						html += GetSubInfo();
						break;
					case ReflectionNodeType.SuperTypes:
						html += GetSuperInfo();
						break;
					case ReflectionNodeType.Link:
						html += GetLinkInfo((Type)node.Attribute);
						break;
					case ReflectionNodeType.Type:
						html += GetTypeInfo((Type)node.Attribute);
						break;
					case ReflectionNodeType.Constructor:
						html += GetCtorInfo((ConstructorInfo)node.Attribute);
						break;
					case ReflectionNodeType.Event:
						html += GetEventInfo((EventInfo)node.Attribute);
						break;
					case ReflectionNodeType.Field:
						html += GetFieldInfo((FieldInfo)node.Attribute);
						break;
					case ReflectionNodeType.Method:
						html += GetMethodInfo((MethodInfo)node.Attribute);
						break;
					case ReflectionNodeType.Property:
						html += GetPropInfo((PropertyInfo)node.Attribute);
						break;
					default:
						//ll.Text = "";
						break;
				}
			} catch(Exception ex) {
				html += tree.ress.GetString("ObjectBrowser.Info.CollectError") + "<br>" + ex.ToString().Replace("\n", "<br>");
			}
			
			html += RenderFoot();
			
			ht.Html = html;
		//	ht.CascadingStyleSheet = cssPath;
		}
		
		string GetLinkInfo(Type type)
		{
			string text;
			text = ln(references.Add(type), RT("GotoType"));
			//ll.Links.Add(0, ll.Text.Length, type);
			text += "<br><br>" + RT("LinkedType") + "<br>";
			return text + GetInAsm(type.Assembly);
		}
		
		string GetSubInfo()
		{
			return RT("SubInfo");
		}
		
		string GetSuperInfo()
		{
			return RT("SuperInfo");
		}
		
		string GetNSInfo(Assembly asm)
		{
			return GetInAsm(asm);
		}
		
		string GetFolderInfo(string folder)
		{
			if (folder == tree.ress.GetString("ObjectBrowser.Nodes.Resources")) 
				return RT("ResFInfo");
			else if (folder == tree.ress.GetString("ObjectBrowser.Nodes.References"))
				return RT("RefFInfo");
			else if (folder == tree.ress.GetString("ObjectBrowser.Nodes.Modules"))
				return RT("ModFInfo");
			else
				return RT("NoInfo");
		}
		
		string GetModInfo(Module mod)
		{
			string text = String.Format(RT("ModInfo"), mod.Name, mod.ScopeName, mod.IsResource());
			text += GetCustomAttribs(mod);
			text += "<br>";
			return text + GetInAsm(mod.Assembly);
		}
		
		string GetLibInfo(Assembly asm)
		{
			return RT("LibInfo") + GetInAsm(asm);
		}
		
		string GetRefInfo(AssemblyName asn)
		{
			string text = String.Format(RT("RefInfo"), asn.Name, asn.FullName, asn.Version.ToString(), "");
			return text + ln(references.Add(asn), RT("OpenRef"));
		}
		
		string GetAssemblyInfo(Assembly asm)
		{
			string text = String.Format(RT("AsmInfo"),
			                        asm.GetName().Name, asm.FullName, asm.GetName().Version.ToString(),
			                        asm.Location, asm.GlobalAssemblyCache);
			return text + GetCustomAttribs(asm);
		}
		
		string GetEventInfo(EventInfo info)
		{
			string ret = "public ";
			ret += GetTypeRef(info.EventHandlerType) + " ";
			ret += info.Name;
			ret += "<br><br>" + RT("Attributes") + "<br>";
			ret += GetCustomAttribs(info);
			
			IClass c = parserService.GetClass(info.DeclaringType.FullName);
			if(c == null) goto noDoc;
			foreach(IEvent e in c.Events) {
				if(e.Name == info.Name) {
					ret += "<br>" + RT("Documentation") + "<br>" + GetDocumentation(e.Documentation) + "<br>";
					break;
				}
			}
						
			noDoc:
			
			ret += "<br>";
			ret += GetInType(info.DeclaringType);
			ret += "<br><br>";
			ret += GetInAsm(info.DeclaringType.Assembly);
			
			return ret;
		}
		
		string GetFieldInfo(FieldInfo info)
		{
			string ret = "";
			if (info.IsPublic) ret += "public ";
 			if (info.IsFamily || info.IsFamilyAndAssembly || info.IsFamilyOrAssembly) ret += "protected ";
			if (info.IsAssembly || info.IsFamilyAndAssembly || info.IsFamilyOrAssembly) ret += "internal ";
			if (info.IsPrivate) ret += "private ";
			if (info.IsStatic) ret += "static ";
			if (info.IsLiteral) ret += "const ";
			ret += GetTypeRef(info.FieldType) + " ";
			ret += info.Name;
			ret += "<br><br>" + RT("Attributes") + "<br>";
			ret += GetCustomAttribs(info);
			
			IClass c = parserService.GetClass(info.DeclaringType.FullName);
			if(c == null) goto noDoc;
			foreach(IField f in c.Fields) {
				if(f.Name == info.Name) {
					ret += "<br>" + RT("Documentation") + "<br>" + GetDocumentation(f.Documentation) + "<br>";
					break;
				}
			}
						
			noDoc:
			
			ret += "<br>" + GetInType(info.DeclaringType);
			ret += "<br><br>" + GetInAsm(info.DeclaringType.Assembly);
			
			return ret;
		}
		
		string GetCtorInfo(ConstructorInfo info)
		{
			string ret = "";
			if (info.IsPublic) ret += "public ";
 			if (info.IsFamily || info.IsFamilyAndAssembly || info.IsFamilyOrAssembly) ret += "protected ";
			if (info.IsAssembly || info.IsFamilyAndAssembly || info.IsFamilyOrAssembly) ret += "internal ";
			if (info.IsPrivate) ret += "private ";
			if (info.IsStatic) ret += "static ";
			ret += info.Name + " (";
			ret += GetParameters(info);
			ret += ")<br><br>" + RT("Attributes") + "<br>";
			ret += GetCustomAttribs(info);
			
			IClass c = parserService.GetClass(info.DeclaringType.FullName);
			if(c == null) goto noDoc;
			foreach(IMethod cc in c.Methods) {
				if(cc.IsConstructor && IsSameSig(cc, info)) {
					ret += "<br>" + RT("Documentation") + "<br>" + GetDocumentation(cc.Documentation) + "<br>";
					break;
				}
			}
						
			noDoc:
			
			ret += "<br>" + GetInType(info.DeclaringType);
			ret += "<br><br>" + GetInAsm(info.DeclaringType.Assembly);
			
			return ret;
		}
		
		string GetMethodInfo(MethodInfo info)
		{
			string ret = "";
			if (info.IsPublic) ret += "public ";
 			if (info.IsFamily || info.IsFamilyAndAssembly || info.IsFamilyOrAssembly) ret += "protected ";
			if (info.IsAssembly || info.IsFamilyAndAssembly || info.IsFamilyOrAssembly) ret += "internal ";
			if (info.IsPrivate) ret += "private ";
			if (info.IsStatic) ret += "static ";
			if (info.IsVirtual && !info.IsFinal) ret += "virtual ";
			if (info.IsAbstract) ret += "abstract ";
			if (info.ReturnType != typeof(void)) {
				ret += GetTypeRef(info.ReturnType);
			} else {
				ret += "void";
			}
			ret += " " + info.Name + " (";
			ret += GetParameters(info);
			ret += ")<br><br>" + RT("Attributes") + "<br>";
			ret += GetCustomAttribs(info);
			
			IClass c = parserService.GetClass(info.DeclaringType.FullName);
			if(c == null) goto noDoc;
			foreach(IMethod cc in c.Methods) {
				if(cc.Name == info.Name && IsSameSig(cc, info)) {
					ret += "<br>" + RT("Documentation") + "<br>" + GetDocumentation(cc.Documentation) + "<br>";
					break;
				}
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -