[英]Can I iterate referenced assemblies at design-time in C#?
I am attempting to write a .NET component. 我正在尝试编写.NET组件。 The component will be dropped onto a form/user control and needs to access attributes in assemblies referenced by the components parent form/user control at design-time.
组件将被放置到窗体/用户控件上,并且需要在设计时访问组件父窗体/用户控件所引用的程序集中的属性。 Is it possible to obtain these assemblies at design time?
是否可以在设计时获得这些组件?
从Visual Studio自动化和可扩展性的角度出发,您可以在设计时访问此类信息,因为您可以在设计时具有并可以访问数据。
Have you tried using Assembly.GetReferencedAssemblies ? 您是否尝试过使用Assembly.GetReferencedAssemblies ?
I undeleted this post as you haven't had any other replies. 由于您没有其他任何回复,因此我取消删除了该帖子。 When I originally replied, I hadn't read the question properly, so hadn't seen the "at design time" part.
当我最初回答时,我没有正确阅读问题,因此也没有看到“设计时”部分。 On the other hand, maybe that doesn't matter - this at least gives you something to try.
另一方面,也许没关系-至少这给了您一些尝试的机会。
Good luck, and apologies if it's a wild goose chase. 祝你好运,如果这是追赶大雁的话,我深表歉意。
This is the proof of concept that I finally came up with for this question. 这是我最终为这个问题想到的概念证明。 It is not without flaws but I believe that with a little work it will function properly.
它并非没有缺陷,但我相信只要稍作努力,它就能正常运行。
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);
}
}
}
} }
Thanks for your answers! 感谢您的回答!
Mike 麦克风
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.