我正在嘗試編寫一個.NET組件。組件將被放置到窗體/用戶控件上,並需要在設計時訪問組件父窗體/用戶控件所引用的組件中的屬性。在設計時是否有可能獲得這些組件?我可以在設計時在C#中迭代引用的程序集嗎?
2
A
回答
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?
編輯:我沒有刪除這篇文章,因爲你還沒有任何其他答覆。當我最初回答時,我沒有正確閱讀這個問題,所以沒有看到「在設計時間」部分。另一方面,也許這並不重要 - 這至少會給你一些嘗試。祝你好運,如果這是一場瘋狂的追逐,我們會很抱歉。
1
Visual Studio Automation and Extensibility將允許您在設計時訪問該類信息,這意味着您可以在設計時擁有並加載訪問數據。
相關問題
- 1. 我可以在一個程序集中同時使用C#和C++/CLI嗎?
- 2. 我可以在後臺迭代嗎?
- 3. 我可以在struts2.0的jsp上的迭代器中使用迭代器嗎?
- 4. Silverlight 5程序集可以引用Silverlight 4程序集嗎?
- 5. 程序集 - 我可以在程序中聲明變量嗎?
- 6. 我可以在4.0應用程序中引用.NET 3.5 .DLL嗎?
- 7. 我可以在C程序中使用C++庫嗎?
- 8. 你可以在LaTeX中迭代嗎?
- 9. 我可以在C#中設置UdpClient的超時時間嗎?
- 10. 我可以在android中收集應用程序通知嗎?
- 11. 我們可以在iPhone應用程序中集成Flash嗎?
- 12. 我可以在C#中使用類似foreach的方式迭代數組嗎
- 13. iOS XCode應用程序可以引用MonoTouch程序集嗎?
- 14. C#中可能在特定索引處啓動迭代器嗎?
- 15. 我可以在openshift應用程序中設置cPanel嗎?
- 16. 我可以避免引用從現有引用繼承的程序集嗎?
- 17. 我可以在迭代中添加額外的步驟嗎?
- 18. 我可以使用引導程序設計HTML電子郵件模板嗎
- 19. 我可以在C程序中使用模式匹配嗎?
- 20. 我可以在運行時計算Objc中的方程嗎?
- 21. 。我可以找出我目前正在進行的迭代嗎?
- 22. 在設計時可以在richtextbox中設置文本樣式嗎?
- 23. 在C++中使用集合迭代器
- 24. svn update:我可以迭代結果嗎?
- 25. 我可以迭代數據源嗎?
- 26. 我可以迭代.NET4 MemoryCache嗎?
- 27. 我可以在我的應用程序中使用notepad ++ exe嗎?
- 28. 我可以在環中實現迭代器end()嗎?
- 29. 我可以在while循環中定義一個迭代器嗎?
- 30. 代理類可以是可索引或可迭代的序列