Archive for the ‘tip’ Category

Exception: could not load file or assembly system.data.datasetextensions version 3.5.0.0

August 18th, 2010

When copying an application from a Wndows 7 developer machine to a XP test server i stumbled upon an error message similar to:

could not load file or assembly system.data.datasetextensions version 3.5.0.0

To make a long story short it turned out that the System.Data.DataSetExtenstions.dll was missing.

Dotnet is found in different places in different OSes so instead of searching I opened the project in Visual studio, found the reference to the file and looked at the reference’s properties.  There was the exact path.  Copied the file to the target machine’s application’s folder and it worked.
This solution, copying the DLL to the application’s folder, is not really recommended since the update system of Dotnet does not work any more.  It is better to find the correct place to put it.

I don’t know why this very DLL was missing from the target machine.

categories: tip | no comments »

The type or namespace name ‘log4net’ could not be found (are you missing a using directive or an assembly reference?)

August 17th, 2010

When creating an application with dotnet 4 (dotnet4) there is a caveat regarding log4net.  One has to change the Target Framework from the default .Net Framework 4 Client Profile to the full .Net Framework 4.

This is done through the project properties.

Honour those who should.

A slightly strange side note is that in my VS2010 log4net showed up in the intellisense to start with but disappeared later; until I did the Target-Framework patch.

The tutorial/quickstart I used is here.

categories: tip | 30 comments »

T4 – <#@ Assembly name="$(SolutionDir)\T4Helper\bin\debug\T4Helper.dll" #>

July 16th, 2010

As mentioned here and here the assembly referencing has changed from VS2008 to VS2010.

Say you have a T4 helper assembly aptly named T4Helper and you have it in a project called… T4Helper.

MySolution
....MyProject
........MyT4Files
........MyOtherFiles
....T4Helper
........T4Source

The VS-macro-way to reference it from  MyProject is

<#@ Assembly name="$(SolutionDir)\T4Helper\bin\debug\T4Helper.dll" #>

It obviously has problems when switching away from debug compile.  But the line above will help you get the backslashes right at least.

Update:
Something with

$(ProjectDir)$(OutDir)

might do the trick.

categories: T4, tip | no comments »

Multi line strings in C#

July 15th, 2010

Instead of writing

string query =
"select * " +
"from Customer " +
"where CustomerID = @customerID";

or

string xml =
"<Customers> " + Environment.NewLine +
"    <Customer> " + Environment.NewLine +
"        <ID value=\"12\"/> " + Environment.NewLine +
"    </Customer> " + Environment.NewLine +
"</Customers>";

one can write

string query =
"select *
from Customer
where CustomerID = @customerID";

or

string xml =
@"
<Customers>
    <Customer>
        <ID value='12'/>
    </Customer>
</Customers>
";

with just quotation marks in the beginning and end – not at every row.

Note how one can write apostrophes ( ‘ ) instead of quotation mark ( ” ) for the xml attributes.

The at sign ( @ ) is there to tell the parser that any back slashes ( \ ) should be treated like regular characters and not escape characters.  Very useful when handling files and folders on the Windows platform.  Not very useful in my example.

Also note that if you want an xml declaration <?xml version=’1.0′ encoding=’utf-8’ ?> it must be before any whitespace

string xml =
@"<?xml version='1.0' encoding='utf-8' ?>
<Customers>
    ...

In Vbnet one can write XML inline – like if it is a part of the language.

categories: tip | 3 comments »

T4 – how to read output easy

July 15th, 2010

When working with Microsoft’s Text Template Transformation Toolkit there is a lot of ctrl-tabbing to and fro to validate the result.

Instead split the editor  (I am talking Visual studio here) vertically and put the .tt file in one and the output (.cs) file in the other.  Whenever you save the .tt file the output file is updated.  It even remembers the scrolling position.

categories: T4, tip | no comments »

T4 – Template and TemplateTemplate

July 15th, 2010

Update: use this tip instead.

One can call a template from another through <#@ include file=”MyTemplateTemplate.tt” #> Variables defined in MyTemplate.tt are then transfered to MyTemplateTemplate.tt. The trick is to set them in the right order.

Below is also shown a way to avoid unnecessary output. Here is [another].

MyTemplate.tt:

<#
string Greeting = "HelloWorld";
bool createOutput = true;
#>
<#@ include file="MyTemplateTemplate.tt" #>

MyTemplateTemplate.tt:

<#
if( !createOutput )
{
return "//	This file is not in use.";
}
#>

…the template code

<#+
bool createOutput = false;
string Greeting = string.Empty;
#>

As you can see the TemplateTemplate has the variable assignment as a statement “<#+” below/after the code.

Then there is the createOutput flag as a trick to avoid output. Return blank if you want an empty file. Another way to avoid compilable code that won’t compile is here.

categories: T4, tip | no comments »

T4 – Avoid getting compilable output

July 15th, 2010

Say you have a handy parameter driven T4 template to create a business object BusinessObject.tt. You then have one T4-file per business object Customer.tt, User.tt, Role.tt. Now when editing BusinessObject.tt it outputs a .cs file with a class name like MyClass. This output file will naturally not compile since MyClass isn’t part of your domain.

A simple solution is to set <#@ output extension=”.txt” #> in BusinessObject.tt but keep <#@ output extension=”.cs” #> in Customer.tt, User.tt etc.
A drawback is that the output from BusinessObject.tt won’t compile so you won’t get feedback from the compiler. My workaround for this is to save Customer.tt once in a while to check for compilation errors.

Here is another way.

categories: T4, tip | no comments »

T4 – Getting started

July 15th, 2010

Download the templates from Tangible, there is a gratis version.
Use the Simple and the Advanced templates from Tangible.
Read Oleg’s article.

You might notice that T4 once in a while complains about a right parenthesis. This is often a false error. In some cases you get more errors so just skip to the right-parenthesis-complaint and they will vanish. Other cases this is the only error. Just type a character and delete it and save again. It can even be enough to just step away from the edited row.
The error is always on the same row as the cursor is positioned.

If you know that the output should compile but the compiler complains every time you save the .tt file it is working as it should. Saving the .tt file runs the template transformation but doesn’t start any compilation. So just compile after saving the .tt file and all should be well.

categories: T4, tip | no comments »

T4 – A namespace cannot directly contain members such as fields or methods

July 15th, 2010

If you are using T4 and get this error you are probably having an error in the template which makes it fail transforming.  Check here for a tidbit more information.

An error example:
Error 11 A namespace cannot directly contain members such as fields or methods C:\MyProject\MyOutputFile.cs 1 1 MyProjectName

categories: T4, tip | no comments »

Parallel foreach with linq in dotnet3.5

June 29th, 2010

If you want to do threading the nice dotnet4 way with myList.AsParallel().ForEach but are stuck in a 3.5 environment look here.

categories: tip | no comments »