Exception thrown when using XSLT extensions

This is a question I was asked today but it's also something which I have come across myself when creating XSLT extensions.

Have you ever had this exception thrown?

System.MissingMethodException: No parameterless constructor defined for this object.

at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at umbraco.macro.GetXsltExtensions() at umbraco.macro.AddMacroXsltExtensions() at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

(The complete stack trace may be different, it's the thrown exception that should be of note)

So what causes this? Well Umbraco loads its XSLT extensions (from xsltExtensions.config) using Reflection, and it looks for a public default constructor, which is the constructor which takes no arguments.

Basically if you're writing a constructor for your XSLT extensions class you must make sure you have a default one too, so your extensions class must look like this at lease:

public class MyXsltExtensions { 
  public MyXsltExtensions() { }
  ...
}

If you're not defining your own constructor though this isn't a problem.

I only came across this bug when I was trying to define the default constructor as private, attempting to do a very tight API design (not exposing constructors where I didn't want them). Whoops!

umbracoxslt
Posted by: Aaron Powell
Last revised: 25 Apr, 2010 12:10 PM History

Comments

09 Jun, 2010 11:45 AM

Aaron,

Something that got me recently was creating the xsltextension class as

public static class MyClass{

}

Yes you need to have static methods for xsl extensions to work however do not make the actual class static.

DOH!

Regards

Ismail

09 Jun, 2010 10:44 PM

Yeah, like I said you need to have a public constructor. Static classes don't have them ;)

No new comments are allowed on this post.