久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

有關System.Reflection.Assembly(反射的應用)

 凱之風 2011-06-03
近來學習反射創(chuàng)建對象,遇到一些有關System.Reflection.Assembly的問題及解決方法,現(xiàn)記錄如下,供有此類問題的朋友參考:
  一,、顯示窗體
  System.Reflection.Assembly assembly;
  
   assembly = System.Reflection.Assembly.LoadFile(Application.StartupPath + @"\aa\TestLibrary.dll");
  
   Type type = assembly.GetType("TestLibrary.Form1"); // TestLibrary.Form1 為窗體全名稱
   Form tt = assembly.CreateInstance(type.FullName) as Form;
   tt.Show();
  二、得到DLL文件中的類
  
  1,、實例化一個DataTable
  DataTable dtInfo = new DataTable();
  2、初始化設置DataTable
   private void InitGridDataTable()
   {
   string[] strColName = new string[] { "LibraryFillName", "StrDLLFilePath", "AssembleID", "ClassAPath", "ClassAName", "ClassAVersion", "ClassADescription" };
  
   for (int i = 0; i < strColName.Length; i++)
   {
   DataColumn mydatacolumn = new DataColumn();
   mydatacolumn.DataType = System.Type.GetType("System.String");
   mydatacolumn.ColumnName = strColName[i];
   mydatacolumn.ReadOnly = false;
   dtInfo.Columns.Add(mydatacolumn);
   }
   dtInfo.AcceptChanges();
   }
  
  
  3,、得到文件的內容
  ///
   /// 根據(jù)傳入的文件路徑,,得到文件的內容
   /// 
   /// 文件路徑
   /// 文件內容
   public byte[] GetFileContent(string path)
   {
   System.IO.FileStream fs = System.IO.File.OpenRead(path);
   byte[] cache = new byte[fs.Length];
   fs.Read(cache, 0, (int)fs.Length);
   fs.Close();
   fs = null;
   return cache;
   }
  4、得到DLL文件內的類
  public DataSet GetDLLClass()
  {
  
   //strAssSaveFolder 為文件存儲文件夾,(注:為空時,,String.IsNullOrEmpty 返為真)
   //用來得到文件的全路徑
   if (string.IsNullOrEmpty(strAssSaveFolder))
   {
   filePathString = filePathString + "\\" + strAssFileName;
   }
   else
   {
   filePathString = filePathString + "\\" + strAssSaveFolder + "\\" + strAssFileName;
   }
  
   //判斷文件是否存在
   if (!System.IO.File.Exists(filePathString)) //如果不存在,,則直接退出返回
   {
   return;
   }
  
  //創(chuàng)建AppDomain
   System.Security.Policy.Evidence adevidence = AppDomain.CurrentDomain.Evidence; //得到安全策略
   AppDomain dom = AppDomain.CreateDomain("execDom", adevidence);
   
  
   //得到類型,并給數(shù)據(jù)源中加入行
   //System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(filePathString);
   
  
   byte[] rawAssembly = (new FileService()).GetFileContent(filePathString);//得到文件內容
   System.Reflection.Assembly assembly = dom.Load(rawAssembly,null);
  
  
   //得到DLL文件的類
   foreach (Type type in assembly.GetTypes())
   {
   DataRow drRow = dtInfo.NewRow();
   drRow["LibraryFillName"] = type.FullName;
   drRow["StrDLLFilePath"] = filePathString;
   drRow["AssembleID"] = assembleID;
   drRow["ClassAPath"] = strAssSaveFolder;
   drRow["ClassAName"] = strAssFileName;
   drRow["ClassAVersion"] = strAssVersion;
   drRow["ClassADescription"] = strAssDescription;
   dtInfo.Rows.Add(drRow);
   }
  
   //使用后釋放AppDomain
   AppDomain.Unload(dom);
  }
  
  三,、得到類的字段屬性
  //得到窗體上的控件按鈕(根據(jù)類型及文件路徑)
   ///
   /// 得到窗體上的控件按鈕(根據(jù)類型及文件路徑)
   /// 
   /// 類型完全名稱字符
   /// 程序集絕對路徑
   /// 按鈕名稱及按鈕文本值的鍵值對
   public DataTable GetFormButton(string typeFullName, string strDllPath)
   {
   try
{
//判斷文件是否存在
if (!System.IO.File.Exists(strDllPath)) //如果不存在,,則直接退出返回
{
return null;
}
//得到程序集
//Assembly assembly = Assembly.LoadFile(strDllPath);
Assembly assembly = Assembly.Load(GetFileContent(strDllPath));
//得到窗體類型
Type iMyType = assembly.GetType(typeFullName);
//得到窗體字段屬性的集合
System.Reflection.FieldInfo[] iMyFielInfo_Group =
iMyType.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Public);//取得當然窗體的字段數(shù)組
//如果字段屬性的個數(shù)小于等于 0 ,則返回空(沒有找到按鈕)
if (iMyFielInfo_Group.Length <= 0)
{
return null;
}
DataTable dtInfo = GetNewDataTable();
if (dtInfo != null)
{
dtInfo.Clear();
}
   foreach (System.Reflection.FieldInfo iMyFielInfo in iMyFielInfo_Group)
{//字段屬性的完全限定名
string infoFullName = iMyFielInfo.FieldType.FullName;
//得到窗體類的對象實例
Object iMyObject = iMyFielInfo.GetValue(assembly.CreateInstance(iMyType.FullName));
if (infoFullName == "System.Windows.Forms.Button")
{
//根據(jù)屬性的字符標簽,,如本例中的("Text")得到屬性對象
System.Reflection.PropertyInfo iMyProperText = iMyFielInfo.FieldType.GetProperty("Text");
//判斷屬性對象是否為空且是否可讀
if (iMyProperText != null && iMyProperText.CanRead)
{
DataRow drInfo = dtInfo.NewRow();
drInfo["IsChoose"] = "N";
drInfo["ClsButtonText"] = iMyProperText.GetValue(iMyObject, null).ToString(); //屬性值
drInfo["ClsButtonName"] = iMyFielInfo.Name;
drInfo["ClsButtonType"] = iMyFielInfo.FieldType.FullName;
dtInfo.Rows.Add(drInfo);
dtInfo.AcceptChanges();
}
}
return dtInfo;
}
   catch (Exception Ex)
   {
   throw new Exception(Ex.Message);
   }
   }
  
  四,、其它有關文件操作
   ///
   /// 根據(jù)文件的路徑,得到文件的名稱
   /// 
   /// 文件路徑
   /// 文件名稱
   public string GetFileName(string fullPath)
   {
   int lastXieXianPosition = fullPath.LastIndexOf('\\');
   return fullPath.Substring(lastXieXianPosition + 1);
   }
  
   ///
   /// 根據(jù)傳入的文件路徑,,得到文件的版本號
   /// 
   /// 文件路徑
   /// 文件版本號
   public string GetFileVersion(string fullpath)
   {
   System.Diagnostics.FileVersionInfo fv = System.Diagnostics.FileVersionInfo.GetVersionInfo(fullpath);
   return fv.FileVersion;
   }
  
   ///
   /// 根據(jù)傳入的文件路徑,,得到文件的內容
   /// 
   /// 文件路徑
   /// 文件內容
   public byte[] GetFileContent(string path)
   {
   System.IO.FileStream fs = System.IO.File.OpenRead(path);
   byte[] cache = new byte[fs.Length];
   fs.Read(cache, 0, (int)fs.Length);
   fs.Close();
   fs = null;
   return cache;
   }
  
   //根據(jù)byte數(shù)組生成相應的文件內容
   ///
   /// 根據(jù)byte數(shù)組生成相應的文件內容
   /// 
   /// DLL類庫名稱
   /// 存儲文件路徑
   /// byte數(shù)組
   /// true 表示成功 false 表示失敗
   public bool ByteToLocalFile(string fileNameString, string filePathString, byte[] byteFileContent)
   {
   //判斷內容是否為空
   if (byteFileContent.Length <= 0)
   {
   throw new Exception("下載的文件內容為空!");
   }
  
   //判斷文件名是否為空
   if (fileNameString.Equals(""))
   {
   throw new Exception("下載的文件名為空,!");
   }
  
   //進行文件的生成
   if (!Directory.Exists(filePathString))
   {
   Directory.CreateDirectory(filePathString);
   }
   File.WriteAllBytes(filePathString + "\\" + fileNameString, byteFileContent);
   return true;
   }
  
   //判斷文件是否存在本地磁盤上
   ///
   /// 判斷文件是否存在本地磁盤上
   /// 
   /// 文件存放路徑
   /// 存在返回true,,不存在則返回false
   public bool IsFileExists(string strFileAllPath)
   {
   if (strFileAllPath.Equals(""))
   {
   throw new Exception("文件路徑名為空!");
   }
   if (File.Exists(strFileAllPath))
   {
   return true;
   }
   else
   {
   return false;
   }
   }

    本站是提供個人知識管理的網(wǎng)絡存儲空間,,所有內容均由用戶發(fā)布,,不代表本站觀點,。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,,謹防詐騙,。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報,。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多