Classes in template

Feb 26, 2011 at 7:25 PM

Is it possible to create classes within a template? Something like...

public class MyClass {
public MyClass() {
Three = new List<string>();

public string One { get; set; }
public int Two { get; set; }
public List<string> Three { get; set; }

Currently I get "Unable to compile template. Check the Errors list for details." when I try to do this. I would like to take XML content and use XmlSerializer to create an instance of MyClass within the template. I can't do the deserialization before hand and shove it into the model because the classes could vary depending on the template.

Feb 27, 2011 at 6:33 AM


I'm not sure that is currently possible. When you use codeblocks (@{ }), you're actually writing code within a method, e.g. your above code would do something like:

public void Execute()
    public class MyClass {
        public MyClass() {
            Three = new List<string>();

        public string One { get; set; }
        public int Two { get; set; }
        public List<string> Three { get; set;}

...which of course, is not valid C#. The other problem you will face, is that to use xml serialisation/deserialisation, the type must be known, but if you are defining your type within the template itself, how could do deserialise it in the first place?

What you could do, is use a custom base template:

public class CustomTemplateBase<T> : TemplateBase<T>
    public dynamic Instance { get; set; }

    public dynamic CreateInstance(string typeName)
        Type type = Type.GetType(typeName);
        // You'd to your deserialisation here, I'm going to
        // just cheat and return a new instance.
        return Activator.CreateInstance(type);

Using a dynamic property and dynamic return type, we've defined a method that will let us create an instance (through activation or deserialisation, etc.) and call member access on it. To use that in a template, you could then do:

  Instance = CreateInstance("ConsoleApplication1.MyClass, ConsoleApplication1");
  Instance.One = "Hello World";
Where "MyClass" is a defined somewhere in my application. The important thing is, I'm creating an instance per template.

Feb 27, 2011 at 4:01 PM

I hadn't thought of using dynamic but I think this will work nicely. Something like this will be perfect as I won't need to define classes. I am still in design phase and using JSON will actually work out nicer than XML. Thanks