Referencing external assemblies and custom libraries
By default, AutoCode commands are compiled using the following assemblies:
- System.dll
- System.Core.dll
- System.Data.dll
- System.Xml.dll
- System.Drawing.dl
- System.Windows.Form.dll
- System.Xml.Linq.dll
- EnvDTE.dll
- EnvDTE80.dll
- EnvDTE90.dll
- EnvDTE100.dll (for Visual Studio 2010 only)
In cases where we need any other assembly, we can use the Assembly directive specifying the name of the assembly. For example, in the following template, we reference the assemblies System.Data.Entity and MyLibrary.dll:
<?xml version="1.0"?>
<Commands xmlns="http://schemas.devprojects.net/AutoCode/v4.0">
<Command name="ExternalLibsSample" version="1.0">
<Assemblies>
<Assembly name="System.Data.Entity.dll"/>
<Assembly name="@MyLibrary.dll"/>
</Assemblies>
…
There are some considerations to notice in this example:
- When the assembly is a custom library, the name of the assembly needs to be preceded with the ‘@’ symbol.
- The custom assembly and its custom references must be copied to the PublicAssemblies folder in the Visual Studio path, usually in:
"C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies" - The assembly dependencies must be also added to the template
Once we declare the assemblies, we can instantiate classes or invoke static methods defined in those assemblies. For example:
<#= MyNamespace.MyClass.MyStaticMethod() #>
Note that the full qualified name is used.
Using external code files
New in AutoCode 4 it is possible to separate command code from command definition. This means that we can specify an external file for the command code, just like code behind in ASPX.
The Class command is a good example. We have the .AUTOX file declaring the command features and a .CS file to override some command methods.
Class.autox
<?xml version="1.0"?>
<Commands xmlns="http://schemas.devprojects.net/AutoCode/v4.0">
<Command name="Class" version="1.0">
<Includes>
<Include file="Class.cs" />
</Includes>
<CommandBehavior>
<CommandLine shortcut="class" />
<ActiveDocument extensions=".cs"/>
</CommandBehavior>
<CommandInfo>
<LanguageCategory>CSharp</LanguageCategory>
<Category>Code</Category>
<Usage>
<![CDATA[<type1> <name1> ... <className> class]]>
</Usage>
<Example>int id string name person class</Example>
<Description>Constructs a Class with multiple properties.</Description>
<Author>DevProjects</Author>
<HelpUrl>http://help.devprojects.net/gallery/command/class</HelpUrl>
</CommandInfo>
<CommandCode language="csharp">
<Codes>
<Code id="Template" file="Class.txt" />
</Codes>
…
Class.cs
using System;
using System.IO;
using System.Collections.Generic;
using DevProjects.AutoCode.Commands;
public partial class ClassCommand : CommandBase
{
public class Property
{
public string Type { get; set; }
public string Name { get; set; }
}
private string ClassName;
private List<Property> Properties;
protected override bool BeforeExecute(string commandArgs)
{
if (Arguments.Length % 2 == 0)
{
CommandHelper.InvalidArguments("Please, specify property type and property name pairs ending with the class name.");
return false;
}
ClassName = DocumentHelper.ToPascalCase(Arguments[Arguments.Length-1]);
Properties = new List<Property>();
for (int n = 1; n < Arguments.Length; n+=2)
{
Properties.Add(new Property() { Type = Arguments[n-1], Name = Arguments[n] });
}
return true;
}
}
Summary
AutoCode uses a predefined set of assemblies references when compiling the command.
It is possible to add external references using the assembly directive in the .AUTOX file like in the example above.
This feature, together with the ability of using external files to override the command behavior, allows the development of more powerful commands.
Comments
