Uredi

Deli z drugimi prek


ITypeResolutionService Interface

Definition

Provides an interface to retrieve an assembly or type by name.

public interface class ITypeResolutionService
public interface ITypeResolutionService
type ITypeResolutionService = interface
Public Interface ITypeResolutionService

Examples

The following example control provides an interface to call methods of the ITypeResolutionService from design mode.

#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.dll>
#using <System.Design.dll>

using namespace System;
using namespace System::Collections;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Drawing;
using namespace System::Reflection;
using namespace System::Text;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Design;
using namespace System::Security::Permissions;

namespace ITypeResolutionServiceExample
{
   // Since this example provides a control-based user interface
   // in design mode, this designer passes window messages to the
   // controls at design time.
   public ref class WindowMessageDesigner: public System::Windows::Forms::Design::ControlDesigner
   {
   public:
      WindowMessageDesigner(){}

   protected:

      // Window procedure override passes events to the control.

      [SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
      virtual void WndProc( System::Windows::Forms::Message% m ) override
      {
         if ( m.HWnd == this->Control->Handle )
                  ControlDesigner::WndProc( m );
         else
                  this->DefWndProc( m );
      }

   public:
      virtual void DoDefaultAction() override {}

   };

   // This control provides an example design-time user interface to
   // the ITypeResolutionService.

   [DesignerAttribute(WindowMessageDesigner::typeid,IDesigner::typeid)]
   public ref class ITypeResolutionServiceControl: public System::Windows::Forms::UserControl
   {
   private:

      // Reference to a type resolution service interface, or null
      // if not obtained.
      ITypeResolutionService^ rs;

      // Textbox for input of assembly and type names.
      System::Windows::Forms::TextBox^ entryBox;

      // Textbox for output of assembly, type, and status information.
      System::Windows::Forms::TextBox^ infoBox;

      // Panel to contain the radio buttons used to select the
      // method to InvokeMethod.
      System::Windows::Forms::Panel^ panel1;
      System::Windows::Forms::RadioButton^ radioButton1;
      System::Windows::Forms::RadioButton^ radioButton2;
      System::Windows::Forms::RadioButton^ radioButton3;
      System::Windows::Forms::RadioButton^ radioButton4;

      // Label to display textbox entry information.
      System::Windows::Forms::Label ^ label1;

      // Button to InvokeMethod command.
      System::Windows::Forms::Button^ button1;

   public:
      ITypeResolutionServiceControl()
      {
         InitializeComponent();
         rs = nullptr;
         
         // Attaches event handlers for control clicked events.
         radioButton1->CheckedChanged += gcnew EventHandler( this, &ITypeResolutionServiceControl::GetAssembly );
         radioButton2->CheckedChanged += gcnew EventHandler( this, &ITypeResolutionServiceControl::GetPathToAssembly );
         radioButton3->CheckedChanged += gcnew EventHandler( this, &ITypeResolutionServiceControl::GetInstanceOfType );
         radioButton4->CheckedChanged += gcnew EventHandler( this, &ITypeResolutionServiceControl::ReferenceAssembly );
         button1->Click += gcnew EventHandler( this, &ITypeResolutionServiceControl::InvokeMethod );
         entryBox->KeyUp += gcnew KeyEventHandler( this, &ITypeResolutionServiceControl::InvokeOnEnterKeyHandler );
      }

   protected:

      // Displays example control name and status information.
      virtual void OnPaint( System::Windows::Forms::PaintEventArgs^ e ) override
      {
         e->Graphics->DrawString( "ITypeResolutionService Interface Control", gcnew System::Drawing::Font( FontFamily::GenericMonospace,9 ), gcnew SolidBrush( Color::Blue ), 5, 5 );
         if ( this->DesignMode )
                  e->Graphics->DrawString( "Currently in Design Mode", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::DarkGreen ), 350, 2 );
         else
                  e->Graphics->DrawString( "Requires Design Mode", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Red ), 350, 2 );

         if ( rs != nullptr )
                  e->Graphics->DrawString( "Type Resolution Service Obtained", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::DarkGreen ), 350, 12 );
         else
                  e->Graphics->DrawString( "Type Resolution Service Not Obtained", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Red ), 350, 12 );
      }

   private:

      // InvokeMethods the currently selected method, if any, when
      // the InvokeMethod button is pressed.
      void InvokeMethod( Object^ /*sender*/, EventArgs^ /*e*/ )
      {
         // If the GetAssembly or GetPathofAssembly radio button
         // is selected.
         if ( this->radioButton1->Checked || this->radioButton2->Checked || this->radioButton4->Checked )
         {
            if ( this->entryBox->Text->Length == 0 )
            {
               // If there is no assembly name specified, display status message.
               this->infoBox->Text = "You must enter the name of the assembly to retrieve.";
            }
            else
            if ( rs != nullptr )
            {
               // Create a System.Reflection.AssemblyName
               // for the entered text.
               AssemblyName^ name = gcnew AssemblyName;
               name->Name = this->entryBox->Text->Trim();

               // If the GetAssembly radio button is checked...
               if ( this->radioButton1->Checked )
               {
                  // Use the ITypeResolutionService to attempt to
                  // resolve an assembly reference.
                  Assembly^ a = rs->GetAssembly( name, false );

                  // If an assembly matching the specified name was not
                  // located in the GAC or local project references,
                  // display a message.
                  if ( a == nullptr )
                                    this->infoBox->Text = String::Format( "The {0} assembly could not be located.", this->entryBox->Text );
                  else
                  {
                     // An assembly matching the specified name was located.
                     // Builds a list of types.
                     array<Type^>^types = a->GetTypes();
                     StringBuilder^ sb = gcnew StringBuilder;
                     for ( int i = 0; i < types->Length; i++ )
                        sb->Append( String::Concat( types[ i ]->FullName, "\r\n" ) );
                     String^ path = rs->GetPathOfAssembly( name );
                     
                     // Displays assembly information and a list of types contained in the assembly.
                     this->infoBox->Text = String::Format( "Assembly located:\r\n\r\n{0}\r\n  at: {1}\r\n\r\nAssembly types:\r\n\r\n{2}", a->FullName, path, sb );
                  }
               }
               else
               if ( this->radioButton2->Checked )
               {
                  String^ path = rs->GetPathOfAssembly( name );
                  if ( path != nullptr )
                                    this->infoBox->Text = String::Format( "Assembly located at:\r\n{0}", path );
                  else
                                    this->infoBox->Text = "Assembly was not located.";
               }
               else
               if ( this->radioButton4->Checked )
               {
                  Assembly^ a = nullptr;
                  try
                  {
                     // Add a reference to the assembly to the
                     // current project.
                     rs->ReferenceAssembly( name );
                     
                     // Use the ITypeResolutionService to attempt
                     // to resolve an assembly reference.
                     a = rs->GetAssembly( name, false );
                  }
                  catch ( Exception^ ) 
                  {
                     // Catch this exception so that the exception
                     // does not interrupt control behavior.
                  }

                  // If an assembly matching the specified name was not
                  // located in the GAC or local project references,
                  // display a message.
                  if ( a == nullptr )
                                    this->infoBox->Text = String::Format( "The {0} assembly could not be located.", this->entryBox->Text );
                  else
                                    this->infoBox->Text = String::Format( "A reference to the {0} assembly has been added to the project's referenced assemblies.", a->FullName );
               }
            }
         }
         else
         if ( this->radioButton3->Checked )
         {
            if ( this->entryBox->Text->Length == 0 )
            {
               // If there is no type name specified, display a
               // status message.
               this->infoBox->Text = "You must enter the name of the type to retrieve.";
            }
            else
            if ( rs != nullptr )
            {
               Type^ type = nullptr;
               try
               {
                  type = rs->GetType( this->entryBox->Text, false, true );
               }
               catch ( Exception^ ) 
               {
                  // Consume exceptions raised during GetType call
               }

               if ( type != nullptr )
               {
                  // Display type information.
                  this->infoBox->Text = String::Format( "Type: {0} located.\r\n  Namespace: {1}\r\n{2}", type->FullName, type->Namespace, type->AssemblyQualifiedName );
               }
               else
                              this->infoBox->Text = "Type not located.";
            }
         }
      }

      void GetAssembly( Object^ /*sender*/, EventArgs^ /*e*/ )
      {
         if ( this->radioButton1->Checked )
                  this->label1->Text = "Enter the assembly name:";
      }

      void GetPathToAssembly( Object^ /*sender*/, EventArgs^ /*e*/ )
      {
         if ( this->radioButton2->Checked )
                  this->label1->Text = "Enter the assembly name:";
      }

      void GetInstanceOfType( Object^ /*sender*/, EventArgs^ /*e*/ )
      {
         if ( this->radioButton3->Checked )
                  this->label1->Text = "Enter the type name:";
      }

      void ReferenceAssembly( Object^ /*sender*/, EventArgs^ /*e*/ )
      {
         if ( this->radioButton4->Checked )
                  this->label1->Text = "Enter the assembly name:";
      }

      void InvokeOnEnterKeyHandler( Object^ sender, KeyEventArgs^ e )
      {
         if ( e->KeyCode == Keys::Enter )
                  this->InvokeMethod( sender, gcnew EventArgs );
      }

   public:

      property System::ComponentModel::ISite^ Site 
      {
         virtual System::ComponentModel::ISite^ get() override
         {
            return __super::Site;
         }

         virtual void set( System::ComponentModel::ISite^ value ) override
         {
            __super::Site = value;
            
            // Attempts to obtain ITypeResolutionService interface.
            rs = dynamic_cast<ITypeResolutionService^>(this->GetService( ITypeResolutionService::typeid ));
         }

      }

   private:
      void InitializeComponent()
      {
         this->entryBox = gcnew System::Windows::Forms::TextBox;
         this->infoBox = gcnew System::Windows::Forms::TextBox;
         this->panel1 = gcnew System::Windows::Forms::Panel;
         this->radioButton1 = gcnew System::Windows::Forms::RadioButton;
         this->radioButton2 = gcnew System::Windows::Forms::RadioButton;
         this->radioButton3 = gcnew System::Windows::Forms::RadioButton;
         this->radioButton4 = gcnew System::Windows::Forms::RadioButton;
         this->label1 = gcnew System::Windows::Forms::Label;
         this->button1 = gcnew System::Windows::Forms::Button;
         this->panel1->SuspendLayout();
         this->SuspendLayout();

         // entryBox
         this->entryBox->Location = System::Drawing::Point( 176, 80 );
         this->entryBox->Name = "entryBox";
         this->entryBox->Size = System::Drawing::Size( 320, 20 );
         this->entryBox->TabIndex = 0;
         this->entryBox->Text = "";

         // infoBox
         this->infoBox->Anchor = static_cast<AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) | System::Windows::Forms::AnchorStyles::Left) | System::Windows::Forms::AnchorStyles::Right);
         this->infoBox->Location = System::Drawing::Point( 16, 111 );
         this->infoBox->Multiline = true;
         this->infoBox->Name = "infoBox";
         this->infoBox->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
         this->infoBox->Size = System::Drawing::Size( 592, 305 );
         this->infoBox->TabIndex = 1;
         this->infoBox->Text = "";

         // panel1
         array<System::Windows::Forms::Control^>^temp0 = {this->radioButton4,this->radioButton3,this->radioButton2,this->radioButton1};
         this->panel1->Controls->AddRange( temp0 );
         this->panel1->Location = System::Drawing::Point( 16, 32 );
         this->panel1->Name = "panel1";
         this->panel1->Size = System::Drawing::Size( 480, 40 );
         this->panel1->TabIndex = 2;

         // radioButton1
         this->radioButton1->Location = System::Drawing::Point( 8, 8 );
         this->radioButton1->Name = "radioButton1";
         this->radioButton1->Size = System::Drawing::Size( 96, 24 );
         this->radioButton1->TabIndex = 0;
         this->radioButton1->Text = "GetAssembly";

         // radioButton2
         this->radioButton2->Location = System::Drawing::Point( 112, 8 );
         this->radioButton2->Name = "radioButton2";
         this->radioButton2->Size = System::Drawing::Size( 128, 24 );
         this->radioButton2->TabIndex = 1;
         this->radioButton2->Text = "GetPathToAssembly";

         // radioButton3
         this->radioButton3->Location = System::Drawing::Point( 248, 8 );
         this->radioButton3->Name = "radioButton3";
         this->radioButton3->Size = System::Drawing::Size( 80, 24 );
         this->radioButton3->TabIndex = 2;
         this->radioButton3->Text = "GetType";

         // radioButton4
         this->radioButton4->Location = System::Drawing::Point( 344, 8 );
         this->radioButton4->Name = "radioButton4";
         this->radioButton4->Size = System::Drawing::Size( 128, 24 );
         this->radioButton4->TabIndex = 3;
         this->radioButton4->Text = "ReferenceAssembly";

         // label1
         this->label1->Location = System::Drawing::Point( 18, 83 );
         this->label1->Name = "label1";
         this->label1->Size = System::Drawing::Size( 150, 16 );
         this->label1->TabIndex = 3;

         // button1
         this->button1->Location = System::Drawing::Point( 519, 41 );
         this->button1->Name = "button1";
         this->button1->TabIndex = 4;
         this->button1->Text = "Invoke";

         // ITypeResolutionServiceControl
         this->BackColor = System::Drawing::Color::White;
         array<System::Windows::Forms::Control^>^temp1 = {this->button1,this->label1,this->panel1,this->infoBox,this->entryBox};
         this->Controls->AddRange( temp1 );
         this->Name = "ITypeResolutionServiceControl";
         this->Size = System::Drawing::Size( 624, 432 );
         this->panel1->ResumeLayout( false );
         this->ResumeLayout( false );
      }
   };
}
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace ITypeResolutionServiceExample
{	
    // This control provides an example design-time user interface to 
    // the ITypeResolutionService.
    [DesignerAttribute(typeof(WindowMessageDesigner), typeof(IDesigner))]
    public class ITypeResolutionServiceControl : System.Windows.Forms.UserControl
    {        
        // Reference to a type resolution service interface, or null 
        // if not obtained.
        private ITypeResolutionService rs;
        // Textbox for input of assembly and type names.
        private System.Windows.Forms.TextBox entryBox;
        // Textbox for output of assembly, type, and status information.
        private System.Windows.Forms.TextBox infoBox;
        // Panel to contain the radio buttons used to select the 
        // method to InvokeMethod.
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.RadioButton radioButton3;
        private System.Windows.Forms.RadioButton radioButton4;
        // Label to display textbox entry information.
        private System.Windows.Forms.Label label1;
        // Button to InvokeMethod command.
        private System.Windows.Forms.Button button1;        

        public ITypeResolutionServiceControl()
        {
            InitializeComponent();
            rs = null;
            // Attaches event handlers for control clicked events.
            radioButton1.CheckedChanged += new EventHandler(this.GetAssembly);
            radioButton2.CheckedChanged += new EventHandler(this.GetPathToAssembly);
            radioButton3.CheckedChanged += new EventHandler(this.GetInstanceOfType);
            radioButton4.CheckedChanged += new EventHandler(this.ReferenceAssembly);
            button1.Click += new EventHandler(this.InvokeMethod);
            entryBox.KeyUp += new KeyEventHandler(this.InvokeOnEnterKeyHandler);
        }

        // Displays example control name and status information.
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.DrawString("ITypeResolutionService Interface Control", new Font(FontFamily.GenericMonospace, 9), new SolidBrush(Color.Blue), 5, 5);
            if(this.DesignMode)
                e.Graphics.DrawString("Currently in Design Mode", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.DarkGreen), 350, 2);
            else
                e.Graphics.DrawString("Requires Design Mode", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Red), 350, 2);
            if( rs != null )
                e.Graphics.DrawString("Type Resolution Service Obtained", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.DarkGreen), 350, 12);
            else
                e.Graphics.DrawString("Type Resolution Service Not Obtained", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Red), 350, 12);
        }

        // InvokeMethods the currently selected method, if any, when 
        // the InvokeMethod button is pressed.
        private void InvokeMethod(object sender, EventArgs e)
        {
            // If the GetAssembly or GetPathofAssembly radio button 
            // is selected.
            if(this.radioButton1.Checked || this.radioButton2.Checked || this.radioButton4.Checked)
            {
                if(this.entryBox.Text.Length == 0)
                {
                    // If there is no assembly name specified, display status message.
                    this.infoBox.Text = "You must enter the name of the assembly to retrieve.";
                }
                else if( rs != null )
                {
                    // Create a System.Reflection.AssemblyName 
                    // for the entered text.
                    AssemblyName name = new AssemblyName();                               
                    name.Name = this.entryBox.Text.Trim();                    

                    // If the GetAssembly radio button is checked...
                    if(this.radioButton1.Checked)
                    {
                        // Use the ITypeResolutionService to attempt to 
                        // resolve an assembly reference.
                        Assembly a = rs.GetAssembly( name, false );
                        // If an assembly matching the specified name was not 
                        // located in the GAC or local project references, 
                        // display a message.
                        if( a == null )
                        {
                            this.infoBox.Text = "The "+this.entryBox.Text+" assembly could not be located.";
                        }
                        else
                        {
                            // An assembly matching the specified name was located.
                            // Builds a list of types.
                            Type[] types = a.GetTypes();
                            StringBuilder sb = new StringBuilder();
                            for(int i=0; i<types.Length; i++)                        
                                sb.Append(types[i].FullName+"\r\n");
                            string path = rs.GetPathOfAssembly(name);
                            // Displays assembly information and a list of types contained in the assembly.
                            this.infoBox.Text = "Assembly located:\r\n\r\n"+a.FullName+"\r\n  at: "+path+"\r\n\r\nAssembly types:\r\n\r\n"+sb.ToString();
                        }
                    }
                    else if(this.radioButton2.Checked)
                    {
                        string path = rs.GetPathOfAssembly(name);
                        if( path != null )                        
                            this.infoBox.Text = "Assembly located at:\r\n"+path;
                        else
                            this.infoBox.Text = "Assembly was not located.";
                    }
                    else if(this.radioButton4.Checked)
                    {
                        Assembly a = null;
                        try
                        {
                            // Add a reference to the assembly to the 
                            // current project.
                            rs.ReferenceAssembly(name);
                            // Use the ITypeResolutionService to attempt 
                            // to resolve an assembly reference.                        
                            a = rs.GetAssembly( name, false );
                        }
                        catch
                        {     
                            // Catch this exception so that the exception 
                            // does not interrupt control behavior.
                        }
                        // If an assembly matching the specified name was not 
                        // located in the GAC or local project references, 
                        // display a message.
                        if( a == null )
                            this.infoBox.Text = "The "+this.entryBox.Text+" assembly could not be located.";
                        else                        
                            this.infoBox.Text = "A reference to the "+a.FullName+" assembly has been added to the project's referenced assemblies.";                        
                    }
                }
            }
            else if(this.radioButton3.Checked)
            {
                if(this.entryBox.Text.Length == 0)
                {
                    // If there is no type name specified, display a 
                    // status message.
                    this.infoBox.Text = "You must enter the name of the type to retrieve.";
                }
                else if( rs != null )
                {
                    Type type = null;
                    try
                    {
                        type = rs.GetType(this.entryBox.Text, false, true);
                    }
                    catch
                    {                
                        // Consume exceptions raised during GetType call
                    }
                    if( type != null )
                    {
                        // Display type information.
                        this.infoBox.Text = "Type: "+type.FullName+" located.\r\n  Namespace: "+type.Namespace+"\r\n"+type.AssemblyQualifiedName;
                    }
                    else
                    {
                        this.infoBox.Text = "Type not located.";
                    }
                }                
            }
        }

        private void GetAssembly(object sender, EventArgs e)
        {
            if(this.radioButton1.Checked)            
                this.label1.Text = "Enter the assembly name:";            
        }

        private void GetPathToAssembly(object sender, EventArgs e)
        {
            if(this.radioButton2.Checked)            
                this.label1.Text = "Enter the assembly name:";            
        }

        private void GetInstanceOfType(object sender, EventArgs e)
        {
            if(this.radioButton3.Checked)
            this.label1.Text = "Enter the type name:";            
        }
    
        private void ReferenceAssembly(object sender, EventArgs e)
        {
            if(this.radioButton4.Checked)           
                this.label1.Text = "Enter the assembly name:";
        }

        private void InvokeOnEnterKeyHandler(object sender, KeyEventArgs e)
        {
            if( e.KeyCode == Keys.Enter )            
                this.InvokeMethod(sender, new EventArgs());            
        }

        public override System.ComponentModel.ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                base.Site = value;
            
                // Attempts to obtain ITypeResolutionService interface.
                rs = (ITypeResolutionService)this.GetService(typeof(ITypeResolutionService));
            }
        }

        #region Component Designer generated code
        private void InitializeComponent()
        {
            this.entryBox = new System.Windows.Forms.TextBox();
            this.infoBox = new System.Windows.Forms.TextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton3 = new System.Windows.Forms.RadioButton();
            this.radioButton4 = new System.Windows.Forms.RadioButton();
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // entryBox
            this.entryBox.Location = new System.Drawing.Point(176, 80);
            this.entryBox.Name = "entryBox";
            this.entryBox.Size = new System.Drawing.Size(320, 20);
            this.entryBox.TabIndex = 0;
            this.entryBox.Text = "";
            // infoBox
            this.infoBox.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.infoBox.Location = new System.Drawing.Point(16, 111);
            this.infoBox.Multiline = true;
            this.infoBox.Name = "infoBox";
            this.infoBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.infoBox.Size = new System.Drawing.Size(592, 305);
            this.infoBox.TabIndex = 1;
            this.infoBox.Text = "";
            // panel1
            this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                this.radioButton4,
                                                                            this.radioButton3,
                                                                                this.radioButton2,
                                                                                this.radioButton1});
            this.panel1.Location = new System.Drawing.Point(16, 32);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(480, 40);
            this.panel1.TabIndex = 2;
            // radioButton1
            this.radioButton1.Location = new System.Drawing.Point(8, 8);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(96, 24);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.Text = "GetAssembly";
            // radioButton2
            this.radioButton2.Location = new System.Drawing.Point(112, 8);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(128, 24);
            this.radioButton2.TabIndex = 1;
            this.radioButton2.Text = "GetPathToAssembly";
            // radioButton3
            this.radioButton3.Location = new System.Drawing.Point(248, 8);
            this.radioButton3.Name = "radioButton3";
            this.radioButton3.Size = new System.Drawing.Size(80, 24);
            this.radioButton3.TabIndex = 2;
            this.radioButton3.Text = "GetType";
            // radioButton4
            this.radioButton4.Location = new System.Drawing.Point(344, 8);
            this.radioButton4.Name = "radioButton4";
            this.radioButton4.Size = new System.Drawing.Size(128, 24);
            this.radioButton4.TabIndex = 3;
            this.radioButton4.Text = "ReferenceAssembly";
            // label1
            this.label1.Location = new System.Drawing.Point(18, 83);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(150, 16);
            this.label1.TabIndex = 3;
            // button1
            this.button1.Location = new System.Drawing.Point(519, 41);
            this.button1.Name = "button1";
            this.button1.TabIndex = 4;
            this.button1.Text = "Invoke";
            // ITypeResolutionServiceControl
            this.BackColor = System.Drawing.Color.White;
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                        this.button1,
                                                                        this.label1,
                                                                        this.panel1,
                                                                        this.infoBox,
                                                                        this.entryBox});
            this.Name = "ITypeResolutionServiceControl";
            this.Size = new System.Drawing.Size(624, 432);
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);
        }
        #endregion
    }

    // Since this example provides a control-based user interface 
    // in design mode, this designer passes window messages to the 
    // controls at design time.    
    public class WindowMessageDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        public WindowMessageDesigner()
        {                       
        }

        // Window procedure override passes events to the control.
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {   
            if( m.HWnd == this.Control.Handle )            
                base.WndProc(ref m);            
            else            
                this.DefWndProc(ref m);            
        }

        public override void DoDefaultAction()
        {        
        }
    }
}
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Reflection
Imports System.Text
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

