[英]How do I hide some of the default control properties at design-time (C#)?
[英]Can I iterate referenced assemblies at design-time in C#?
我正在嘗試編寫.NET組件。 組件將被放置到窗體/用戶控件上,並且需要在設計時訪問組件父窗體/用戶控件所引用的程序集中的屬性。 是否可以在設計時獲得這些組件?
從Visual Studio自動化和可擴展性的角度出發,您可以在設計時訪問此類信息,因為您可以在設計時具有並可以訪問數據。
您是否嘗試過使用Assembly.GetReferencedAssemblies ?
由於您沒有其他任何回復,因此我取消刪除了該帖子。 當我最初回答時,我沒有正確閱讀問題,因此也沒有看到“設計時”部分。 另一方面,也許沒關系-至少這給了您一些嘗試的機會。
祝你好運,如果這是追趕大雁的話,我深表歉意。
這是我最終為這個問題想到的概念證明。 它並非沒有缺陷,但我相信只要稍作努力,它就能正常運行。
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);
}
}
}
}
感謝您的回答!
麥克風
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.