Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

AppDomain.TypeResolve Event

Occurs when the resolution of a type fails.

[Visual Basic]
Public Overridable Event TypeResolve As ResolveEventHandler
[C#]
public virtual event ResolveEventHandler TypeResolve;
[C++]
public: virtual __event ResolveEventHandler* TypeResolve;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type ResolveEventArgs containing data related to this event. The following ResolveEventArgs property provides information specific to this event.

Property Description
Name Gets the name of the item to resolve.

Remarks

The TypeResolve event occurs when the common language runtime is unable to determine the assembly that can create the requested type. This can occur if the type is defined in a dynamic assembly, or the type is not defined in a dynamic assembly but the runtime does not know which assembly the type is defined in. The latter situation can occur when Type.GetType is called with a type name that is not qualified with the assembly name.

The ResolveEventHandler for this event can attempt to locate and create the type.

However, the TypeResolve event does not occur if the runtime knows it is not possible to find a type in certain assemblies. For example, this event does not occur if the type is not found in a static assembly because the runtime knows types cannot be added dynamically to static assemblies.

To register an event handler for this event, you must have the permissions described in the Permissions section. If you do not have the appropriate permissions, a SecurityException occurs.

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, C++] The following sample demonstrates the TypeResolve event.

[Visual Basic, C#, C++] For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.

[Visual Basic] 
Imports System
Imports System.Reflection
Imports System.Windows.Forms

Module Test

   Sub Main()
      ' You must supply a valid fully qualified assembly name here. 
      FindType("System.Windows.Forms.TextBox, System.Windows.FormsSystem.Windows.Forms, Version, Culture, PublicKeyToken")
      ' OK!

      FindType("System.Windows.Forms.TextBox")
      ' Failed!

      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      AddHandler currentDomain.TypeResolve, AddressOf MyResolver
      
      FindType("System.Windows.Forms.TextBox")
      ' OK!
   End Sub 'Main
   
   Sub FindType(name As String)
      Try
         Dim t As Type = Type.GetType(name, True)
         Console.WriteLine(t.FullName)
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'FindType

   Function MyResolver(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Console.WriteLine("MyResolver fired...")
      Return GetType(TextBox).Assembly
   End Function 'MyResolver

End Module 'Test

[C#] 
using System;
using System.Reflection;
using System.Windows.Forms;

class Test {
   public static void Main() {
      // You must supply a valid fully qualified assembly name here. 
      FindType("System.Windows.Forms.TextBox, System.Windows.Forms, Version, Culture, PublicKeyToken");
      // OK!

      FindType("System.Windows.Forms.TextBox");
      // Failed!

      AppDomain currentDomain = AppDomain.CurrentDomain;
      currentDomain.TypeResolve += new ResolveEventHandler(MyResolver);
      
      FindType("System.Windows.Forms.TextBox");
      // OK!
   }

   static void FindType(string name) {
      try {
         Type t = Type.GetType(name, true);
         Console.WriteLine(t.FullName);
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }
   
   static Assembly MyResolver(object sender, ResolveEventArgs args) {
      Console.WriteLine("MyResolver fired...");
      return typeof(TextBox).Assembly;
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Windows::Forms;

__gc class Test {
   static void FindType(String* name) {
      try {
         Type*  t = Type::GetType(name, true);
         Console::WriteLine(t->FullName);
      } catch (Exception* e) {
         Console::WriteLine(e->Message);
      }
   }

   static Assembly* MyResolver(Object* sender, ResolveEventArgs* args) {
      Console::WriteLine(S"MyResolver fired...");
      return __typeof(TextBox)->Assembly;
   }
public:
   static void test() {
      // You must supply a valid fully qualified assembly name here.
      FindType(S"System.Windows.Forms.TextBox, System.Windows.Forms, Version, Culture, PublicKeyToken");
      // OK!

      FindType(S"System.Windows.Forms.TextBox");
      // Failed!

      AppDomain*  currentDomain = AppDomain::CurrentDomain;
      currentDomain->TypeResolve += new ResolveEventHandler(0, MyResolver);

      FindType(S"System.Windows.Forms.TextBox");
      // OK!
   }

};

int main(){
   Test::test();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

AppDomain Class | AppDomain Members | System Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft