反射获取类型
winform开发中经常使用反射来动态配置程序!例如将所有窗体放到一个dll里使用的时候通过反射来加载窗体以实现灵活配置!
今天就针对反射获取类型给出自己的一些代码:
using System;
using System.IO;
using System.Collections.Generic;
using static System.Console;
using System.Reflection;
using System.Text;
namespace Gaofajin.DllHandler
{
public class ReflectionHelper
{
/// <summary>
/// 反射创建类实例
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fullname">名称空间.类名</param>
/// <param name="assemblyName">Dll名.dll</param>
/// <returns></returns>
public static T CreateInstance<T>(string fullname,string assemblyName)
{
try
{
string path = fullname + "," + assemblyName;
Type tp = Type.GetType(path, true);
return (T)Activator.CreateInstance(tp, true);
}
catch
{
return default(T);
}
}
/// <summary>
/// 反射创建类实例
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="assemblyName">必须使用全路径,此处如果只有dll名称则默认在程序启动目录下找</param>
/// <param name="nameSpace"></param>
/// <param name="className"></param>
/// <returns></returns>
public static T CreateInstanceByLoadFile<T>(string assemblyName,string nameSpace,string className)
{
if (!assemblyName.Contains("\\"))
{
assemblyName = System.Windows.Forms.Application.StartupPath + "\\" + assemblyName;
}
try
{
string fullName = nameSpace + "." + className;
Assembly ass = Assembly.LoadFile(assemblyName);
object obj= ass.CreateInstance(fullName);
return (T)obj;
}
catch
{
return default(T);
}
}
/// <summary>
/// 反射创建类实例
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="assemblyName">这个参数实际上是需要加载的程序集的强名称(名称,版本,语言,公钥标记)。例如.NET 2.0中的FileIOPermission类,它的强名称是:
///System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</param>
/// <param name="nameSpace">名称空间</param>
/// <param name="className">类名</param>
/// <returns></returns>
public static T CreateInstanceByLoad<T>(string assemblyName, string nameSpace, string className)
{
try
{
string fullName = nameSpace + "." + className;
Assembly ass = Assembly.Load(assemblyName);
object obj = ass.CreateInstance(fullName);
return (T)obj;
}
catch
{
return default(T);
}
}
/// <summary>
/// 反射创建类实例
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="assemblyName">LoadFrom支持从一个URL加载程序集(如"http://www.abc.com/test.dll"),这个程序集会被下载到用户缓存文件夹中。</param>
/// <param name="nameSpace">名称空间</param>
/// <param name="className">类名</param>
/// <returns></returns>
public static T CreateInstanceByLoadFrom<T>(string assemblyName, string nameSpace, string className)
{
try
{
string fullName = nameSpace + "." + className;
Assembly ass = Assembly.LoadFrom(assemblyName);
object obj = ass.CreateInstance(fullName);
return (T)obj;
}
catch
{
return default(T);
}
}
/// <summary>
/// 反射获取类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fullname">名称空间.类名</param>
/// <param name="assemblyName">Dll名.dll</param>
/// <returns></returns>
public static Type GetType(string fullname, string assemblyName)
{
try
{
string path = fullname + "," + assemblyName;
return Type.GetType(path, true);
}
catch
{
return null;
}
}
/// <summary>
/// 反射获取类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="assemblyName">必须使用全路径,此处如果只有dll名称则默认在程序启动目录下找</param>
/// <param name="nameSpace"></param>
/// <param name="className"></param>
/// <returns></returns>
public static Type getTypeByLoadFile(string assemblyName, string nameSpace, string className)
{
if (!assemblyName.Contains("\\"))
{
assemblyName = System.Windows.Forms.Application.StartupPath + "\\" + assemblyName;
}
try
{
string fullName = nameSpace + "." + className;
Assembly ass = Assembly.LoadFile(assemblyName);
return ass.GetType(fullName, true);
}
catch
{
return null;
}
}
/// <summary>
/// 反射获取类型
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="assemblyName">这个参数实际上是需要加载的程序集的强名称(名称,版本,语言,公钥标记)。例如.NET 2.0中的FileIOPermission类,它的强名称是:
///System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</param>
/// <param name="nameSpace">名称空间</param>
/// <param name="className">类名</param>
/// <returns></returns>
public static Type GetTypeByLoad(string assemblyName, string nameSpace, string className)
{
try
{
string fullName = nameSpace + "." + className;
Assembly ass = Assembly.Load(assemblyName);
return ass.GetType(fullName, true);
}
catch
{
return null;
}
}
/// <summary>
/// 反射获取类型
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="assemblyName">LoadFrom支持从一个URL加载程序集(如"http://www.abc.com/test.dll"),这个程序集会被下载到用户缓存文件夹中。</param>
/// <param name="nameSpace">名称空间</param>
/// <param name="className">类名</param>
/// <returns></returns>
public static Type GetTypeByLoadFrom(string assemblyName, string nameSpace, string className)
{
try
{
string fullName = nameSpace + "." + className;
Assembly ass = Assembly.LoadFrom(assemblyName);
return ass.GetType(fullName,true);
}
catch
{
return null;
}
}
}
}
取得type以后可以使用反射相应的方法来实例化:例如object o = Activator.CreateInstance( type )