Generics Part I: Generic Parameters of Intrinsic Types

With VSTA v 2 it is possible to use generic parameters of intrinsic types with minimal effort.  The example below demonstrates this with a method added to the ShapeApp.Application class which accepts a List<string>.

Steps:
1)       Create the proxy file ignoring the ProxyGen warnings:
ProxyGen.exe Warning: 21070 : Generics are not supported. The following types contain generic parameters and will be excluded. Its occurrences will be replaced by System.Object.
    List`1 

2)       In the proxy file, the generic types were replaced with System.Object.  Change the object type back to the type List<String>.

3)       In the HostTypeMapProvider.GetTypeForCanonicalName, add a check for these types and return them if found.

Host:

using System;
using System.Collections.Generic;

public partial class Application
{

   public int ReturnCount(List<string> listToCount)
   {      return listToCount.Count;   }
} 

HostTypeMapProvider:
internal partial class HostTypeMapProvider
{
   public System.Type GetTypeForCanonicalName(string canonicalName)
   {
       //default fetch from the dictionary
       if (canonicalToTypeName.ContainsKey(canonicalName))
       {
           return canonicalToTypeName[canonicalName];
       } 

       //check for List<string>

       if (canonicalName.Contains("List") && canonicalName.Contains("String"))
           return typeof(System.Collections.Generic.List<string>);

        return null;
   }
}

Proxy:
public abstract partial class Application : global::System.MarshalByRefObject
{
   //change type from object to List<string>
   [global::Microsoft.VisualStudio.Tools.Applications.Runtime. HostMemberAttribute("ReturnCount", BindingFlags = global::System.Reflection.BindingFlags.Instance | global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.InvokeMethod)]

   //public virtual int ReturnCount(object listToCount) { throw new global::System.NotImplementedException(); }
   public virtual int ReturnCount(List<string> listToCount) { throw new global::System.NotImplementedException(); }
}

public partial class ApplicationEntryPoint
{
   //change type from object to List<string>
   //public virtual int ReturnCount(object listToCount)
   public virtual int ReturnCount(List<string> listToCount)
   {       return this.RemoteObject.ReturnCount(listToCount);   }
}

 


Posted Oct 24 2008, 03:26 PM by Melody
Filed under: , , ,
Copyright Summit Software Company, 2008. All rights reserved.