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

📄 reflectionnode.cs

📁 CSharpDevelop:这是一个包含源代码的C#、VB.NET的编辑器。
💻 CS
字号:
//  ReflectionNode.cs
//  Copyright (C) 2001 Mike Krueger
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections;
using System.IO;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Reflection;
using System.Reflection.Emit;

using SharpDevelop.Gui.Edit;
using SharpDevelop.Internal.Undo;
using SharpDevelop.Gui.Window;

namespace SharpDevelop.Gui.Edit.Reflection {
	
	public enum ReflectionNodeType {
		Folder,
		Resource,
		Assembly,
		Library,
		Namespace,
		Type,
		Constructor,
		Method,
		Field,
		Property,
		SubTypes,
		SuperTypes,
		Reference,
		Event,
		Link
	}
	
	public class ReflectionNode : TreeNode
	{
		protected const int CLASSINDEX     = 14;
		protected const int STRUCTINDEX    = CLASSINDEX + 1 * 4;
		protected const int INTERFACEINDEX = CLASSINDEX + 2 * 4;
		protected const int ENUMINDEX      = CLASSINDEX + 3 * 4;
		protected const int METHODINDEX    = CLASSINDEX + 4 * 4;
		protected const int PROPERTYINDEX  = CLASSINDEX + 5 * 4;
		protected const int FIELDINDEX     = CLASSINDEX + 6 * 4;
		protected const int DELEGATEINDEX  = CLASSINDEX + 7 * 4;
		
		protected ReflectionNodeType type;
		protected string             name;
		protected object             attribute;
		
		protected bool  populated = false;
		
		public ReflectionNodeType Type {
			get {
				return type;
			}
			set {
				type = value;
			}
		}
		
		public object Attribute {
			get {
				return attribute;
			}
		}
		
		public bool Populated {
			get {
				return populated;
			}
		}
		
		public string Name {
			get {
				return name;
			}
			set {
				name = value;
			}
		}
		
		public ReflectionNode(string name, object attribute, ReflectionNodeType type) : base(name)
		{
			this.attribute = attribute;
			this.type = type;
			this.name = name;
			if (type == ReflectionNodeType.Namespace ||
				type == ReflectionNodeType.Assembly  ||
				type == ReflectionNodeType.Library   ||
				type == ReflectionNodeType.Type) {
					Nodes.Add(new TreeNode(""));
			}
			if (type == ReflectionNodeType.Field) {
				Text += " : " + GetTypeString(((FieldInfo)attribute).FieldType.ToString());
			} else
			if (type == ReflectionNodeType.Property) {
				Text += " : " + GetTypeString(((PropertyInfo)attribute).PropertyType.ToString());
			}
			SetIcon();
		}
		
		
		protected string GetTypeString(string s)
		{
			string[] str = s.Split(new char[] {'.'});
			string type  = str[str.Length - 1];
			return type;
		}
		
		protected virtual void SetIcon()
		{
			switch (type) {
				case ReflectionNodeType.Link:
					break;
				case ReflectionNodeType.Resource:
					ImageIndex  = SelectedImageIndex = 11;
					break;
				
				case ReflectionNodeType.Reference:
					ImageIndex  = SelectedImageIndex = 8;
					break;
					
				case ReflectionNodeType.SubTypes:
					ImageIndex  = SelectedImageIndex = 4;
					break;
					
				case ReflectionNodeType.SuperTypes:
					ImageIndex  = SelectedImageIndex = 5;
					break;
				
				case ReflectionNodeType.Event:
					ImageIndex  = SelectedImageIndex = 12;
					EventInfo eventinfo = (EventInfo)attribute;
					MethodInfo add    = eventinfo.GetAddMethod();
					MethodInfo raise  = eventinfo.GetRaiseMethod();
					MethodInfo remove = eventinfo.GetRemoveMethod();
					
					if (add != null)
						Nodes.Add(new ReflectionMethodNode(add));
					if (raise != null)
						Nodes.Add(new ReflectionMethodNode(raise));
					if (remove != null)
						Nodes.Add(new ReflectionMethodNode(remove));
					break;
				
				
				case ReflectionNodeType.Property: // TODO : private, protected, internal properties
					PropertyInfo propertyinfo = (PropertyInfo)attribute;
					ImageIndex  = SelectedImageIndex = PROPERTYINDEX;
					
					if (propertyinfo.CanRead)
						Nodes.Add(new ReflectionMethodNode(propertyinfo.GetGetMethod()));
					if (propertyinfo.CanWrite)
						Nodes.Add(new ReflectionMethodNode(propertyinfo.GetSetMethod()));
					break;
				
				case ReflectionNodeType.Field: // TODO : internal fields
					FieldInfo fieldinfo = (FieldInfo)attribute;
					if (fieldinfo.IsLiteral) {
						ImageIndex  = SelectedImageIndex = 13;
						break;
					} 
					if (fieldinfo.IsPrivate) {
						ImageIndex  = SelectedImageIndex = FIELDINDEX + 3;
						break;
					} 
					if (!(fieldinfo.IsPrivate || fieldinfo.IsPublic)) {
						ImageIndex  = SelectedImageIndex = FIELDINDEX + 2;
						break;
					}
					ImageIndex  = SelectedImageIndex = FIELDINDEX;
					break;
				default:
					throw new Exception("ReflectionFolderNode.SetIcon : unknown ReflectionNodeType " + type.ToString());
			}
		}
		
