<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>{Dev Tricks} &#187; C#</title>
	<atom:link href="http://dev-tricks.net/category/code/c/feed" rel="self" type="application/rss+xml" />
	<link>http://dev-tricks.net</link>
	<description>Blogging developper tips and tricks</description>
	<lastBuildDate>Sun, 11 Dec 2011 11:54:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Covariance and Contravariance in C#3</title>
		<link>http://dev-tricks.net/covariance-and-contravariance-in-c-sharp-3</link>
		<comments>http://dev-tricks.net/covariance-and-contravariance-in-c-sharp-3#comments</comments>
		<pubDate>Mon, 09 Mar 2009 19:19:51 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=203</guid>
		<description><![CDATA[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 &#8230; <a href="http://dev-tricks.net/covariance-and-contravariance-in-c-sharp-3">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A short introduction to Covariance and Contravariance in C# 3 preparing you to an article about that in C# 4.<br />
So what is covariance ?<br />
Covariance is basically using a method which returns something derived from the expected type.</p>
<p>An exemple ? It&#8217;s safe to have a method returning a cat when you expect it to return an animal.<br />
<span id="more-203"></span><br />
In C sharp it&#8217;s</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Animal
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Cat <span style="color: #008000;">:</span> Animal
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Dog <span style="color: #008000;">:</span> Animal
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// It's safe to say that something returns a Animal when in fact this thing returns a Cat</span>
<span style="color: #6666cc; font-weight: bold;">class</span> Covariance
<span style="color: #008000;">&#123;</span>
	<span style="color: #6666cc; font-weight: bold;">void</span> test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		Func<span style="color: #008000;">&lt;</span>Animal<span style="color: #008000;">&gt;</span> a <span style="color: #008000;">=</span> Method<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// OK</span>
		Func<span style="color: #008000;">&lt;</span>Animal<span style="color: #008000;">&gt;</span> b <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Cat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// OK</span>
		Func<span style="color: #008000;">&lt;</span>Animal<span style="color: #008000;">&gt;</span> c <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> Cat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// OK</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	Cat Method<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Cat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>So Funcs a, b, and c are returning animals which in fact are cats, which is true.</p>
<p>And, what is contravariance ?<br />
Contravariance is basically using a method which takes something which is a parent of the expected type.<br />
An exemple ? It&#8217;s safe to have a method taking an animal when you expect it to take a cat</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// It's safe to say that something can take a Cat if in fact this thing can take any Animal</span>
<span style="color: #6666cc; font-weight: bold;">class</span> Contravariance
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		Action<span style="color: #008000;">&lt;</span>Cat<span style="color: #008000;">&gt;</span> a <span style="color: #008000;">=</span> Method<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// OK</span>
		Action<span style="color: #008000;">&lt;</span>Cat<span style="color: #008000;">&gt;</span> b <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span>Animal value<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// ERROR</span>
		<span style="color: #008080; font-style: italic;">// From C#3 Specification :</span>
		<span style="color: #008080; font-style: italic;">// $7.14.1 Anonymous function signatures :</span>
		<span style="color: #008080; font-style: italic;">// [...] contra-variance of anonymous function parameter types is not supported.</span>
		Action<span style="color: #008000;">&lt;</span>Cat<span style="color: #008000;">&gt;</span> d <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Animal value<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// idem... anonymous... not supported.</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Method<span style="color: #008000;">&#40;</span>Animal value<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>So Action a take Cats, but in fact can take any Animals, so it&#8217;s safe.  </p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/covariance-and-contravariance-in-c-sharp-3/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Combinatory logic from scratch</title>
		<link>http://dev-tricks.net/combinatory-logic-from-scratch</link>
		<comments>http://dev-tricks.net/combinatory-logic-from-scratch#comments</comments>
		<pubDate>Thu, 27 Nov 2008 23:56:32 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Combinatory Logic]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=64</guid>
		<description><![CDATA[Cause it&#8217;s sooooo sexy, let&#8217;s speak about Combinatory Logic ! Rule 1 : You don&#8217;t talk about Combinatory Logic Rule 2 : You don&#8217;t talk about Combinatory Logic Rule 3 : Combinatory Logic is based on Lambda Calculus (see Wikipedia &#8230; <a href="http://dev-tricks.net/combinatory-logic-from-scratch">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Cause it&#8217;s sooooo sexy, let&#8217;s speak about Combinatory Logic !<br />
Rule 1 : You don&#8217;t talk about Combinatory Logic<br />
Rule 2 : You don&#8217;t talk about Combinatory Logic<br />
Rule 3 : Combinatory Logic is based on Lambda Calculus (see Wikipedia for both)<br />
Rule 4 : A combinator is a Lambda expression taking One and only One combinator as parameter, and returning a Combinator.</p>
<p><span id="more-64"></span><br />
As i&#8217;m speaking to developers, i&#8217;ll use the C# Lambda syntax which is :<br />
(parameter) => statement</p>
<p>Let&#8217;s now try our first Combinator, named the Identity Combinator<br />
I = (a) => a;<br />
I named it I, it takes one parameter, localy named &#8216;a&#8217; and return the parameter as is.</p>
<p>Important Point : How to build combinator taking more than one parameter ?<br />
In C# you should use (a, b, c) => blah blah&#8230; but the Rule 4 forbid us to give more than one paraneter, so let&#8217;s cheat, imagine :<br />
K = (x) => (y) => x;<br />
K is a Combinator taking x, returning a Combinator taking y and returning x.<br />
so we have K(x) = (y) => x and K(x)(y) = x !<br />
So K take two arguments, x and y, and returns x, but ! K can take only one argument, look at the &#8220;K(x) = (y) => x&#8221; &#8230;</p>
<p>Let&#8217;s try with three arguments :</p>
<p>S = (x) => (y) => (z) => x(z)(y(z))<br />
can be called with one, two, or three arguments :<br />
S(x) returns (y) => (z) => x(z)(y(z))<br />
S(x)(y) returns (z) => x(z)(y(z))<br />
s(x)(y)(z) returns x(z)(y(z))</p>
<p>In combinatory logic, they wrote :<br />
I a = a<br />
K x y = x<br />
S x y z = x(z)(y(z))</p>
<p>then they say that in fact, I can be build from S and K :<br />
I = SKK<br />
Ok but what does it means ? where are arguments ? it&#8217;s easy :</p>
<p>I = S(K)(K);<br />
S can take 2 parameters &#8220;S(x)(y) returns (z) => x(z)(y(z))&#8221; ok ? so :<br />
I = (z) => K(z)(K(z))<br />
We have to execute it from left to right, remember, K(a)(b) returns a, so (with a == z and b == K(z)) :<br />
I = (z) => z;</p>
<p>Do you want more ?</p>
<p>Let&#8217;s try to understand<br />
B = S (K S) K x y z<br />
B stands for Barbara, from &#8220;Syllogism Barbara&#8221; (wikipedia says :)<br />
Barbara :<br />
    All men are animals.<br />
    All animals are mortal.<br />
    All men are mortal. </p>
<p>So before all, write B as we understand it, and for readability reasons, i&#8217;ll underline parameters for the bolded combinator :<br />
B = <strong>S</strong> <u>(K(S))</u>  <u>K</u>  <u>(x)</u> (y) (z)<br />
We have to execute it from left to right, and we have a S with three parameters :<br />
<strong>S</strong>(<u>a</u>)(<u>b</u>)(<u>c</u>) returns a(c)(b(c)) :<br />
B = <strong>K</strong>  <u>(S)</u>  <u>(x)</u>  (K(x)) (y) (z)<br />
From left to right we have a K with two parameters, S and y, it will return S :<br />
B = <strong>S</strong> <u>(K(x))</u>  <u>(y)</u> <u>(z)</u><br />
Calling S with three parameters (K(x)), (y), (z) returns (K(x))(z)((y)(z)) :<br />
B = <strong>K</strong>  <u>(x)</u>  <u>(z)</u>  ((y)(z))<br />
Calling K with two parameters (x), (z), it returns x :<br />
B = x((y)(z))<br />
Which can be simplified to :<br />
B = x(y(z))</p>
<p>It&#8217;s time to try it !</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #6666cc; font-weight: bold;">delegate</span> C C<span style="color: #008000;">&#40;</span>C c<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	C K <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>a<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#40;</span>b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> a<span style="color: #008000;">;</span>
	C S <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>a<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#40;</span>b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> a<span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>b<span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	C I <span style="color: #008000;">=</span> S<span style="color: #008000;">&#40;</span>K<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>K<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	C B <span style="color: #008000;">=</span> S<span style="color: #008000;">&#40;</span>K<span style="color: #008000;">&#40;</span>S<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>K<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>It works ! Enjoy ! ! Next time, we will try a Swap combinator, a Combinator reducing to himself and progressing step to the Y Combinator !<br />
[dramatic chord]</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/combinatory-logic-from-scratch/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The art of Events</title>
		<link>http://dev-tricks.net/the-art-of-events</link>
		<comments>http://dev-tricks.net/the-art-of-events#comments</comments>
		<pubDate>Fri, 29 Aug 2008 21:32:55 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[event]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=28</guid>
		<description><![CDATA[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 &#8230; ?) &#8230; <a href="http://dev-tricks.net/the-art-of-events">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The art of using events to build more independent classes.<br />
/* Found a better example */<br />
Imagine you have a class A and a class B.<br />
A builds B, and B have to communicate with A (call methods &#8230; ?)<br />
Some developers (Boooo (I&#8217;ve done it &#8230; (Booo >> me))) will pass a reference of A to B<br />
&#8220;A(){B my_B = new B(this);}&#8221; /* Please note the newB private joke */<br />
But what happens when you want an other class, C, who don&#8217;t know A to build B ? </p>
<p>So we have to remove the A&#8217;s reference in the B ctor.<br />
How B should communicate now ?<br />
 &#8211; Using Events !<br />
B exposes an event, the builder of B can register on it.<br />
 &#8211; B can speak to A throwing this event.<br />
 &#8211; Every class can build a B, and can, if it needs, register on its events, doing whatever he wants when the event is thrown.<br />
B is now fully reusable !</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/the-art-of-events/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Howto invoke an event via reflection</title>
		<link>http://dev-tricks.net/howto-invoke-an-event-via-reflexion</link>
		<comments>http://dev-tricks.net/howto-invoke-an-event-via-reflexion#comments</comments>
		<pubDate>Fri, 29 Aug 2008 21:14:53 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=24</guid>
		<description><![CDATA[Why this article ? because of this note found on the msdn&#8217;s EventInfo page: &#8220;EventInfo is not intended to be used to raise events. An object raises events as dictated by its internal state. &#8221; Let&#8217;s try to raise an &#8230; <a href="http://dev-tricks.net/howto-invoke-an-event-via-reflexion">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Why this article ? because of this note found on the msdn&#8217;s EventInfo page:<br />
&#8220;EventInfo is not intended to be used to raise events. An object raises events as dictated by its internal state. &#8221;</p>
<p>Let&#8217;s try to raise an event with reflection &#8230;</p>
<p><span id="more-24"></span><br />
Firstly, let&#8217;s search, but not in GetEvent, in GetField :<br />
<a href="http://msdn.microsoft.com/en-us/library/4ek9c21e.aspx">Type.GetField(String, BindingFlags)</a><br />
String is the name of the field to get and <a href="http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx">BindingFlags</a> a bitmask of serarching flags.</p>
<p>Note : To use reflexion, use &#8220;System.Reflection&#8221;</p>
<p>Let&#8217;s try a GetField :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">FieldInfo my_event_FieldInfo <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetField</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;my_event&quot;</span>,
                                                       BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">NonPublic</span>
                                                       <span style="color: #008000;">|</span> BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Yeah ! it&#8217;s not null ! we have now the FieldInfo of our event.<br />
What to do now ? in a FieldInfo, we do not have something like &#8220;Fire the event&#8221;, huh<br />
But what about &#8220;GetValue&#8221; ? The &#8220;Value&#8221; of a &#8220;FieldInfo&#8221; is the field, so the Value of an event&#8217;s FieldInfo isn&#8217;t the Event ?<br />
<a href="http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.getvalue.aspx">FieldInfo.GetValue(object)</a><br />
let&#8217;s try :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">object</span> my_event_by_reflection <span style="color: #008000;">=</span> my_event_FieldInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Is null &#8230; ):<br />
Ohhhh but, while not registered, an event is null &#8230; so, let&#8217;s try adding a handler&#8230;<br />
Is not null ! ! Yeah, so we have our event by reflection now.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
Note for those who just said &#8220;Why giving an object to GetValue ? And why giving &#8216;this&#8217;&#8221; :<br />
1) Take a look at the first &#8220;this.getType()&#8221;<br />
2) Deduce that it ask the Object to give you its Type, an Object knows its Type.<br />
3) Note that a Type can&#8217;t know its Objects &#8230;<br />
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 : &#8216;this&#8217;;<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
Now, we have an object, which is the event to call, but, how to call it ?<br />
By reflection ? but &#8230; where to search ?<br />
TRICK : Put a breakpoint just after the GetValue, when breaked, add a watch on &#8220;my_event_by_reflection.GetType()&#8221; and browse it&#8230; try my_event_by_reflection.GetType().GetFields() &#8230; nothing &#8230; try my_event_by_reflection.GetType().GetMethods() &#8230; browse it &#8230; OH ? What is &#8220;Invoke&#8221; ?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var my_event_invoke <span style="color: #008000;">=</span> my_event_type<span style="color: #008000;">.</span><span style="color: #0000FF;">GetMethod</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Invoke&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Searching how to call the Invoke &#8230; by reflection ? Invoking invoke ? Let&#8217;s try :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">my_event_invoke<span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span>my_event_by_reflection, <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span><span style="color: #0600FF; font-weight: bold;">this</span>, <span style="color: #008000;">new</span> EventArgs<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>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.<br />
It Works ! here the full working source code :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> Cage_en_metal
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler ecraser<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Cage_en_metal<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        ecraser <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span>Libellule_ecraser<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        fireEvent<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ecraser&quot;</span>, <span style="color: #008000;">new</span> EventArgs<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">void</span> Libellule_ecraser<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Splatch !&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">void</span> fireEvent<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> handler, EventArgs eventArgs<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var eventInfo <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetField</span><span style="color: #008000;">&#40;</span>handler,
		                                BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span>
						<span style="color: #008000;">|</span> BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">NonPublic</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>eventInfo <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            var event_member <span style="color: #008000;">=</span> eventInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008080; font-style: italic;">// Note : If event_member is null, nobody registered to the event, you can't call it.</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>event_member <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                event_member<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetMethod</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Invoke&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span>event_member, <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">this</span>, eventArgs <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/howto-invoke-an-event-via-reflexion/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

