March 2008 Entries
I've been playing with Boo recently and I find it to be very powerful language. One little thing I like particularly is, that I can do this: name = "Krzysztof"print "Hello, ${name}, on the beautiful day of ${date.Today}"
And get this:
Can I please have this in C# 4.0?
Technorati Tags: Boo, C# 4.0, String Interpolation
C# doesn't stop to surprise me. While looking at Moq source code I noticed something strange: Generic constraint says class but yet somehow interfaces account for class? Technorati Tags: Moq, Generics
Last week I've read quite a few new blogposts about Moq mocking framework. I had looked at it once, when it was first released, but I didn't find it interesting back then. Now that version 2.0 was released I decided to give it a go once again. Here's very simple model I created: It's a class with one method that uses helper object to obtain a value and possibly raise an event. Here's the whole code: public interface IHelper { int Param { set; } ...
Here's the problem: Having some ID, get a new instance of some class corresponding to this ID. All classes implement the same common interface (by which we will use them). So we need to be able to write something like: public IMessage DoSomethingWithMessage(Id id){ IMessage message = MessageFactory.CreateMessage(id); message.DoSomething();}
Now, how to actually implement MessageFactory, knowing that it needs to be really fast, and without having to explicitly change its implementation when new messages are added, Ids change and so on? And how to bind IDs to Messages?
Here's my idea for the solution, and...
Can anyone tell me what am I missing here? Here's a simple class public class Mock<TType>{ private readonly TType _actual; public Mock(TType actual) { _actual = actual; } public TType Actual { get { return _actual; } } public static implicit operator TType(Mock<TType> item) { return item.Actual; }}
That's a...
I just spent good few hours trying to fight awkward limitations of .NET framework in regard to generics, events, delegates and expression trees. And I'm actually not much further than when I started. It bothers me: why put artificial limitations in regard to delegates as generic constraints? I wish I could do: public IEventManager<T1,T2> Metod<T1,T2,TEvent>(TEvent @event) where TEvent: Func<T1,T2>
(this will not compile, can not use delegates as constraints...) or even better:
public IEventManager<T1,T2> Metod<T1,T2,TEvent>(TEvent @event) where TEvent: void delegate(T1,T2)
This seems to ruin my every idea. Even worse, when I looked for help at Expression Trees, I learned that you can't have...
I've been playing in my limited spare time with Rhino.Mocks, trying to come up with a way to simplify interactions with events, and creating strongly typed IEventRaiser (or something similar). Here's what I've come up with so far: It's not an easy task, but I have few ideas. If you have any ideas or suggestions post them in the comments. I'll submit it to Ayende, so if he likes it you may actually see this stuff in some future version of Rhino.Mocks. Technorati Tags: Rhino.Mocks
Well, due to overwhelming feedback for the previous post, here's the answer: 0. Now the most interesting part: why? When you use inline initialization, compiler generates a static constructor for you, and does all the initialization there. It looks like, it's not smart enough to pick this kind of dependencies, and simply initializes fields in the order they've been declared in the class' body. That's why, by the time Field is initialized, _height has value 3, but _width is still 0. If you wanted to overcome this, you'd have to use const instead of readonly (that's an option only for...
Let's play. What will this program output to the console: namespace Test{ public class Program { private static void Main() { System.Console.WriteLine(MyClass.Field); } } public class MyClass { private static readonly int _height = 3; private static readonly int...
In .NET < 3.5 the only collections you could initialize inline were arrays. So this was legal: public class CollectionTest{ public static readonly ICollection<string> _list = new string[]{"one","two","three"};}
However if you wanted to have List instead of array, you had to use a trick and pass array as constructor parameter:
public static readonly ICollection<string> _list = new List<string>(new string[]{"one","two","three"});
Not the most elegant piece of code, but at least it works. So far so good. What if you wanted to...