' This control provides an example design-time user interface to 
' the ITypeResolutionService.
<DesignerAttribute(GetType(WindowMessageDesigner), GetType(IDesigner))>  _
Public Class ITypeResolutionServiceControl
   Inherits System.Windows.Forms.UserControl
   ' Reference to a type resolution service interface, or null 
   ' if not obtained.
   Private rs As ITypeResolutionService
   ' Textbox for input of assembly and type names.
   Private entryBox As System.Windows.Forms.TextBox
   ' Textbox for output of assembly, type, and status information.
   Private infoBox As System.Windows.Forms.TextBox
   ' Panel to contain the radiobuttons used to select the 
   ' method to InvokeMethod.
   Private panel1 As System.Windows.Forms.Panel
   Private radioButton1 As System.Windows.Forms.RadioButton
   Private radioButton2 As System.Windows.Forms.RadioButton
   Private radioButton3 As System.Windows.Forms.RadioButton
   Private radioButton4 As System.Windows.Forms.RadioButton
   ' Label to display textbox entry information.
   Private label1 As System.Windows.Forms.Label
   ' Button to InvokeMethod command.
   Private button1 As System.Windows.Forms.Button

    Public Sub New()
        InitializeComponent()
        rs = Nothing
        ' Attaches event handlers for control clicked events.
        AddHandler radioButton1.CheckedChanged, AddressOf Me.GetAssembly
        AddHandler radioButton2.CheckedChanged, AddressOf Me.GetPathToAssembly
        AddHandler radioButton3.CheckedChanged, AddressOf Me.GetInstanceOfType
        AddHandler radioButton4.CheckedChanged, AddressOf Me.ReferenceAssembly
        AddHandler button1.Click, AddressOf Me.InvokeMethod
        AddHandler entryBox.KeyUp, AddressOf Me.InvokeOnEnterKeyHandler
    End Sub

    ' Displays example control name and status information.
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        e.Graphics.DrawString("ITypeResolutionService Interface Control", New Font(FontFamily.GenericMonospace, 9), New SolidBrush(Color.Blue), 5, 5)
        If Me.DesignMode Then
            e.Graphics.DrawString("Currently in Design Mode", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.DarkGreen), 350, 2)
        Else
            e.Graphics.DrawString("Requires Design Mode", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Red), 350, 2)
        End If
        If (rs IsNot Nothing) Then
            e.Graphics.DrawString("Type Resolution Service Obtained", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.DarkGreen), 350, 12)
        Else
            e.Graphics.DrawString("Type Resolution Service Not Obtained", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Red), 350, 12)
        End If
    End Sub

    ' InvokeMethods the currently selected method, if any, when 
    ' the InvokeMethod button is pressed.
    Private Sub InvokeMethod(ByVal sender As Object, ByVal e As EventArgs)
        ' If the GetAssembly or GetPathofAssembly radio button 
        ' is selected.
        If Me.radioButton1.Checked OrElse Me.radioButton2.Checked OrElse Me.radioButton4.Checked Then
            If Me.entryBox.Text.Length = 0 Then
                ' If there is no assembly name specified, display status message.
                Me.infoBox.Text = "You must enter the name of the assembly to retrieve."
            ElseIf (rs IsNot Nothing) Then
                ' Create a System.Reflection.AssemblyName 
                ' for the entered text.
                Dim name As New AssemblyName()
                name.Name = Me.entryBox.Text.Trim()

                ' If the GetAssembly radio button is checked...
                If Me.radioButton1.Checked Then
                    ' Use the ITypeResolutionService to attempt to 
                    ' resolve an assembly reference.
                    Dim a As [Assembly] = rs.GetAssembly(name, False)
                    ' If an assembly matching the specified name was not 
                    ' located in the GAC or local project references, 
                    ' display a message.
                    If a Is Nothing Then
                        Me.infoBox.Text = "The " + Me.entryBox.Text + " assembly could not be located."
                    Else
                        ' An assembly matching the specified name was located.
                        ' Builds a list of types.
                        Dim types As Type() = a.GetTypes()
                        Dim sb As New StringBuilder()
                        Dim i As Integer
                        For i = 0 To types.Length - 1
                            sb.Append((types(i).FullName + ControlChars.Cr + ControlChars.Lf))
                        Next i
                        Dim path As String = rs.GetPathOfAssembly(name)
                        ' Displays assembly information and a list of types contained in the assembly.
                        Me.infoBox.Text = "Assembly located:" + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf + a.FullName + ControlChars.Cr + ControlChars.Lf + "  at: " + path + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf + "Assembly types:" + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf + sb.ToString()
                    End If
                ElseIf Me.radioButton2.Checked Then
                    Dim path As String = rs.GetPathOfAssembly(name)
                    If (path IsNot Nothing) Then
                        Me.infoBox.Text = "Assembly located at:" + ControlChars.Cr + ControlChars.Lf + path
                    Else
                        Me.infoBox.Text = "Assembly was not located."
                    End If
                ElseIf Me.radioButton4.Checked Then
                    Dim a As [Assembly] = Nothing
                    Try
                        ' Add a reference to the assembly to the 
                        ' current project.
                        rs.ReferenceAssembly(name)
                        ' Use the ITypeResolutionService to attempt 
                        ' to resolve an assembly reference.
                        a = rs.GetAssembly(name, False)
                    Catch
                    End Try
                    ' Catch this exception so that the exception 
                    ' does not interrupt control behavior.
                    ' If an assembly matching the specified name was not 
                    ' located in the GAC or local project references, 
                    ' display a message.
                    If a Is Nothing Then
                        Me.infoBox.Text = "The " + Me.entryBox.Text + " assembly could not be located."
                    Else
                        Me.infoBox.Text = "A reference to the " + a.FullName + " assembly has been added to the project's referenced assemblies."
                    End If
                End If
            End If
        Else
            If Me.radioButton3.Checked Then
                If Me.entryBox.Text.Length = 0 Then
                    ' If there is no type name specified, display a 
                    ' status message.
                    Me.infoBox.Text = "You must enter the name of the type to retrieve."
                Else
                    If (rs IsNot Nothing) Then
                        Dim type As Type = Nothing
                        Try
                            type = rs.GetType(Me.entryBox.Text, False, True)
                        Catch  ' Consume exceptions raised during GetType call
                        End Try
                        If (type IsNot Nothing) Then
                            ' Display type information.
                            Me.infoBox.Text = "Type: " + type.FullName + " located." + ControlChars.Cr + ControlChars.Lf + "  Namespace: " + type.Namespace + ControlChars.Cr + ControlChars.Lf + type.AssemblyQualifiedName
                        Else
                            Me.infoBox.Text = "Type not located."
                        End If
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub GetAssembly(ByVal sender As Object, ByVal e As EventArgs)
        If Me.radioButton1.Checked Then
            Me.label1.Text = "Enter the assembly name:"
        End If
    End Sub

    Private Sub GetPathToAssembly(ByVal sender As Object, ByVal e As EventArgs)
        If Me.radioButton2.Checked Then
            Me.label1.Text = "Enter the assembly name:"
        End If
    End Sub

    Private Sub GetInstanceOfType(ByVal sender As Object, ByVal e As EventArgs)
        If Me.radioButton3.Checked Then
            Me.label1.Text = "Enter the type name:"
        End If
    End Sub

    Private Sub ReferenceAssembly(ByVal sender As Object, ByVal e As EventArgs)
        If Me.radioButton4.Checked Then
            Me.label1.Text = "Enter the assembly name:"
        End If
    End Sub

    Private Sub InvokeOnEnterKeyHandler(ByVal sender As Object, ByVal e As KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            Me.InvokeMethod(sender, New EventArgs())
        End If
    End Sub

    Public Overrides Property Site() As System.ComponentModel.ISite
        Get
            Return MyBase.Site
        End Get
        Set(ByVal Value As System.ComponentModel.ISite)
            MyBase.Site = Value

            ' Attempts to obtain ITypeResolutionService interface.
            rs = CType(Me.GetService(GetType(ITypeResolutionService)), ITypeResolutionService)
        End Set
    End Property

    Private Sub InitializeComponent()
        Me.entryBox = New System.Windows.Forms.TextBox()
        Me.infoBox = New System.Windows.Forms.TextBox()
        Me.panel1 = New System.Windows.Forms.Panel()
        Me.radioButton1 = New System.Windows.Forms.RadioButton()
        Me.radioButton2 = New System.Windows.Forms.RadioButton()
        Me.radioButton3 = New System.Windows.Forms.RadioButton()
        Me.radioButton4 = New System.Windows.Forms.RadioButton()
        Me.label1 = New System.Windows.Forms.Label()
        Me.button1 = New System.Windows.Forms.Button()
        Me.panel1.SuspendLayout()
        Me.SuspendLayout()
        ' entryBox
        Me.entryBox.Location = New System.Drawing.Point(176, 80)
        Me.entryBox.Name = "entryBox"
        Me.entryBox.Size = New System.Drawing.Size(320, 20)
        Me.entryBox.TabIndex = 0
        Me.entryBox.Text = ""
        ' infoBox
        Me.infoBox.Anchor = System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right
        Me.infoBox.Location = New System.Drawing.Point(16, 111)
        Me.infoBox.Multiline = True
        Me.infoBox.Name = "infoBox"
        Me.infoBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.infoBox.Size = New System.Drawing.Size(592, 305)
        Me.infoBox.TabIndex = 1
        Me.infoBox.Text = ""
        ' panel1
        Me.panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.radioButton4, Me.radioButton3, Me.radioButton2, Me.radioButton1})
        Me.panel1.Location = New System.Drawing.Point(16, 32)
        Me.panel1.Name = "panel1"
        Me.panel1.Size = New System.Drawing.Size(480, 40)
        Me.panel1.TabIndex = 2
        ' radioButton1
        Me.radioButton1.Location = New System.Drawing.Point(8, 8)
        Me.radioButton1.Name = "radioButton1"
        Me.radioButton1.Size = New System.Drawing.Size(96, 24)
        Me.radioButton1.TabIndex = 0
        Me.radioButton1.Text = "GetAssembly"
        ' radioButton2
        Me.radioButton2.Location = New System.Drawing.Point(112, 8)
        Me.radioButton2.Name = "radioButton2"
        Me.radioButton2.Size = New System.Drawing.Size(128, 24)
        Me.radioButton2.TabIndex = 1
        Me.radioButton2.Text = "GetPathToAssembly"
        ' radioButton3
        Me.radioButton3.Location = New System.Drawing.Point(248, 8)
        Me.radioButton3.Name = "radioButton3"
        Me.radioButton3.Size = New System.Drawing.Size(80, 24)
        Me.radioButton3.TabIndex = 2
        Me.radioButton3.Text = "GetType"
        ' radioButton4
        Me.radioButton4.Location = New System.Drawing.Point(344, 8)
        Me.radioButton4.Name = "radioButton4"
        Me.radioButton4.Size = New System.Drawing.Size(128, 24)
        Me.radioButton4.TabIndex = 3
        Me.radioButton4.Text = "ReferenceAssembly"
        ' label1
        Me.label1.Location = New System.Drawing.Point(18, 83)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(150, 16)
        Me.label1.TabIndex = 3
        ' button1
        Me.button1.Location = New System.Drawing.Point(519, 41)
        Me.button1.Name = "button1"
        Me.button1.TabIndex = 4
        Me.button1.Text = "Invoke"
        ' ITypeResolutionServiceControl
        Me.BackColor = System.Drawing.Color.White
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.button1, Me.label1, Me.panel1, Me.infoBox, Me.entryBox})
        Me.Name = "ITypeResolutionServiceControl"
        Me.Size = New System.Drawing.Size(624, 432)
        Me.panel1.ResumeLayout(False)
        Me.ResumeLayout(False)
    End Sub

End Class

' Since this example provides a control-based user interface 
' in design mode, this designer passes window messages to the 
' controls at design time.   
Public Class WindowMessageDesigner
    Inherits System.Windows.Forms.Design.ControlDesigner

    Public Sub New()
    End Sub

    ' Window procedure override passes events to the control.
    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.HWnd.Equals(Me.Control.Handle) Then
            MyBase.WndProc(m)
        Else
            Me.DefWndProc(m)
        End If
    End Sub

    Public Overrides Sub DoDefaultAction()
    End Sub

End Class

Remarks

This service is used to load types at design time.

Methods

GetAssembly(AssemblyName, Boolean)

Gets the requested assembly.

GetAssembly(AssemblyName)

Gets the requested assembly.

GetPathOfAssembly(AssemblyName)

Gets the path to the file from which the assembly was loaded.

GetType(String, Boolean, Boolean)

Loads a type with the specified name.

GetType(String, Boolean)

Loads a type with the specified name.

GetType(String)

Loads a type with the specified name.

ReferenceAssembly(AssemblyName)

Adds a reference to the specified assembly.

Applies to