<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Reflection on Bits and Pieces</title>
		<link>https://bits.strawjackal.org/tags/reflection/</link>
		<description>Recent content in Reflection on Bits and Pieces</description>
		<generator>Hugo</generator>
		<language>en-us</language>
		
		
		
		
			<lastBuildDate>Fri, 07 Oct 2011 00:16:03 +0200</lastBuildDate>
		
			<atom:link href="https://bits.strawjackal.org/tags/reflection/index.xml" rel="self" type="application/rss+xml" />
			<item>
				<title>Moq, Callbacks and Out parameters: a particularly tricky edge case</title>
				<link>https://bits.strawjackal.org/113/</link>
				<pubDate>Fri, 07 Oct 2011 00:16:03 +0200</pubDate>
				<guid>https://bits.strawjackal.org/113/</guid>
				<description>&lt;p&gt;In my current project we’re using &lt;a href=&#34;http://code.google.com/p/moq&#34;&gt;Moq&lt;/a&gt; as our mocking framework for unit testing. It’s a very nice package – very simple, very intuitive for someone who has his wrapped around basic mocking concepts, but tonight I ran into an annoying limitation that, while rare, impeded my tests considerably.&lt;/p&gt;  &lt;p&gt;Consider the following code:&lt;/p&gt;  &lt;pre style=&#34;border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 567px; padding-right: 5px; height: 390px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px&#34;&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  1: &lt;span style=&#34;color: #0000ff&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #0000ff&#34;&gt;interface&lt;/span&gt; IOut&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  2: {&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  3:    &lt;span style=&#34;color: #0000ff&#34;&gt;void&lt;/span&gt; DoOut (&lt;span style=&#34;color: #0000ff&#34;&gt;out&lt;/span&gt; &lt;span style=&#34;color: #0000ff&#34;&gt;string&lt;/span&gt; outval);&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  4: }&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  5: &#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  6: [Test]&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  7: &lt;span style=&#34;color: #0000ff&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #0000ff&#34;&gt;void&lt;/span&gt; TestMethod()&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  8: {&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  9:   var mock = &lt;span style=&#34;color: #0000ff&#34;&gt;new&lt;/span&gt; Mock&lt;IOut&gt;();&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt; 10:   &lt;span style=&#34;color: #0000ff&#34;&gt;string&lt;/span&gt; outVal = &#34;&lt;span style=&#34;color: #8b0000&#34;&gt;&lt;/span&gt;&#34;;&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt; 11:   mock.Setup(&lt;span style=&#34;color: #0000ff&#34;&gt;out&lt;/span&gt; =&gt; &lt;span style=&#34;color: #0000ff&#34;&gt;out&lt;/span&gt;.DoOut(&lt;span style=&#34;color: #0000ff&#34;&gt;out&lt;/span&gt; outVal))&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #ffff00; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt; 12:       .Callback&lt;&lt;span style=&#34;color: #0000ff&#34;&gt;string&lt;/span&gt;&gt;(theValue =&gt; DoSomethingWithTheValue(theValue));&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt; 13: }&lt;/pre&gt;&lt;/pre&gt;&#xD;&#xA;&lt;p&gt;What this code SHOULD do is create a mock object for the IOut interface that, when called with an out parameter, both assigns a value to it, and does something with the value in the Callback. &lt;/p&gt;</description>
			</item>
			<item>
				<title>Reflecting over an Event</title>
				<link>https://bits.strawjackal.org/148/</link>
				<pubDate>Thu, 29 Mar 2007 14:03:00 +0200</pubDate>
				<guid>https://bits.strawjackal.org/148/</guid>
				<description>&lt;p&gt;A coworker (Hey, Evgeny!) came to me with a puzzler today. He has an object that exposes an event, and he wants to know whether he&amp;#39;s already subscribed to that event. &lt;/p&gt;&lt;p&gt;Two things immediately came to mind - the first was that he really shouldn&amp;#39;t be doing that, and the second was to suggest that if the reason for this is to avoid multiple registrations, he can always call -= and then += afterwards, thus making sure he&amp;#39;s subscribed only once. Having established that, I got down to thinking about it.&lt;/p&gt;</description>
			</item>
			<item>
				<title>Crossing the line - Reflection and Reality</title>
				<link>https://bits.strawjackal.org/163/</link>
				<pubDate>Tue, 12 Dec 2006 18:42:00 +0200</pubDate>
				<guid>https://bits.strawjackal.org/163/</guid>
				<description>&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Trebuchet MS&#34;&gt;When we&#39;re working Reflection, we tend to think in different terms than we do when we&#39;re actually calling methods. There&#39;s an extra level of abstraction in everything we do. Translating between these levels of abstraction isn&#39;t always easy or intuitive, so I&#39;ll try to go over some common paths, using this following class definition for my examples: &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;COLOR: blue&#34;&gt;using &lt;/SPAN&gt;System.Reflection;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;COLOR: blue&#34;&gt;public&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: blue&#34;&gt;delegate&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: blue&#34;&gt;void&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyDelegate&lt;/SPAN&gt;(); &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;COLOR: blue&#34;&gt;public&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: blue&#34;&gt;class&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyClass &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;{ &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;    &lt;SPAN style=&#34;COLOR: blue&#34;&gt;public&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: blue&#34;&gt;void&lt;/SPAN&gt; MyMethod() { } &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;    &lt;SPAN style=&#34;COLOR: blue&#34;&gt;private&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: blue&#34;&gt;void&lt;/SPAN&gt; MyPrivateMethod() { } &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;    &lt;SPAN style=&#34;COLOR: blue&#34;&gt;public&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: blue&#34;&gt;static&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: blue&#34;&gt;void&lt;/SPAN&gt; MyStaticMethod() { } &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;    &lt;SPAN style=&#34;COLOR: blue&#34;&gt;public&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: blue&#34;&gt;int&lt;/SPAN&gt; MyField; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;    &lt;SPAN style=&#34;COLOR: blue&#34;&gt;public&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: blue&#34;&gt;int&lt;/SPAN&gt; MyProperty &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;    { &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;        &lt;SPAN style=&#34;COLOR: blue&#34;&gt;get&lt;/SPAN&gt; { &lt;SPAN style=&#34;COLOR: blue&#34;&gt;return&lt;/SPAN&gt; MyField; } &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;        &lt;SPAN style=&#34;COLOR: blue&#34;&gt;set&lt;/SPAN&gt; { MyField = &lt;SPAN style=&#34;COLOR: blue&#34;&gt;value&lt;/SPAN&gt;; }&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;    } &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;}&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 13pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 13pt; FONT-FAMILY: Trebuchet MS&#34;&gt;Concrete -&gt; Reflection &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;1. Objects &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyClass&lt;/SPAN&gt; myObject = &lt;SPAN style=&#34;COLOR: blue&#34;&gt;new&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyClass&lt;/SPAN&gt;();&lt;BR&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;Type&lt;/SPAN&gt; myType = myObject.GetType(); &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;2. Classes &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;Type&lt;/SPAN&gt; myType = &lt;SPAN style=&#34;COLOR: blue&#34;&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyClass&lt;/SPAN&gt;);&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Verdana&#34;&gt;&lt;STRONG&gt;Note: The examples from now on will assume we have the myObject and myType variables as defined above. &lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;3. Public Instance Methods &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MethodInfo&lt;/SPAN&gt; method = myType.GetMethod(&lt;SPAN style=&#34;COLOR: maroon&#34;&gt;&#34;MyMethod&#34;&lt;/SPAN&gt;);&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;4. Private Instance Methods &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MethodInfo&lt;/SPAN&gt; privMethod = myType.GetMethod(&lt;SPAN style=&#34;COLOR: maroon&#34;&gt;&#34;MyPrivateMethod&#34;&lt;/SPAN&gt;, &lt;SPAN style=&#34;COLOR: teal&#34;&gt;BindingFlags&lt;/SPAN&gt;.NonPublic | &lt;SPAN style=&#34;COLOR: teal&#34;&gt;BindingFlags&lt;FONT color=#000000&gt;.Instance &lt;/FONT&gt;&lt;/SPAN&gt;);&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;5. Static Methods &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MethodInfo&lt;/SPAN&gt; statMethod = myType.GetMethod(&lt;SPAN style=&#34;COLOR: maroon&#34;&gt;&#34;MyStaticMethod&#34;&lt;/SPAN&gt;, &lt;SPAN style=&#34;COLOR: teal&#34;&gt;BindingFlags&lt;/SPAN&gt;.Public | &lt;SPAN style=&#34;COLOR: teal&#34;&gt;BindingFlags&lt;/SPAN&gt;.Static);&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;6. Fields &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;FieldInfo&lt;/SPAN&gt; fi = myType.GetField(&lt;SPAN style=&#34;COLOR: maroon&#34;&gt;&#34;MyField&#34;&lt;/SPAN&gt;);&lt;STRONG&gt; &lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Verdana&#34;&gt;&lt;STRONG&gt;Note: For static or non-public fields, overloads exist similar to those for methods above.&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Trebuchet MS&#34;&gt; &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;7. Properties (with their associated Get/Set methods) &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;PropertyInfo&lt;/SPAN&gt; pi = myType.GetProperty(&lt;SPAN style=&#34;COLOR: maroon&#34;&gt;&#34;MyProperty&#34;&lt;/SPAN&gt;); &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MethodInfo&lt;/SPAN&gt; getMethod = pi.GetGetMethod();&lt;BR&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MethodInfo&lt;/SPAN&gt; setMethod = pi.GetSetMethod(); &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Verdana&#34;&gt;&lt;STRONG&gt;Note: For static or non-public properties, overloads exist similar to those for methods above.&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Trebuchet MS&#34;&gt; &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;8. Delegates &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyDelegate&lt;/SPAN&gt; del = myObject.MyMethod;&lt;BR&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MethodInfo&lt;/SPAN&gt; mi = d.Method;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;  &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;   &lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 13pt; FONT-FAMILY: Trebuchet MS&#34;&gt;Reflection -&gt; Concrete &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-FAMILY: Trebuchet MS&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt&#34;&gt;1. Objects &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 9pt&#34;&gt;&lt;STRONG&gt;(note the cast)&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyClass&lt;/SPAN&gt; myObj = &lt;SPAN style=&#34;COLOR: teal&#34;&gt;Activator&lt;/SPAN&gt;.CreateInstance(myType) &lt;SPAN style=&#34;COLOR: blue&#34;&gt;as&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyClass&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;2. Methods (invoking) &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Verdana&#34;&gt;For a static method, we will supply &lt;EM&gt;null&lt;/EM&gt; for the first parameter (the instance). &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;mi.Invoke(myObj, &lt;SPAN style=&#34;COLOR: blue&#34;&gt;new&lt;/SPAN&gt; &lt;SPAN style=&#34;COLOR: blue&#34;&gt;object&lt;/SPAN&gt;[] {&lt;SPAN style=&#34;COLOR: blue&#34;&gt;…&lt;/SPAN&gt;} );&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;3. Fields &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;&lt;SPAN style=&#34;COLOR: blue&#34;&gt;int&lt;/SPAN&gt; value = (&lt;SPAN style=&#34;COLOR: blue&#34;&gt;int&lt;/SPAN&gt;)fi.GetValue(myObj);&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;4. Properties &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Verdana&#34;&gt;Unlike fields, properties can take a set of parameters. Either parameterized properties in VB or indexers in C#. &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;&lt;SPAN style=&#34;COLOR: blue&#34;&gt;int&lt;/SPAN&gt; value = (&lt;SPAN style=&#34;COLOR: blue&#34;&gt;int&lt;/SPAN&gt;)pi.GetValue(myObj, &lt;SPAN style=&#34;COLOR: blue&#34;&gt;null&lt;/SPAN&gt;);&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt; FONT-FAMILY: Trebuchet MS&#34;&gt;5. Delegates &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Verdana&#34;&gt;Delegate to static method: &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyDelegate&lt;/SPAN&gt; del = (&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyDelegate&lt;/SPAN&gt;) &lt;SPAN style=&#34;COLOR: teal&#34;&gt;Delegate&lt;/SPAN&gt;.CreateDelegate(&lt;SPAN style=&#34;COLOR: blue&#34;&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyDelegate&lt;/SPAN&gt;), method);&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 12pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Verdana&#34;&gt;Delegate to instance method: &lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P style=&#34;BACKGROUND: #eeece1&#34;&gt;&lt;SPAN style=&#34;FONT-FAMILY: Courier New&#34;&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt;&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyDelegate&lt;/SPAN&gt; del = (&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyDelegate&lt;/SPAN&gt;)&lt;SPAN style=&#34;COLOR: teal&#34;&gt;Delegate&lt;/SPAN&gt;.CreateDelegate(&lt;SPAN style=&#34;COLOR: blue&#34;&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style=&#34;COLOR: teal&#34;&gt;MyDelegate&lt;/SPAN&gt;), myObj, method.Name);&lt;/SPAN&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt&#34;&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;SPAN style=&#34;FONT-SIZE: 10pt; FONT-FAMILY: Trebuchet MS&#34;&gt;Note that there is no syntax to create an instance delegate from a MethodInfo; for some reason we only have a weakly-typed overload that accepts the method name as a string. Only the first overload, for static methods, receives a strongly typed MethodInfo.&lt;/SPAN&gt;&lt;/P&gt;</description>
			</item>
			<item>
				<title>From Accessors to Properties</title>
				<link>https://bits.strawjackal.org/187/</link>
				<pubDate>Wed, 31 May 2006 12:02:00 +0200</pubDate>
				<guid>https://bits.strawjackal.org/187/</guid>
				<description>&lt;P&gt;For various debugging processes, I find myself needing to find several bits of runtime information for my properties. This means I want to get the property&#39;s PropertyInfo object by Reflection when that property&#39;s code is running.&lt;/P&gt;&#xD;&#xA;&lt;P&gt;The problem is that Properties are C# constructs, not IL constructs. My property&#39;s &lt;EM&gt;get&lt;/EM&gt; accessor is actually compiled into a &lt;EM&gt;get_MyProp()&lt;/EM&gt; method. This means that while I can easily get the MethodInfo for the current accessor, there doesn&#39;t appear to be any easy way of getting the PropertyInfo from the MethodInfo.&lt;/P&gt;&#xD;&#xA;&lt;P&gt;The ugly way, of course, is to take the method&#39;s name, strip the first four characters from it and do a GetProperty() with that name - and that works, of course, but causes me actual physical pain to use:&lt;FONT color=#008080 size=2&gt;&lt;/P&gt;&#xD;&#xA;&lt;P&gt;&lt;FONT face=&#34;Courier New&#34;&gt;PropertyInfo&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face=&#34;Courier New&#34;&gt; prop = accessorMethod.ReflectedType.GetProperty(accessorMethod.Name.Remove(0, 4));&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&#xD;&#xA;&lt;P&gt;Is there a better, simpler way I&#39;m missing?&lt;/P&gt;</description>
			</item>
			<item>
				<title>Non-generic default values in C#? Why not?!</title>
				<link>https://bits.strawjackal.org/191/</link>
				<pubDate>Sun, 21 May 2006 08:58:00 +0200</pubDate>
				<guid>https://bits.strawjackal.org/191/</guid>
				<description>&lt;p&gt;A relatively obscure new keyword in C# 2.0 is the default(T) keyword. &lt;br /&gt;It&#39;s shorthand for checking if the given generic parameter T is a value type or reference type. If it&#39;s a reference type, it returns null. Otherwise it returns 0/false/0.0 or whatever the default value for the type is.&lt;/p&gt; &lt;p&gt;For some reason, though, it can only be used on generic parameters. Why can&#39;t I use default(typeof(int)) to get 0? Or, more realistically, default(myUnknownType) to return the default value?&lt;/p&gt;</description>
			</item>
			<item>
				<title>System.Reflection.Omit</title>
				<link>https://bits.strawjackal.org/198/</link>
				<pubDate>Mon, 15 May 2006 06:45:00 +0200</pubDate>
				<guid>https://bits.strawjackal.org/198/</guid>
				<description>&lt;p&gt;In TechEd Israel 2006, I listened to &lt;A href=&#34;http://weblogs.asp.net/rosherove&#34;&gt;Roy Osherove&lt;/a&gt;&amp;rsquo;s excellent talk on the new &lt;A href=&#34;http://weblogs.asp.net/rosherove/archive/2006/05/14/446356.aspx&#34;&gt;reflection features&lt;/a&gt; in .NET 2.0 - a great assortment of topics on things ranging from DynamicMethods to the ReflectionOnly- set of properties.&lt;br /&gt;One of the things he mentioned was that the GetCustomAttributes method that&amp;rsquo;s exposed by various reflection types was unsafe. When the method returns the collection of custom attributes, it has to instantiate each one of them, and this means calling the constructor on each one of them. This could be the &amp;ldquo;crack in the door&amp;rdquo; for an attacker to use an extensible plug-in architecture to slide his own code by inheriting a custom atttribute with malicious constructor code and decorating a class with it.&lt;br /&gt;The solution in .NET 2.0 comes from the CustomAttributeData class, which has a single static method - GetCustomAttributes - which returns the list of attributes without calling their constructors. This seems a cleaner and safer solution, and I couldn&amp;rsquo;t wait to go and fix up my reflection code in the current project with it.&lt;br /&gt;&lt;br /&gt;The problem with this implementation, as always, is in the little details. The GetCustomAttributes method we have on the reflection types are implemented via the ICustomAttributeProvider interface, which is implemented by Type, ParameterInfo, MethodInfo and all those other attribute-supporting constructs. The GetCustomAttribute method of the CustomAttributeData class, though, has several overloads taking MemberInfos, Assemblys, ParameterInfo or Module - the four classes that implement ICustomAttributeProvider. This means that while it is theoretically equivalent to the older method, it does not allow me to cast my member/type/assembly to an ICustomAttributeProvider and perform operations on it - I have several attribute-related functions that take either a Type or a Property, so in this case I can cast them both to MemberInfos and use that as my lowest common denominator. But what if I had needed to handle Assemblies as well?&lt;br /&gt;&lt;br /&gt;A rather annoying ommision. Whenever I&amp;rsquo;m forced to use concrete classes rather than base-classes or interfaces, I get slightly nervous. It may be fine for the time being, but I know that somewhere down the line, I&amp;rsquo;ll try to refactor something and get bitten.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;EDIT: &lt;/b&gt;searching Google, it seems that I am the very first to use the &amp;ldquo;Reflection.Omit&amp;rdquo; pun, or at least to publish it on a Googleable medium. Cool.&lt;br /&gt;&lt;/p&gt;</description>
			</item>
	</channel>
</rss>
