Melody's VSTA Blog

Get the latest and greatest information on VSTA here! Find new samples, workarounds, and inside tips in our downloads and blogs. Get your questions answered quickly in our forums or search our site for a FAQ. We're here to help- just ask.

Nested Types in VSTA v 2 Part II: Implicitly Declared Events in VB in Non-Entry Point Classes

In Visual Basic, events can be declared explicitly or implicitly typed.  When events are declared implicitly typed, Visual Basic creates a hidden type nested in the declaring type’s class.  ProxyGen v 2 does not support nested types; however, the work around given in Part I for nested types in non-entry point classes can be applied to this hidden nested type. 

The workaround for implicitly declared events is very similar to the workaround for non-entry point nested types (from Part I).  The nested event handler will appear in the same namespace as the outer class, but needs to be moved within the class.  Corrections are needed to the syntax of the event type and the name of the nested event handler.

Workaround for implicitly declared events:

1)      Create the descriptor file as normal.

2)      In descriptor file, change the isExcluded attribute from true to false for the handler created by Visual Basic for the implicitly declared event to expose. 

3)      Create the proxy file as normal.

4)      In the proxy file, move the nested handler type into the class in which the event was declared.

5)      Correct the delegate name from OuterClass_ImplicitEventHandler to ImplicitEventHandler

6)      Correct the syntax of the event (within the class) from global::NameSpace.OuterClass_ImplicitEventEventHandler to globall::NameSpace.OuterClass.ImplicitEventEventHandler

From the host:

Partial Public Class Application

 

    'explicit events require no work around

    Public Event ExplicitEvent As System.EventHandler

 

    'implicit events create hidden nested types which require a work around

    Public Event ImplicitEvent(ByVal sender As Object, ByVal e As System.EventArgs)

 

End Class

ProxyGen error:

ProxyGen.exe Error: 11110 : Cannot generate a proxy for the following types, bec

ause they contain an inner type. ProxyGen does not support nested types. In the

XML descriptor file, nested types are extracted from the outer type and marked a

s excluded. To generate proxy code for these types, you must modify the XML desc

riptor file.

    NestedTypes_Workaround_VB.Application

    NestedTypes_Workaround_VB.Application.ImplicitEventEventHandler

ProxyGen.exe Information: 0 : Finished

ProxyGen.exe Information: 0 :   1 Errors

 

Descriptor file (changes in bold):

<Class originalFullyQualifiedName="NestedTypes_Workaround_VB.Application" isExcluded="false" isAddInEntryPoint="false">

  <Event originalName="ExplicitEvent" isExcluded="false">

    <Type>

      <ExternalTypeReference isInterface="false" type="System.EventHandler" />

    </Type>

  </Event>

  <Event originalName="ImplicitEvent" isExcluded="false">

    <Type>

      <TypeReference type="NestedTypes_Workaround_VB.Application.ImplicitEventEventHandler" />

    </Type>

  </Event>

</Class>

 

<!--This is the hidden nested event handler used to deal with implicitly typed events-->

<!--Nested Types Fix:  change isExcluded to false-->

<Delegate originalFullyQualifiedName="NestedTypes_Workaround_VB.Application.ImplicitEventEventHandler" newName="Application_ImplicitEventEventHandler" isExcluded="false" newNamespace="NestedTypes_Workaround_VB">

  <Parameter originalName="sender">

    <Type>

      <ExternalTypeReference isInterface="false" type="System.Object" />

    </Type>

  </Parameter>

  <Parameter originalName="e">

    <Type>

      <ExternalTypeReference isInterface="false" type="System.EventArgs" />

    </Type>

  </Parameter>

  <Type>

    <ExternalTypeReference isInterface="false" type="System.Void" />

  </Type>

</Delegate>

 

Modified Proxy File (changes in bold):

[global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostTypeAttribute("NestedTypes_Workaround_VB, NestedTypes_Workaround_VB.Application")]

public abstract partial class Application : global::System.MarshalByRefObject

{

 

    virtual public event global::System.EventHandler ExplicitEvent

    {

 

        add { throw new global::System.NotImplementedException(); }

 

        remove { throw new global::System.NotImplementedException(); }

    }

 

    //Nested Type Fix 3:  Corrected syntax of type from _ to .

    //virtual public event global:: NestedTypes_Workaround_VB.Application_ImplicitEventEventHandler ImplicitEvent

    virtual public event global:: NestedTypes_Workaround_VB.Application.ImplicitEventEventHandler ImplicitEvent

    {

 

        add { throw new global::System.NotImplementedException(); }

 

        remove { throw new global::System.NotImplementedException(); }

    }

 

    //Nested Type Fix 1:  Moved the nested event handler into declaring class

    [global::Microsoft.VisualStudio.Tools.Applications.Runtime. HostTypeAttribute("NestedTypes_Workaround_VB, NestedTypes_Workaround_VB.Application.ImplicitEventEventHandler")]

 

    //Nested Type Fix 2:  Corrected delegate name removing OuterClass_

    //public delegate void Application_ImplicitEventEventHandler(object sender, global::System.EventArgs e);

    public delegate void ImplicitEventEventHandler(object sender, global::System.EventArgs e);

}

 

//Nested Type Fix 1:  Move the nested event handler into the declaring class

//[global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostTypeAttribute("NestedTypes_Workaround_VB, NestedTypes_Workaround_VB.Application.ImplicitEventEventHandler")]

//public delegate void Application_ImplicitEventEventHandler(object sender, global::System.EventArgs e);

 

It may be a easier to see the changes like this (parts renamed, and parts moved):

Original Proxy:

[global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostTypeAttribute("NestedTypes_Workaround_VB, NestedTypes_Workaround_VB.Application")]

public abstract partial class Application : global::System.MarshalByRefObject

{

 

    virtual public event global::System.EventHandler ExplicitEvent

    {

 

        add { throw new global::System.NotImplementedException(); }

 

        remove { throw new global::System.NotImplementedException(); }

    }

 

    virtual public event global:: NestedTypes_Workaround_VB.Application_ImplicitEventEventHandler ImplicitEvent

    {

 

        add { throw new global::System.NotImplementedException(); }

 

        remove { throw new global::System.NotImplementedException(); }

    }

}

 

[global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostTypeAttribute("NestedTypes_Workaround_VB, NestedTypes_Workaround_VB.Application.ImplicitEventEventHandler")]

public delegate void Application_ImplicitEventEventHandler(object sender, global::System.EventArgs e);

 

Modified Proxy:

[global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostTypeAttribute("NestedTypes_Workaround_VB, NestedTypes_Workaround_VB.Application")]

public abstract partial class Application : global::System.MarshalByRefObject

{

 

    virtual public event global::System.EventHandler ExplicitEvent

    {

 

        add { throw new global::System.NotImplementedException(); }

 

        remove { throw new global::System.NotImplementedException(); }

    }

 

    virtual public event global:: NestedTypes_Workaround_VB.Application.ImplicitEventEventHandler ImplicitEvent

    {

 

        add { throw new global::System.NotImplementedException(); }

 

        remove { throw new global::System.NotImplementedException(); }

    }

 

    [global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostTypeAttribute("NestedTypes_Workaround_VB, NestedTypes_Workaround_VB.Application.ImplicitEventEventHandler")]

    public delegate void ImplicitEventEventHandler(object sender, global::System.EventArgs e);

}

*Updated to reflect that this workaround is for implicitly declared events in non-entry point classes
Published Wednesday, May 28, 2008 2:45 PM by Melody

Comments

  • No Comments
Anonymous comments are disabled