It is possible to support the “new” operator in VSTA
add-ins. Integration styles which do not
use a full proxy, such as NoRuntime and ProxyShim, can easily support this
feature. All that is required is public
constructor methods in the host application.
In the ShapeAppDyanmicProgrammingModelCSharp sample an additional code
change is required in order to ensure all host items created in the add-in
using the “new” operator are hooked-up prior to building (for more information
on this please see the blog ShapeAppDynamicProgrammingModelCSharp_ProxyShim
Sample). This is possible because
these styles of integration do not use a full proxy, which generally cannot
support the new operator and must instead use factory methods. One exception to this rule is for structs and
custom exceptions in VSTA v 2 add-ins.
Because structs and custom exceptions are reimplemented in the proxy
their public constructors are available for VSTA v 2 add-ins. For more information on the different styles
of integration, please see the blog Integration
Styles and the Benefits: SDK, NoRuntime, and ProxyShim.
Below are code samples which show how to create a new Drawing
object for the ShapeAppCSharp samples in the SDK, NoRuntime, and ProxyShim
style integrations:
NoRuntime and ProxyShim:
Both integration styles can use public constructor
methods, in this example a public constructor for the Drawing class was added
by changing the existing private constructor to public. Note that the factory method may still be
used.
//create a new Drawing using a public constructor
Drawing d = new
Drawing(this.Application,
"Drawing4");
'create a new drawing using a public constructor
Dim d As
Drawing = New Drawing(Me.Application,
"Drawing4")
SDK Factory Method:
This code utilizes the factory method Application.NewDrawing
(this is from the VSTA v 1 samples but applies to VSTA v 2 as well).
//create a new drawing using the factory method
Drawing d = this.NewDrawing();
'create a new drawing using the factory method
Dim d As
Drawing = Me.NewDrawing()
SDK Structs and Custom Exceptions (VSTA v 2 only):
Because structs and custom exceptions are
reimplemented in the proxy layer, their public constructors are available.
//factory methods, these are static methods in
the application class
Color cFM =
Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp.
Application.CreateColor(System.Drawing.Color.Green.ToArgb());
Point pFM =
Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp.
Application.CreatePoint(25, 25);
Size sFM =
Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp.
Application.CreateSize(25, 25);
//public constructos for structs and custom
exceptions
Color cPC = new
Color(System.Drawing.Color.Green);
Point pPC = new
Point(25,25);
Size sPC = new
Size(25, 25);
LocationInvalidException liePC= new LocationInvalidException("invalid location");
SizeInvalidException siePC = new SizeInvalidException("invalid size");
'factory methods, these are static methods in the
application class
Dim cFM As
Color = Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp. _
Application.CreateColor(System.Drawing.Color.Green.ToArgb())
Dim pFM As
Point = Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp. _
Application.CreatePoint(25, 25)
Dim sFM As
Size = Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp. _
Application.CreateSize(25, 25)
'public constructos for structs and custom
exceptions
Dim cPC As
Color = New Color(System.Drawing.Color.Green)
Dim pPC As
Point = New Point(25, 25)
Dim sPC As
Size = New Size(25, 25)
Dim liePC As
LocationInvalidException = New
LocationInvalidException("invalid
location")
Dim siePC As
SizeInvalidException = New
SizeInvalidException("invalid size")
Posted
Oct 20 2010, 11:02 AM
by
Melody