views:

456

answers:

3

I am attempting to write a .NET component. 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?

A: 

Have you tried using 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.

Jon Skeet
+1  A: 

Visual Studio Automation and Extensibility would allow you access to that sort of information at design time, in the sense that you could have and add-in access the data at design time.

cmsjr
A: 

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

mreith