{Dev Tricks} » C# http://dev-tricks.net Blogging developper tips and tricks Sun, 30 Aug 2009 00:14:47 +0000 http://wordpress.org/?v=2.9.2 en hourly 1 Covariance and Contravariance in C#3 http://dev-tricks.net/covariance-and-contravariance-in-c-sharp-3 http://dev-tricks.net/covariance-and-contravariance-in-c-sharp-3#comments Mon, 09 Mar 2009 19:19:51 +0000 Julien Palard http://dev-tricks.net/?p=203 A short introduction to Covariance and Contravariance in C# 3 preparing you to an article about that in C# 4.
So what is covariance ?
Covariance is basically using a method which returns something derived from the expected type.

An exemple ? It’s safe to have a method returning a cat when you expect it to return an animal.

In C sharp it’s

Unable to get source code, check your URL given in [viewcode].

Download this code: covariance.cs

So Funcs a, b, and c are returning animals which in fact are cats, which is true.

And, what is contravariance ?
Contravariance is basically using a method which takes something which is a parent of the expected type.
An exemple ? It’s safe to have a method taking an animal when you expect it to take a cat

Unable to get source code, check your URL given in [viewcode].

Download this code: contravariance.cs

So Action a take Cats, but in fact can take any Animals, so it’s safe.

]]>
http://dev-tricks.net/covariance-and-contravariance-in-c-sharp-3/feed 1
Combinatory logic from scratch http://dev-tricks.net/combinatory-logic-from-scratch http://dev-tricks.net/combinatory-logic-from-scratch#comments Thu, 27 Nov 2008 23:56:32 +0000 Julien Palard http://dev-tricks.net/?p=64 Cause it’s sooooo sexy, let’s speak about Combinatory Logic !
Rule 1 : You don’t talk about Combinatory Logic
Rule 2 : You don’t talk about Combinatory Logic
Rule 3 : Combinatory Logic is based on Lambda Calculus (see Wikipedia for both)
Rule 4 : A combinator is a Lambda expression taking One and only One combinator as parameter, and returning a Combinator.


As i’m speaking to developers, i’ll use the C# Lambda syntax which is :
(parameter) => statement

Let’s now try our first Combinator, named the Identity Combinator
I = (a) => a;
I named it I, it takes one parameter, localy named ‘a’ and return the parameter as is.

Important Point : How to build combinator taking more than one parameter ?
In C# you should use (a, b, c) => blah blah… but the Rule 4 forbid us to give more than one paraneter, so let’s cheat, imagine :
K = (x) => (y) => x;
K is a Combinator taking x, returning a Combinator taking y and returning x.
so we have K(x) = (y) => x and K(x)(y) = x !
So K take two arguments, x and y, and returns x, but ! K can take only one argument, look at the “K(x) = (y) => x” …

Let’s try with three arguments :

S = (x) => (y) => (z) => x(z)(y(z))
can be called with one, two, or three arguments :
S(x) returns (y) => (z) => x(z)(y(z))
S(x)(y) returns (z) => x(z)(y(z))
s(x)(y)(z) returns x(z)(y(z))

In combinatory logic, they wrote :
I a = a
K x y = x
S x y z = x(z)(y(z))

then they say that in fact, I can be build from S and K :
I = SKK
Ok but what does it means ? where are arguments ? it’s easy :

I = S(K)(K);
S can take 2 parameters “S(x)(y) returns (z) => x(z)(y(z))” ok ? so :
I = (z) => K(z)(K(z))
We have to execute it from left to right, remember, K(a)(b) returns a, so (with a == z and b == K(z)) :
I = (z) => z;

Do you want more ?

Let’s try to understand
B = S (K S) K x y z
B stands for Barbara, from “Syllogism Barbara” (wikipedia says :)
Barbara :
All men are animals.
All animals are mortal.
All men are mortal.

So before all, write B as we understand it, and for readability reasons, i’ll underline parameters for the bolded combinator :
B = S (K(S)) K (x) (y) (z)
We have to execute it from left to right, and we have a S with three parameters :
S(a)(b)(c) returns a(c)(b(c)) :
B = K (S) (x) (K(x)) (y) (z)
From left to right we have a K with two parameters, S and y, it will return S :
B = S (K(x)) (y) (z)
Calling S with three parameters (K(x)), (y), (z) returns (K(x))(z)((y)(z)) :
B = K (x) (z) ((y)(z))
Calling K with two parameters (x), (z), it returns x :
B = x((y)(z))
Which can be simplified to :
B = x(y(z))

