New CodeRush Plugin – Add Data Contract

A customer recently requested a way to quickly add the [DataContract] and [DataMember] attributes to the active class.

Naturally we were happy to oblige.

The Add Data Contract plugin provides a new CodeProvider available when your caret is within the name of any class.

When invoked, it will add the [DataContract] attribute to the active class, and decorate it’s properties with the [DataMember] attribute.


Naturally we also:

  • Add a reference to System.Runtime.Serialization.dll
  • Add the System.Runtime.Serialization namespace.

Where do I get this Plugin?

This plugin is available on GitHub or via the Visual Studio Gallery

More CodeRush Plugins added to the Visual Studio Gallery

For those of you who didn’t already know, several of our CodeRush plugins are already available from the Visual Studio Gallery.

Plugins added to the Visual Studio Gallery can be installed from within Studio via the Tools\Extensions and Updates menu.

Additionally Studio will let you know if a new version is released. Assuming you agree, it will download the new version and install it for you as well.

So we’ve just added another 5 plugins to those already up there making it even easier to pimp your IDE.

For reference these are the plugins we’ve just added:

To browse a full list of CodeRush plugins on Visual Studio Gallery just visit Tools\Extensions and Updates in Studio and search the online section for CodeRush

Alternatively, just follow this link.

New CodeRush Plugin Template - NewMetric

Note: The template referred to by this post does not ship with CodeRush. To install it, see instructions towards the end of the post.CodeRushMetric

What is a Metric

A metric is a measurement of some quality of your code. In this case it is a per-method measurement. CodeRush displays the metric for each member, on left of the member in question.

CodeRush ships with 3 such metrics:

  • Lines of Code
    Exactly what it sounds like. This metric measures the number of lines occupied by the method. This is a pretty basic metric which includes the signature and braces of the method.
  • Cyclomatic Complexity
    A measurement of the number of paths through your code. The higher this value, the more tests will be needed to cover the method’s behaviours correctly.
  • Maintenance Complexity
    This metric weights each element of the method according to how it affects the maintainability of said method. You can read more about this metric here

You can choose which metric CodeRush displays, by clicking the current value and choosing another metric from the list. CodeRushMetricChoice

The NewMetric Template

As you’ll have guessed based on previous posts in this series, the purpose of the NewMetric template is the quickest way to create your own metric?


As with other plugin templates, this one is intended to be expanded (NewMetric<space>) in the body of your plugin class.

It will produce code that looks like this:


As with other plugin templates, you’ll have to give your Metric 3 names.

As a quick reminder, these are:

  • Your own reference.
    The first name is your own reference. The phrase you type here, is used to name the objects and associated methods of your Metric.
  • Internal name
    This value is used internally by CodeRush. It should be unique, since it will be used to add your Metric to an internal dictionary. Later you can use this name to get a reference your Metric from another plugin.
  • Display Name
    This value is used for the text displayed when your provider is added to the drop down list of metrics provided inline.

Next up, you’ll have to call the registerXXXX method (just generated and named) from within your plugin’s InitializePlugin method.

Finally you need to implement your plugin’s new metric. This is achieved by adding code to the GetMetricValue handler (also stubbed out by the template).

Typically you’ll examine the source of your project and perform some sort of calculation based on the LanguageElement passed to you in e.LanguageElement. Then you’ll assign the result of this calculation to e.Value.

So if you were looking to replicate the lines of code metric, you might use code similar to:


..and that’s it.

In Summary…

  • Expand NewMetric template
  • Call RegisterXXXX from InitializePlugin
  • Implement your metric (possibly in a single line of code).

…and you’re done.

Where do I get this template?

As usual this template is available from the CodeRush Plugin Templates repository on GitHub

If you have any suggestions to improve any of the templates in this series, feel free to contact me via email or on twitter.

If you so inclined you could even fork the project, make some changes and send me a pull request.

New CodeRush Plugin Templates – NewCodeProvider and NewRefactoring

Note: The templates referred to by this post do not ship with CodeRush. To install them, see instructions towards the end of the post.

This post describes two more templates in my Plugin Templates series. The series itself is introduced in my previous post - So you want to write a CodeRush plugin


The NewRefactoring and NewCodeProvider templates are the single quickest way to get started creating Refactoring and CodeProvider(Collectively called ContentProviders) plugins

These types of plugins add items to the Red (Refactoring) and Blue(Code) sections of the CodeRush SmartTag menu.


The templates work very similarly to My previous NewAction template in terms of placement. ie You should expand them in the body of your plugin class and then call the generated register methods from within your plugin’s the InitializePlugin method

This is how the NewRefactoring template expands:


…And here is what you get when you expand the NewCodeProvider template


Note: As before, you’ll need to call the appropriate registration method from your InitializePlugin method

You can see they are structurally very similar. they both create Register, CheckAvailability and Apply methods. In each case there is a small amount of customization to be done in order to get your ContentProviders up and running.

You’ll have to give your ContentProvider 3 names.

  • Your own reference.
    The first name is your own reference. The phrase you type here is used to name the objects and associated methods of your ContentProvider.
  • Internal name
    This value is used internally by CodeRush. It should be unique, since it will be used to add  your ContentProviders to an internal dictionary. Later you can use this name to get a reference your ContentProvider from another plugin.
  • Display Name
    This value is used for the text displayed when your provider is added to the CodeRush SmartTag menus

Next you’ll have to provide the code for:

  • Your CheckAvailability method
  • Your Execute method.


Availability is about determining if it is currently appropriate for your ContentProvider to be present on the CodeRush SmartTag menu.

Your CheckAvailability method is where you do this. In the unlikely event that your Refactoring\CodeProvider is always supposed to be available, simply leave the code as it is.


