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

📄 remoteclassloader.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
字号:
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Collections.Generic;
using GPCore.Protocols;
using GPCore.Vocoders;
using GPCore.NetworkProtocols;
using GPCore.Phones;

namespace GPCore.Plugins
{
    /// <summary>
    /// A Class that loads other classes and is meant to run in
    /// a seperate AppDomain than the rest of the Application
    /// </summary>
	public class RemoteClassLoader : MarshalByRefObject
	{
        /// <summary>
        /// A Dictionary of Plugins linking to their owning assembly
        /// </summary>
        protected Dictionary<string, Assembly> allPlugins = new Dictionary<string, Assembly>();
        /// <summary>
        /// The list of <see cref= "CoreEventListener"/>s
        /// </summary>
		protected ArrayList listenerList = new ArrayList();
        /// <summary>
        /// The list of <see cref="IProtocol"/>s
        /// </summary>
		protected ArrayList protocolList = new ArrayList();
        /// <summary>
        /// The list of all the phone classes
        /// </summary>
        protected ArrayList phoneList = new ArrayList();
        /// <summary>
        /// The list of all the possible voice encodings
        /// </summary>
        protected ArrayList vocoderList = new ArrayList();
        /// <summary>
        /// The list of all network protocols available
        /// </summary>
        protected ArrayList networkProtocolList = new ArrayList();
        /// <summary>
        /// The List of Assemblies handled by this Classloader
        /// </summary>
		protected ArrayList assemblyList = new ArrayList();
        /// <summary>
        /// Creates a new RemoteClassLoader
        /// </summary>
		public RemoteClassLoader() { }
        /// <summary>
        /// Loads an Assembly and all of its types into the
        /// AppDomain
        /// </summary>
        /// <param name="fullname">The full path of the Assembly</param>
		public void LoadAssembly(string fullname)
		{
			string path = Path.GetDirectoryName(fullname);
			string filename = Path.GetFileNameWithoutExtension(fullname);
            if (filename.IndexOf("peex") > -1)
                Console.WriteLine("pause");
			Assembly assembly = Assembly.LoadFrom(fullname);
			assemblyList.Add(assembly);
			foreach (Type loadedType in assembly.GetTypes())
			{
                List<Attribute> a =new List<Attribute>( Attribute.GetCustomAttributes(loadedType));

				if (Attribute.IsDefined(loadedType, typeof(PluginAttribute)))
				{

                    foreach (Type i in loadedType.GetInterfaces())
					{
						if (i == typeof(IProtocol) )
                        {
                            protocolList.Add(loadedType);
                        }
                        else if (i == typeof(IPhone))
                        {
                            phoneList.Add(loadedType);
                        }
                        else if (i == typeof(IVocoder))
                        {
                            vocoderList.Add(loadedType);
                        }
                        else if (i == typeof(INetworkProtocol))
                        {
                            networkProtocolList.Add(loadedType);
                        }
					}
                    Type t = loadedType;
                    while ((t = t.BaseType) != typeof(Object))
                    {
                        if (t == typeof(CoreEventListener))
                            listenerList.Add(loadedType);
                    }
                    allPlugins.Add(loadedType.FullName, assembly);
				}
			}
		}
        /// <summary>
        /// Gets all of the Attributes of A type
        /// </summary>
        /// <param name="typeName">The FullName of the type whose Attributes you want</param>
        /// <returns>An object Array of all the attributes</returns>
		public object[] GetAttributes(string typeName)
		{
			MemberInfo inf = GetTypeByName(typeName);
			return inf.GetCustomAttributes(true);
		}
        /// <summary>
        /// Gets a Static PropertyValue
        /// </summary>
        /// <param name="typeName">The FullName of the type</param>
        /// <param name="propertyName">The property you are interested in</param>
        /// <returns>The value of the property</returns>
		public object GetStaticPropertyValue(string typeName, string propertyName)
		{
			Type type = GetTypeByName(typeName);
			if (type == null)
			{
				throw new ArgumentException("Cannot find a type of name " + typeName +
					" within the plugins or the common library.");
			}
			return type.GetProperty(propertyName,
				BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
		}
        /// <summary>
        /// Gets the <see cref=" CoreEventListener"/>s
        /// </summary>
        /// <returns>a string array of all of the Types handled by this ClassLoader</returns>
		public string[] GetListeners()
		{
			ArrayList classList = new ArrayList();
			foreach (Type pluginType in listenerList)
			{
				classList.Add(pluginType.FullName);
			}
			return (string[])classList.ToArray(typeof(string));
		}
        /// <summary>
        /// Gets the <see cref="IProtocol"/>s
        /// </summary>
        /// <returns>a string array of all of the Types handled by this ClassLoader</returns>
		public string[] GetProtocols()
		{
			ArrayList classList = new ArrayList();
			foreach (Type pluginType in protocolList)
			{
				classList.Add(pluginType.FullName);
			}
			return (string[])classList.ToArray(typeof(string));
		}
        /// <summary>
        /// Returns a string array of the full names of all the Vocoders currently loaded.
        /// </summary>
        public string[] GetVocoders()
        {
            List<string> classList = new List<string>();
            foreach (Type type in vocoderList )
            {
                classList.Add(type.FullName);
            }
            return classList.ToArray();
        }
        /// <summary>
        /// Returns a string array of the full names of all the Phones currently loaded.
        /// </summary>
        public string[] GetPhones()
        {
            List<string> classList = new List<string>();
            foreach (Type t in phoneList)
                classList.Add(t.FullName);
            return classList.ToArray();
        }
        /// <summary>
        /// Returns a string array of the full names of all the NetworkProtocols currently loaded.
        /// </summary>
        public string[] GetNetworkProtocols()
        {
            List<string> classList = new List<string>();
            foreach (Type t in networkProtocolList)
                classList.Add(t.FullName);
            return classList.ToArray();
        }
        /// <summary>
        /// Gets all the Assemblies
        /// </summary>
        /// <returns>a string array of all of the Assemblies handled by this Classloader</returns>
		public string[] GetAssemblies()
		{
			ArrayList assemblyNameList = new ArrayList();
			foreach (Assembly userAssembly in assemblyList)
			{
				assemblyNameList.Add(userAssembly.FullName);
			}
			return (string[])assemblyNameList.ToArray(typeof(string));
		}
        /// <summary>
        /// Returns all of the subclasses of <paramref name="baseClass"/>
        /// </summary>
        /// <param name="baseClass">The Class you are looking for</param>
        /// <returns></returns>
		public string[] GetSubclasses(string baseClass)
		{
			Type baseClassType = Type.GetType(baseClass);
			if (baseClassType == null)
			{
				baseClassType = GetTypeByName(baseClass);
			}
			if (baseClassType == null)
			{
				throw new ArgumentException("Cannot find a type of name " + baseClass + " within the plugins or the common library.");
			}
			ArrayList subclassList = new ArrayList();
			foreach (Type pluginType in listenerList)
			{
				if (pluginType.IsSubclassOf(baseClassType))
				{
					subclassList.Add(pluginType.FullName);
				}
			}
			return (string[])subclassList.ToArray(typeof(string));
		}
        /// <summary>
        /// Gets the name of the owning assembly
        /// </summary>
        /// <param name="typeName">the type whose Assembly you want</param>
        /// <returns>the filename of the assembly that owns <paramref name="typeName"/></returns>
        /// <exception cref="InvalidOperationException">Thrown when <paramref name="typeName"/>is not handled by this Assembly</exception>
		public string GetOwningAssembly(string typeName)
		{
			Assembly owningAssembly = null;
            if (allPlugins.TryGetValue(typeName, out owningAssembly))
                return owningAssembly.ManifestModule.ToString();
            else
			    throw new InvalidOperationException("Could not find owning assembly for type " + typeName);
		}
        /// <summary>
        /// Checks if <paramref name="typeName"/>is part of this assembly
        /// </summary>
        /// <param name="typeName">The TypeName to look for</param>
        /// <returns>Returns True if <paramref name="typeName"/> belongs to any of the assemblies loaded</returns>
		public bool ManagesType(string typeName)
		{
			return (GetTypeByName(typeName) != null);
		}
        
		internal Type GetTypeByName(string typeName)
		{
			foreach (Type pluginType in listenerList)
			{
				if (pluginType.FullName == typeName)
				{
					return pluginType;
				}
			}
			foreach (Type pluginType in protocolList)
			{
				if (pluginType.FullName == typeName)
					return pluginType;
			}
			return null;
		}
	}
}

⌨️ 快捷键说明

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