It’s time to try it !

Unable to get source code, check your URL given in [viewcode].

Download this code: test.cs

It works ! Enjoy ! ! Next time, we will try a Swap combinator, a Combinator reducing to himself and progressing step to the Y Combinator !
[dramatic chord]

]]>
http://dev-tricks.net/combinatory-logic-from-scratch/feed 0
The art of Events http://dev-tricks.net/the-art-of-events http://dev-tricks.net/the-art-of-events#comments Fri, 29 Aug 2008 21:32:55 +0000 Julien Palard http://dev-tricks.net/?p=28 > me))) will pass a reference of A to B “A(){B my_B [...]]]> The art of using events to build more independent classes.
/* Found a better example */
Imagine you have a class A and a class B.
A builds B, and B have to communicate with A (call methods … ?)
Some developers (Boooo (I’ve done it … (Booo >> me))) will pass a reference of A to B
“A(){B my_B = new B(this);}” /* Please note the newB private joke */
But what happens when you want an other class, C, who don’t know A to build B ?

So we have to remove the A’s reference in the B ctor.
How B should communicate now ?
– Using Events !
B exposes an event, the builder of B can register on it.
– B can speak to A throwing this event.
– Every class can build a B, and can, if it needs, register on its events, doing whatever he wants when the event is thrown.
B is now fully reusable !

]]>
http://dev-tricks.net/the-art-of-events/feed 0
Howto invoke an event via reflection http://dev-tricks.net/howto-invoke-an-event-via-reflexion http://dev-tricks.net/howto-invoke-an-event-via-reflexion#comments Fri, 29 Aug 2008 21:14:53 +0000 Julien Palard http://dev-tricks.net/?p=24 Why this article ? because of this note found on the msdn’s EventInfo page:
“EventInfo is not intended to be used to raise events. An object raises events as dictated by its internal state. ”

Let’s try to raise an event with reflection …


Firstly, let’s search, but not in GetEvent, in GetField :
Type.GetField(String, BindingFlags)
String is the name of the field to get and BindingFlags a bitmask of serarching flags.

Note : To use reflexion, use “System.Reflection”

Let’s try a GetField :

Unable to get source code, check your URL given in [viewcode].

Download this code: 1.cs

Yeah ! it’s not null ! we have now the FieldInfo of our event.
What to do now ? in a FieldInfo, we do not have something like “Fire the event”, huh
But what about “GetValue” ? The “Value” of a “FieldInfo” is the field, so the Value of an event’s FieldInfo isn’t the Event ?
FieldInfo.GetValue(object)
let’s try :

Unable to get source code, check your URL given in [viewcode].

Download this code: 2.cs

Is null … ):
Ohhhh but, while not registered, an event is null … so, let’s try adding a handler…
Is not null ! ! Yeah, so we have our event by reflection now.
————————————————————
Note for those who just said “Why giving an object to GetValue ? And why giving ‘this’” :
1) Take a look at the first “this.getType()”
2) Deduce that it ask the Object to give you its Type, an Object knows its Type.
3) Note that a Type can’t know its Objects …
4) Finally to get a Field of your Object, you ask the Type to give it or your Object, giving a reference to your Object, here : ‘this’;
————————————————————
Now, we have an object, which is the event to call, but, how to call it ?
By reflection ? but … where to search ?
TRICK : Put a breakpoint just after the GetValue, when breaked, add a watch on “my_event_by_reflection.GetType()” and browse it… try my_event_by_reflection.GetType().GetFields() … nothing … try my_event_by_reflection.GetType().GetMethods() … browse it … OH ? What is “Invoke” ?

Unable to get source code, check your URL given in [viewcode].

Download this code: 3.cs

Searching how to call the Invoke … by reflection ? Invoking invoke ? Let’s try :

Unable to get source code, check your URL given in [viewcode].

Download this code: 4.cs

Invoke take the instance of this type, so the object where GetType where called, and in second parameter an array of object to be given as parameter to the method called, we have to give an object and an eventArgs.
It Works ! here the full working source code :

Unable to get source code, check your URL given in [viewcode].

Download this code: full.cs

]]>
http://dev-tricks.net/howto-invoke-an-event-via-reflexion/feed 0