Nant Error: “The type or namespace does not exist”

I was trying to run a nant task on a new project this morning but received the following error:

error CS0234: The type or namespace name 'RegularExpressions' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)

The RegularExpressions namespace definitely exists as it has been around since .NET 1.1.

The code looked something like this:

<target name="DoReplace">
  <script language="C#" >
   <code>
     <![CDATA[
       public static void ScriptMain(Project project)
       {
         var rx = System.Text.RegularExpressions.Regex();
         // ...
       }
     ]]>
   </code>
  </script>
</target>

The solution was quite simple, add a reference block:

<target name="DoReplace">
  <script language="C#" >
    <references>
      <include name="System.dll" />
    </references>
   <code>
     // as before
   </code>
  </script>
</target>

NOTE: In earlier versions of this post I had <references><lib><include …/> as it appears from the references documentation that lib is required. However, it should NOT be present.

With recent versions of nant (at least 0.91 and 0.92, only a couple of basic assemblies are referenced by default. With earlier version of nant, like 0.85, this is unnecessary. The examples in the referenced documentation indicate such.

Tags: ,

2 Responses to “Nant Error: “The type or namespace does not exist””

  1. Kaleb Pederson January 5, 2013 at 5:19 pm #

    @FrankMao.com – yes, it indeed wouldn’t work as I typo’d the post, or mis-remembered, or something. If you remove the lib as referenced in my updated post it should work.

Trackbacks/Pingbacks

  1. Update version number in vbproj file in NAnt build « FrankMao.com - July 20, 2012

    […] system.dll reference in NAnt according to this post, no […]

Leave a Reply