Posts

Showing posts from January, 2012

C# Runtime Error: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

Add <startup> tag to the bottom of app.config file<?xml version="1.0"?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>

C# Get server IP address

publicstaticstring GetIPAddress(string server) { var host = Dns.GetHostEntry(server); if(host.AddressList.Length>0) { return host.AddressList[0].ToString(); } returnnull; }

C# T4 Templating Engine

VS2010 build in templating engine.Add new ‘Preprocessed Text Template’ called T4Template.tt to your project that looks like that<#@ template debug="true" language="C#"#> <#@ parameter name="FirstName" type="System.String" #> Now: <#= DateTime.Now.ToString() #> FirstName: <#= FirstName #>This is a sample program to run the templateclass Program { privatestaticvoid Main(string[] args) { var template = new T4Template(); template.Session = new Dictionary<string, object>(); template.Session.Add("FirstName", "John"); template.Initialize(); Console.WriteLine(template.TransformText()); } }and outputNow: 12/01/2012 10:42:07 FirstName: John

C# Razor Templating Engine

Templating engine built upon Microsoft's Razor parsing technology
http://razorengine.codeplex.com/
Add new text file to your project called ‘RazorTemplate.txt’ that looks like thatHello @Model.Name! Welcome to Razor!This is a sample program to run the templateclass Program { privatestaticvoid Main() { var template = File.ReadAllText("RazorTemplate.txt"); var result = Razor.Parse(template, new {Name = "John"}); Console.WriteLine(result); } }and outputHello John! Welcome to Razor!another example with custom model//template@inherits RazorEngine.Templating.TemplateBase<RazorSample.RazorModel> Hello @Model.Name! Welcome to Razor! //Testnamespace RazorSample { [TestFixture] class Tests { [Test] publicvoid Test() { var template = File.ReadAllText("RazorTemplate.txt"); var result = Razor.Parse(template, new RazorModel(){Name="John"}); Console.WriteLi…