2009-01-10 63 views

回答

0

這是我終於想出這個問題的概念證明。這不是沒有缺陷,但我相信只要一點工作,它就能正常運作。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.ComponentModel.Design; 
using System.Reflection; 
using System.Windows.Forms; 

namespace ReferencedAssemblies 
{ 
    public partial class GetReferencedComponents : Component, ISupportInitialize 
    { 
     private Control hostingControl; 

     public GetReferencedComponents(IContainer container) : this() 
     { 
      container.Add(this); 
     } 

     public GetReferencedComponents() 
     { 
      InitializeComponent(); 
      Assemblies = new List<string>(); 
      GetAssemblies(); 
     } 

     public List<string> Assemblies { get; private set; } 

     [Browsable(false)] 
     public Control HostingControl 
     { 
      get 
      { 
       if (hostingControl == null && this.DesignMode) 
       { 
        IDesignerHost designer = this.GetService(typeof(IDesignerHost)) as IDesignerHost; 
        if (designer != null) 
         hostingControl = designer.RootComponent as Control; 
       } 
       return hostingControl; 
      } 
      set 
      { 
       if (!this.DesignMode && hostingControl != null && hostingControl != value) 
        throw new InvalidOperationException("Cannot set at runtime."); 
       else 
        hostingControl = value; 
      } 
     } 

     public void BeginInit() 
     { 
     } 

     public void EndInit() 
     { 
      // use ISupportInitialize.EndInit() to trigger loading assemblies at design-time. 
      GetAssemblies(); 
     } 

     private void GetAssemblies() 
     { 
      if (HostingControl != null) 
      { 
       if (this.DesignMode) 
        MessageBox.Show(String.Format("Getting Referenced Assemblies from {0}", HostingControl.Name)); 
       Assemblies.Clear(); 
       AssemblyName[] assemblyNames = HostingControl.GetType().Assembly.GetReferencedAssemblies(); 
       foreach (AssemblyName item in assemblyNames) 
        Assemblies.Add(item.Name); 
      } 
     } 
    } 

}

謝謝您的回答!

Mike

0

您是否嘗試過使用Assembly.GetReferencedAssemblies

編輯:我沒有刪除這篇文章,因爲你還沒有任何其他答覆。當我最初回答時,我沒有正確閱讀這個問題,所以沒有看到「在設計時間」部分。另一方面,也許這並不重要 - 這至少會給你一些嘗試。祝你好運,如果這是一場瘋狂的追逐,我們會很抱歉。

相關問題