winform開發中經常使用反射來動態配置程式!例如將所有窗體放到一個dll里使用的時候通過反射來載入窗體以實現靈活配置! 今天就針對反射獲取類型給出自己的一些代碼: using System;using System.IO;using System.Collections.Generic;usin ...
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 )