简体   繁体   English

我可以在设计时使用C#迭代引用的程序集吗?

[英]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

EDIT: 编辑:

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.

相关问题 如何在设计时 (C#) 隐藏一些默认控件属性? - How do I hide some of the default control properties at design-time (C#)? Entity Framework Core - 未找到引用的设计时服务 - Entity Framework Core - No referenced design-time services were found C#Dev Express功能区表单-设计时:系统内存不足异常 - C# Dev Express Ribbon Form - Design-Time: System Out Of Memory Exception C#Windows窗体:设计时对选项卡控件的支持 - C# windows forms: design-time support for tab-control C#Visual Studio 2008 - Windows窗体设计器中的设计时错误 - C# Visual Studio 2008 - Design-Time Error in the Windows Forms Designer 使用c#和Visual Studio设计时数据集访问数据库 - Accessing a database using c# and Visual Studio design-time dataset C#WPF自定义控件在设计时不响应XAML属性? - C# WPF custom control not responding to XAML properties at design-time? 在设计时确定 C# 代码 class 文件的父目录路径 - Determine C# code class file's parent directory path at design-time C#:在运行时添加项目和子项目时,VirtualMode中的ListView不显示设计时列吗? - C#: ListView in VirtualMode not displaying design-time columns when adding items & subitems at runtime? C#Winforms Designer:如何在设计时复制和粘贴集合? - C# Winforms Designer: How to copy and paste collections at design-time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM