Nested Types in VSTA v 2 Part I: Types Nested in Non-Entry Point Classes

ProxyGen does not support nested types; however, with a little manual editing of the descriptor and proxy files nested types can be supported in the Proxy.  There are three kinds of nested types that will be covered in this blog series (non-entry point, entry point, and implicitly declared events) and a sample will be available for download demonstrating how to handle each kind of nested type. 

The first kind of nest type is a type nested in a non-entry point class.    When exposing the Object Model to ProxyGen to create the descriptor file, ProxyGen will display error 11110- that a proxy cannot be generated for the outer type.  In the descriptor file, the nested type will be extracted from the outer type and all instances of the isExcluded attribute will be marked true in the nested type (note- this includes all methods and properties as well as the get and set within properties). 

Once the nested types and their methods and properties have been marked included, the updated descriptor file can be run through ProxyGen to create a proxy file with no errors from ProxyGen.  In the proxy file, the nested type will appear in its own namespace like “Namespace.OuterType”.  Move the nested class out of this namespace and place it in the original outer type.  The namespace which contained the outer type can now be removed.  No changes to the HostTypeMapProvider are necessary.

Workaround:

1)      Create the descriptor file as normal.

2)      In descriptor file, change the isExcluded attribute from true to false for all types, methods and properties in the nested type to expose. 

3)      Create the proxy file as normal.

4)      In the proxy file, move the nested type from the namespace “Namespace.OuterType” into the outer class (or type).

Types from the host:
Partial Public Class Application

    Public Sub New()

 

        'create a new instance of the nested type

        mAppMyNestedObj = New AppMyNestedObjectType("hello nested world")

 

    End Sub

 

    'Application's property of the nested type

    Private mAppMyNestedObj As AppMyNestedObjectType

    Public Property AppMyNestedObj() As AppMyNestedObjectType

        Get

            Return mAppMyNestedObj

        End Get

        Set(ByVal value As AppMyNestedObjectType)

            mAppMyNestedObj = value

        End Set

    End Property

 

    'Type nested in the Application class

    Public Class AppMyNestedObjectType

 

        Public Sub New(ByVal nID As String)

            mID = nID

        End Sub

 

        Private mID As String

        Public Property ID() As String

            Get

                Return mID

            End Get

            Set(ByVal value As String)

                mID = value

            End Set

        End Property

 

        Public Sub DoSomething()

            'do something

        End Sub

 

    End Class

 

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

ProxyGen.exe Information: 0 : Finished

ProxyGen.exe Information: 0 :   1 Errors

 

c:\Program Files\Microsoft Visual Studio 9.0\VC>

 

Descriptor file (changes in bold):

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

  <Property originalName="AppMyNestedObj" isExcluded="false">

    <Type>

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

    </Type>

    <Get isExcluded="false" />

    <Set isExcluded="false" />

  </Property>

</Class>

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

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

  <Method originalName="DoSomething" isExcluded="false">

    <ReturnType>

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

    </ReturnType>

  </Method>

  <Property originalName="ID" isExcluded="false">

    <Type>

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

    </Type>

    <Get isExcluded="false" />

    <Set isExcluded="false" />

  </Property>

</Class>

 

Modified Proxy File (changes in bold):

namespace NestedTypes_Workaround_VB

{

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

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

   {

      public virtual global::NestedTypes_Workaround_VB.Application.AppMyNestedObjectType AppMyNestedObj

      {

         [global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostMemberAttribute("AppMyNestedObj", BindingFlags = global::System.Reflection.BindingFlags.Instance | global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.GetProperty)]

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

         [global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostMemberAttribute("AppMyNestedObj", BindingFlags = global::System.Reflection.BindingFlags.Instance | global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.SetProperty)]

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

      }

 

      //Nested Type Fix:  Moved the nested type from the namespace "Namespace.OuterType" into the outer type

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

 

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

      {

         public virtual string ID

         {

            [global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostMemberAttribute("ID", BindingFlags = global::System.Reflection.BindingFlags.Instance | global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.GetProperty)]

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

            [global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostMemberAttribute("ID", BindingFlags = global::System.Reflection.BindingFlags.Instance | global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.SetProperty)]

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

         }

        

         [global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostMemberAttribute("DoSomething", BindingFlags = global::System.Reflection.BindingFlags.Instance | global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.InvokeMethod)]

         public virtual void DoSomething() { throw new global::System.NotImplementedException(); }

      }

      //END Nested Type Fix move

   }

}

 

//Nested Type Fix:  Move the nested type from the "Namespace.OuterType" namespace into the

// outer type.  Remove this namespace

//namespace NestedTypes_Workaround_VB.Application

//{

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

//   public abstract partial class AppMyNestedObjectType : global::System.MarshalByRefObject

//   {

//      public virtual string ID

//      {

//         [global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostMemberAttribute("ID", BindingFlags = global::System.Reflection.BindingFlags.Instance | global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.GetProperty)]

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

//         [global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostMemberAttribute("ID", BindingFlags = global::System.Reflection.BindingFlags.Instance | global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.SetProperty)]

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

//      }

 

//      [global::Microsoft.VisualStudio.Tools.Applications.Runtime.HostMemberAttribute("DoSomething", BindingFlags = global::System.Reflection.BindingFlags.Instance | global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.InvokeMethod)]

//      public virtual void DoSomething() { throw new global::System.NotImplementedException(); }

//   }

//}


*This workaround also works for types nested in entry point classes.  If the declaring class is an entry point no additional changes in the proxy are required.

Posted May 27 2008, 05:00 PM by Melody
Copyright Summit Software Company, 2008. All rights reserved.