Archives page

Posts Tagged ‘c#’

Running PowerShell to change MSBuild scripts with NuGet

One of the things I’ve missed from creating a number of NuGet packages, is not being able to add msbuild tasks to the .csproj file.  From running web.config transforms, versioning assemblies, to running unit tests, code analysis, or deployments – msbuild can add quite a lot of cherries to your builds.   »

C# Joins with Linq and Lambdas

I’m always forgetting the syntax for lambda joins in C#, because I never use them enough and get bored looking for reminders enough that I just revert back my old ways and use the query expression instead. So rather than find a good tutorial and bookmark it, I’ll post it here instead. By the time it falls off the front page, I’ll just about have remembered how to do it without needing this anyway 🙂

Query Syntax

Code block    
var products = from audio in DbContext.DataContext.ProductAudios
join product in DbContext.DataContext.ProductAudios on audio.ProductId equals product.ProductId
select new { Product = product, Audio = audio };

Lambda Syntax

Code block    
var products = DbContext.DataContext.ProductAudios.Join(
                DbContext.DataContext.Products,
                audio => audio.ProductId,
                product => product.ProductId,
                (audio, product) => new { Product = product, Audio = audio });

It might look like more code because of my formatting, but I find the lambda syntax much convenient when chaining queries together with other where’s and groupby’s, especially when that might be split across different methods. It also isolates your join nicely, whereas I find the query syntax will start to get particularly unreadable with more complex queries.

Last but not least, another piece of linq-join-related syntax I’m finding myself always having to look up a lot is for left outer joins. Fortunately I always end up at MSDN for that one, so I’ll just link to it here:
How to: Perform Left Outer Joins

Running Ruby methods within C# / .NET

The last example might have been a little too trivial, even by my standards. Even I struggled to imagine a scenario where I might ever need to use it. So hopefully this one will be a little bit more interesting and demonstrate something more useful.

Useful, but still just as simple as the previous examples, that is. Again – you’ll need your references from the downloaded IronRuby bin/ folder. And as you’ve come to expect, a very simple ruby script defining a lambda function.

Code block    
$m = lambda {
            a = Array.new
            a.push(2, 3)
            (4..50).each do
               |i|
               (2..(Math.sqrt(i).ceil)).each do
                  |thing|
                  if (i.divmod(thing)[1] == 0)
                     a.push i
                     break
                  end
               end
            end
            return a
         }

From this, we’ll get an array of the prime numbers. The function can then be executed rather nicely from within your .NET code like this:

Code block    
var ruby = Ruby.GetEngine(Ruby.CreateRuntime());
ruby.Execute(@"
    $m = lambda {
//.. snip..
            return a
         }
");
var rubyContext = Ruby.GetExecutionContext(ruby);
var m = (Proc)rubyContext.GetGlobalVariable("m");
var rubyArray = (RubyArray) m.Call();
 
foreach (var o in rubyArray)
{
    Console.Write(string.Format("{0},", o));
}

Now we’re really starting to leverage that syntactical beauty of ruby within .NET and jumping (almost) seamlessly between the two. Now, I really should do some demos on something more useful than prime numbers, and perhaps get into one of the big areas of interest of Ruby – testing frameworks. Not tonight though 🙂

Demo project available as usual:

Executing complete (Iron)Ruby scripts from within native C# / .NET

Running Ruby code as-is within .NET is almost too simple to even write home about. In fact I almost didn’t, but after I wrote it the solution stared up at me with its big brown eyes, and I couldn’t resist. I’m not totally sure when I’d ever need to use this, but perhaps taking some legacy Ruby scripts where I don’t really care about the results, and still want to run them amongst some other .NET tasks. It might come up in some obscure unit testing one day. You never know.

So, this still serves as an example of how simple it can be to run ruby scripts within .NET. We’ll get to making use of some return values, and running individual methods in the next post.

Following on from the last example, the following ruby script prints all the prime numbers between 1 and 50.

Code block    
state = Numeric.new
print "2,3,"
(4..50).each do
   |i|
   (2..(Math.sqrt(i).ceil)).each do
      |thing|
      state = 1
      if (i.divmod(thing)[1] == 0)
         state = 0
         break
      end
   end
   print "#{i}\," unless (state == 0)
end

With the ruby script complete (you can also run this with the ir.exe that ships with IronRuby if you like)… Next start up a new project within Visual Studio, and add some references from your downloaded IronRuby bin folder (IronRuby.dll, IronRuby.Libraries.dll etc…).

You only need a few lines of code to execute your ruby script.

Code block    
using IronRuby;
// ..
var runtime = Ruby.CreateRuntime();
runtime.ExecuteFile(@"ruby/run.rb");

As before, you can download the full example to check it and run for yourself. And also as before, you will also need IronRuby.

Using C# / .NET libraries within IronRuby

I attended my first VistaSquad meeting on Wednesday. Part of the evening was a very interesting talk from @ben_hall on IronRuby, which among many other things included how to use any .NET CLR libraries direct from your IronRuby script (running via the .NET DLR).

Whilst my example below is extremely trivial, it shows how you might make use of any existing libraries within your Ruby scripts. This same technique applies to any .NET libraries, whether they’re custom, part of the framework, or created by your gran. I don’t think I really need to sell it in – but I love the flexibility that this provides.

So to get to the example, this simple piece of C# displays all the prime numbers between 0 and maxNumber:

Code block    
public int[] DisplayPrimeNumbers(int maxNumber)
{
    int max = maxNumber;
    List previousPrimes = new List();
    previousPrimes.Add(2);
    if (max < 2) return null; // none
    for (int i = 3; i  maxDivisor) break;
            if ((i % previousPrimes[j]) == 0)
            {
                foundDivisor = true;
            }
        }
        if (!foundDivisor)
        {
            previousPrimes.Add(i);
        }
    }
    return previousPrimes.ToArray();
}

We can build that up into a class library and using IronRuby, manipulate the return of the method the same as though we had been running native ruby.

Code block    
require 'mscorlib'
require 'CSharpLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null';
 
prime_numbers = CSharpLib::PrimeNumbers.new
 
(prime_numbers.DisplayPrimeNumbers 20).each do |num|
	puts num
end

You can download the full sample below, a C# console app is also included for completeness (although isn’t a part of the IronRuby process). You will of course, need to download IronRuby first, and add the installed bin/ folder to your path. Then just change to the <sample>/ruby/ directory, and run it with:

ir run.rb

It’s probably worth noting that IronRuby is still a way off from a 1.0 release, but it’s already very usable and looking rather cool. Since it’s on my recent //TODO list, I’ll be doing a few more examples here – next time turning this one its head and executing your ruby scripts from within C#. In the meantime, you can check out Ben’s set of slides from Wednesday on Slideshare.