2

我正在尝试编写一个 .NET 组件。该组件将被拖放到表单/用户控件上,并且需要在设计时访问组件父表单/用户控件引用的程序集中的属性。是否有可能在设计时获得这些组件?

4

3 回答 3

1

Visual Studio 自动化和可扩展性将允许您在设计时访问此类信息,从某种意义上说,您可以在设计时拥有和加载项访问数据。

于 2009-01-11T00:29:11.463 回答
0

您是否尝试过使用Assembly.GetReferencedAssemblies

编辑:

我取消删除了这篇文章,因为你没有任何其他回复。当我最初回答时,我没有正确阅读问题,所以没有看到“设计时”部分。另一方面,也许这并不重要——这至少给了你一些尝试的机会。

祝你好运,如果这是一场疯狂的追逐,我们深表歉意。

于 2009-01-10T23:38:14.983 回答
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);
            }
        }
    }

}

感谢您的回答!

麦克风

于 2009-01-14T03:43:38.620 回答