Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 a namespace cannot directly contain membes such as fields or methods

Tags:

c#

t4

Hi I am just starting out with T4 templates and I need to generate a javascript file based on the actions in my controller.

I got the code all figured out forgetting the controllers and actions my only problem is that I am getting this error in the T4 template file and I do not understand it:

Compiling transformation: A namespace cannot directly contain members such as fields or methods

This is my code:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(TargetPath)" #>

<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="eConnect.WebApi.Helpers.T4.ControllerDetails" #>
<#@ import namespace="System.Web.Http;"#>
<#@ output extension=".js" #>

define(['services/logger', 
        'services/jsonDataService', 
        'services/config', 
        'services/cachingService'],
        function (logger, jsonDataService, config, cache) {
            var dataService = { };
            return dataService;
        });

<#
    var controllers = ControllersInfo.GetControllers();
    foreach(var controller in controllers) {
        Dictionary<string, ParameterInfo[]> actions = ControllersInfo.GetAllCustomActionsInController(controller, new HttpGetAttribute());
    }
#>

There is also an external class that gets the controllers and actions but I do not think it's necesary for the current problem.

What am I doing wrong?

like image 395
aleczandru Avatar asked Jun 06 '13 06:06

aleczandru


People also ask

What does error CS0116 mean?

Error CS0116: A namespace cannot directly contain members such as fields or methods - Unity Forum.

What is a namespace in programming?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

What is a namespace in C#?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.


1 Answers

You probably have this figured out by now but:

<#@ import namespace="System.Web.Http;"#>

Note the ';'

Instead write:

<#@ import namespace="System.Web.Http"#>
like image 91
Just another metaprogrammer Avatar answered Oct 04 '22 00:10

Just another metaprogrammer