		public virtual void Populate()
		{
			switch (type) {
				case ReflectionNodeType.Assembly:
					PopulateAssembly((Assembly)attribute, this);
					break;
				case ReflectionNodeType.Library:
					PopulateLibrary((Assembly)attribute, this);
					break;
			}
			populated = true;
		}
		
		public TreeNode GetNodeFromCollection(TreeNodeCollection collection, string title)
		{
			foreach (TreeNode node in collection)
				if (node.Text == title)
					return node;
			return null;
		}
		
		void PopulateLibrary(Assembly assembly, TreeNode parentnode)
		{
			parentnode.Nodes.Clear();
			string[] manifestresourcenames = assembly.GetManifestResourceNames();
			Type[]   types                 = assembly.GetTypes();
			foreach (Type type in types) {
				string   typename  = type.ToString();
				int      lastindex = typename.LastIndexOf('.');
				TreeNode path      = parentnode;
				string   nodename  = typename;
				if (lastindex != -1) {
					string pathname = typename.Substring(0, lastindex);
					TreeNode node = GetNodeFromCollection(parentnode.Nodes, pathname);
					if (node == null) {
						TreeNode newnode = new ReflectionFolderNode(pathname, null, ReflectionNodeType.Namespace, 3, 3);
						newnode.Nodes.Clear();
						parentnode.Nodes.Add(newnode);
						path = newnode;
					} else
						path = node;
					nodename = typename.Substring(lastindex + 1);
 				}
				path.Nodes.Add(new ReflectionTypeNode(nodename, type));
			}
		}
		
		void PopulateAssembly(Assembly assembly, TreeNode parentnode)
		{
			parentnode.Nodes.Clear();
			
			TreeNode node = new ReflectionFolderNode(Path.GetFileName(assembly.CodeBase), assembly, ReflectionNodeType.Library, 2, 2);
			parentnode.Nodes.Add(node);
			
			ReflectionFolderNode resourcefolder = new ReflectionFolderNode("Resources", assembly, ReflectionNodeType.Folder, 6, 7);
			string[] resources = assembly.GetManifestResourceNames();
			foreach (string resource in resources) {
				resourcefolder.Nodes.Add(new ReflectionNode(resource, null, ReflectionNodeType.Resource));
			}
			parentnode.Nodes.Add(resourcefolder);
			
			ReflectionFolderNode referencefolder = new ReflectionFolderNode("References", assembly, ReflectionNodeType.Folder, 9, 10);
			AssemblyName[] references = assembly.GetReferencedAssemblies();
			foreach (AssemblyName name in references) {
				referencefolder.Nodes.Add(new ReflectionNode(name.Name, name, ReflectionNodeType.Reference));
			}
			parentnode.Nodes.Add(referencefolder);
		}
		
		public virtual void OnExpand()
		{
		}
		public virtual void OnCollapse()
		{
		}
	}
}

⌨️ 快捷键说明

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