In most cases, your ContentProvider will only make sense under certain circumstances. If these circumstances are not met, then it makes more sense to simple not show your ContentProvider in the SmartTag menu.

Consider the “Split String” refactoring. This refactoring is designed to allow you to change




It’s clear, there is no point in providing this Refactoring, unless the user’s caret is within a string.

Thus the SplitString_CheckAvailability method might look something like this:


See how it only sets ea.Available to true if the ActiveString is not null. ie The caret is on a string literal.

Of course you should feel free to make your determination in any way you see fit.

You could:

  • Analyse the code where the caret is.
  • Analyse the project or the files within it.
  • You could base your own availability on the availability of another CodeProvider.
  • Look for code smells or other patterns.

If you really want to, you could call out to the web, and check a webcam to see if there’s a blue moon Smile

Just keep in mind that the check you perform here, will be performed quite often as the user moves around the code. This code needs to be as fast as possible.


As you might imagine, the ContentProvider_Execute method is where you perform the main function of your ContentProvider.

You might:

The sky really is the limit.

Ok Cool, so how do I get these templates?

The NewRefactoring and NewCodeProvider templates are available in the CodeRush Plugin Templates repository on GitHub

You can import them the traditional way

OR you can use the Template Importer and point it at

  • This URL for the NewRefactoring template
  • This URL for the NewCodeProvider template

If you have any suggestions to improve any of the templates in this series, feel free to contact me via email or on twitter.

If you so inclined you could even fork the project, make some changes and send me a pull request.

New CodeRush Plugin - Template Importer

I was rereading my previous post New CodeRush Plugin Template – NewAction

In it I asked you to ….

  • Visit a page on github.
  • Locate an XML file.
  • Download that file.
  • …and finally Import the file.

However it occurs to me that this is rather lengthy and awkward.

What if we could automate that a little more?
What if we could write a plugin which was able to download templates and install them itself?
What if….

Aw nuts! Never mind ‘What if’……….Let’s just do it!


This is Template Importer.

Note: It’s pretty clear that I’m no UI expert, but that’s another good reason to open source and accept pull requests.

In brief:

It can do 2 amazing things.

  • It can import exactly 1 exported set of templates by providing the address of an exported template file to the 1st box and pressing Import.
  • It can import multiple sets of exported templates by providing the address of a package to the 2nd box and pressing Import.

More Detail

Import Templates (from the Web)

A exported set of templates (see Exporting and Importing CodeRush Templates) is just an XML file. They’re easy to create, easy to store and easy to publish for others to use. All that’s really needed is an easy way to strip them off the web, and import them into your system.

The top half of the screen does exactly this. You can specify the url of a raw template file, and then click the Import button.
(For your convenience, the little “>” button on the left will copy the example into the live box)

The example file is published on github, but there’s no limit to where you can put these files.

  • Host them on your intranet.
  • Put them in your dropbox account.
  • Attach them to your blog.
  • ftp them into place.
  • ‘git push’ them into place.

Anywhere they can be reached by http is good enough.

I’m planning on uploading a lot more of my templates to various appropriate github locations.

But wait …. It gets better!

Import Packages (from the Web)

Q: Hang on… What is a package?

A: A package is a simple xml file (Yes I like XML files. Sorry.) which contains the urls of one or more exported template files.

For example here is the contents of the example package that is listed within the TemplateImporter by default.


The xml is fairly simple.

The template node has a url attribute (pointing the way to a template file) and a language attribute (specifying the language of the templates within the file).

Of course you can add as many ‘template’ nodes as you want, which means that you can create single package file that lists all your favourite 3rd party templates and then import them onto any machine you like in one hit. Great for being able to pull your standard templates onto your co-worker’s machine.


The Current Template Importer is limited in a few ways.

  • Template Import only understands VB.Net and CSharp templates.
    There is no information held in a set of exported templates regarding the source language from which it came. The importer makes an intelligent guess based on the prefix of the filename you point it at. It understands Basic, VB, CSharp and CS.
    Note: Package Import does understand the other languages. As indicated, the package will contain a language attribute for each url resource listed. this is used by the importer in order to place the templates in the correct location.
  • There is no preview.
    There is no option to preview what you’re getting in advance of getting it. Unless of course you’re prepared to wade (and I mean wade) into the xml of the files you’re instructing the Importer to import.
  • There is no selective import.
    You get every template in the file(s) or nothing at all.
  • The import is additive only.
    It will add these templates at their own specified locations in addition to anything else that might be there already. This includes copies of the exact same templates that you imported 5 seconds previously. In order to get only the latest version of a set of templates previously imported, I recommend deleting any previous revisions of those templates prior to each import.


There are no real safeguards in place yet. Be careful.

  • Don’t import templates from a source unless you explicitly trust that source.
    Really this one should speak for itself.
  • Don’t export templates from outside your custom folder.
    If someone were to import those they’d wind up with duplicate core templates. I’m honestly not sure what the effect would be, but it would certainly cause a mess
    If templates are always exported from within custom, they they’ll import there as well. This then limits the potential mess to the users custom folder.
  • Only import templates into your Custom folder.
    If you only ever import templates into you custom folder, then you can always restore your system to normal simply by hosing that folder.

So where can I get it?

As usual, the source code and binary for this plugin are available on github

Like many others, this plugin is listed on the CodeRush Plugins Site.


You can either:

  • Bind a key to the ‘ImportTemplates’ command, and trigger it with the keystroke of your choice.


  • Use the ‘Import Templates’ menu item I’ve placed on the CodeRush menu.

In future when I provide one or more templates, you should be able to use this plugin to point directly at the file’s url and suck the templates straight off the web without all that tedious mucking about with Right click … Save As etc

More Posts