<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>LifeCycle Solutions, LLC - Weblog - Reflection</title>
    <link>http://blog.lifecycle-solutions.com/</link>
    <description>Technical Writing</description>
    <language>en-us</language>
    <copyright>LifeCycle Solutions, LLC</copyright>
    <lastBuildDate>Thu, 28 Feb 2008 15:41:42 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>info@lifecycle-solutions.com</managingEditor>
    <webMaster>info@lifecycle-solutions.com</webMaster>
    <item>
      <trackback:ping>http://blog.lifecycle-solutions.com/Trackback.aspx?guid=f3b61d4a-4aac-4654-9d0c-3d39b2884429</trackback:ping>
      <pingback:server>http://blog.lifecycle-solutions.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.lifecycle-solutions.com/PermaLink,guid,f3b61d4a-4aac-4654-9d0c-3d39b2884429.aspx</pingback:target>
      <dc:creator>Daniel Root</dc:creator>
      <wfw:comment>http://blog.lifecycle-solutions.com/CommentView,guid,f3b61d4a-4aac-4654-9d0c-3d39b2884429.aspx</wfw:comment>
      <wfw:commentRss>http://blog.lifecycle-solutions.com/SyndicationService.asmx/GetEntryCommentsRss?guid=f3b61d4a-4aac-4654-9d0c-3d39b2884429</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've been experimenting with the idea of using System.Reflection.Emit to generate
code from interfaces.  If you're not familiar with this namespace, it contains
classes to generate compiled assemblies programmatically.   Regex uses this
when compiling a regular expression for faster execution.  Instead of parsing
the expression each time, it generates on-the-fly a class that can be used to execute
the expression.  This namespace is also used by Mock Type frameworks such as
RhinoMocks and TypeMock.  Given a base class or interface, these generate on-the-fly
a class that can mimic the base enough to use when unit testing.  I'm sure there
are other places this is used, but suffice it to say this is one of those obscure
corners of the framework that us Morts rarely dig into.  So it's not surprising
that I would run into an obscure error that stumped even Google.
</p>
        <p>
I religiously studied the MSDN examples and wrote code to let me do something like
this:
</p>
        <p>
ICustomer concreteType = CodeGenerator&lt;ICustomer&gt;.GetInstance();
</p>
        <p>
You can see how freaking awesome this could be for component development- I get, at
runtime, a class that implements my interface, but I don't have to wire up goo like
INotifyPropertyChanged, IDataError, property getter/setters, etc.   Depending
on how my tinkering goes, more on the awesomeness later.  The error I was getting
whenever I called typeBuilder.CreateType() was:
</p>
        <p>
System.TypeLoadException: Method 'get_Id' in type 'Customer' from assembly 'TypeAssembly,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation
</p>
        <p>
Except I WAS implementing 'get_Id'.  I copied the code to do it straight from
MSDN! See:
</p>
        <p>
MethodAttributes getSetAttributes = MethodAttributes.Public | 
<br />
MethodAttributes.SpecialName | 
<br />
MethodAttributes.HideBySig;<br />
var getMethodBuilder = typeBuilder.DefineMethod("get_" + propertyOnInterface.Name,<br />
                                               
getSetAttributes,<br />
                                              
propertyOnInterface.PropertyType,<br />
                                              
Type.EmptyTypes); 
</p>
        <p>
ILGenerator getMethodIl = getMethodBuilder.GetILGenerator();<br />
getMethodIl.Emit(OpCodes.Ldarg_0);<br />
getMethodIl.Emit(OpCodes.Ldfld, fieldBuilder);<br />
getMethodIl.Emit(OpCodes.Ret); 
</p>
        <p>
propBuilder.SetGetMethod(getMethodBuilder);
</p>
        <p>
Much googling turned up only a few unrelated posts.  After testing, I discovered
that removing a call to 'AddInterfaceImplementation' fixed the issue- though the resulting
class no longer implemented my interface.  Clearly the interface was generating
some sort of code.  Some more digging, and I figured out the solution:
</p>
        <p>
MethodAttributes getSetAttributes = MethodAttributes.Public | 
<br />
MethodAttributes.SpecialName | 
<br />
MethodAttributes.HideBySig;| <strong>MethodAttributes.Virtual</strong>;
</p>
        <p>
I could be flawed in my understanding here, but apparently, when adding an interface,
.NET treats property get and set accessors sort of like abstract methods.  The
code I was generating <em>did</em> implement 'get_Id', but did not mark it as overriding
the interface's definition.  So, when creating the type, there was the interfaces'
unfinished implementation of 'get_Id', and my completely unrelated 'get_Id'.  
</p>
        <p>
So anyway, hopefully this post will make it into Google and help some other poor guy
who, late one night, finds themselves turning over parts of the framework better left
alone.
</p>
        <img width="0" height="0" src="http://blog.lifecycle-solutions.com/aggbug.ashx?id=f3b61d4a-4aac-4654-9d0c-3d39b2884429" />
      </body>
      <title>Obscure Error: Method 'nnn' in type 'nnn' from assembly 'nnn' does not have an implementation</title>
      <guid isPermaLink="false">http://blog.lifecycle-solutions.com/PermaLink,guid,f3b61d4a-4aac-4654-9d0c-3d39b2884429.aspx</guid>
      <link>http://blog.lifecycle-solutions.com/2008/02/28/ObscureErrorMethodNnnInTypeNnnFromAssemblyNnnDoesNotHaveAnImplementation.aspx</link>
      <pubDate>Thu, 28 Feb 2008 15:41:42 GMT</pubDate>
      <description>&lt;p&gt;
I've been experimenting with the idea of using System.Reflection.Emit to generate
code from interfaces.&amp;nbsp; If you're not familiar with this namespace, it contains
classes to generate compiled assemblies programmatically.&amp;nbsp;&amp;nbsp; Regex uses this
when compiling a regular expression for faster execution.&amp;nbsp; Instead of parsing
the expression each time, it generates on-the-fly a class that can be used to execute
the expression.&amp;nbsp; This namespace is also used by Mock Type frameworks such as
RhinoMocks and TypeMock.&amp;nbsp; Given a base class or interface, these generate on-the-fly
a class that can mimic the base enough to use when unit testing.&amp;nbsp; I'm sure there
are other places this is used, but suffice it to say this is one of those obscure
corners of the framework that us Morts rarely dig into.&amp;nbsp; So it's not surprising
that I would run into an obscure error that stumped even Google.
&lt;/p&gt;
&lt;p&gt;
I religiously studied the MSDN examples and wrote code to let me do something like
this:
&lt;/p&gt;
&lt;p&gt;
ICustomer concreteType = CodeGenerator&amp;lt;ICustomer&amp;gt;.GetInstance();
&lt;/p&gt;
&lt;p&gt;
You can see how freaking awesome this could be for component development- I get, at
runtime, a class that implements my interface, but I don't have to wire up goo like
INotifyPropertyChanged, IDataError, property getter/setters, etc.&amp;nbsp;&amp;nbsp; Depending
on how my tinkering goes, more on the awesomeness later.&amp;nbsp; The error I was getting
whenever I called typeBuilder.CreateType() was:
&lt;/p&gt;
&lt;p&gt;
System.TypeLoadException: Method 'get_Id' in type 'Customer' from assembly 'TypeAssembly,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation
&lt;/p&gt;
&lt;p&gt;
Except I WAS implementing 'get_Id'.&amp;nbsp; I copied the code to do it straight from
MSDN! See:
&lt;/p&gt;
&lt;p&gt;
MethodAttributes getSetAttributes = MethodAttributes.Public | 
&lt;br&gt;
MethodAttributes.SpecialName | 
&lt;br&gt;
MethodAttributes.HideBySig;&lt;br&gt;
var getMethodBuilder = typeBuilder.DefineMethod("get_" + propertyOnInterface.Name,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
getSetAttributes,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
propertyOnInterface.PropertyType,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Type.EmptyTypes); 
&lt;/p&gt;
&lt;p&gt;
ILGenerator getMethodIl = getMethodBuilder.GetILGenerator();&lt;br&gt;
getMethodIl.Emit(OpCodes.Ldarg_0);&lt;br&gt;
getMethodIl.Emit(OpCodes.Ldfld, fieldBuilder);&lt;br&gt;
getMethodIl.Emit(OpCodes.Ret); 
&lt;p&gt;
propBuilder.SetGetMethod(getMethodBuilder);
&lt;/p&gt;
&lt;p&gt;
Much googling turned up only a few unrelated posts.&amp;nbsp; After testing, I discovered
that removing a call to 'AddInterfaceImplementation' fixed the issue- though the resulting
class no longer implemented my interface.&amp;nbsp; Clearly the interface was generating
some sort of code.&amp;nbsp; Some more digging, and I figured out the solution:
&lt;/p&gt;
&lt;p&gt;
MethodAttributes getSetAttributes = MethodAttributes.Public | 
&lt;br&gt;
MethodAttributes.SpecialName | 
&lt;br&gt;
MethodAttributes.HideBySig;| &lt;strong&gt;MethodAttributes.Virtual&lt;/strong&gt;;
&lt;/p&gt;
&lt;p&gt;
I could be flawed in my understanding here, but apparently, when adding an interface,
.NET treats property get and set accessors sort of like abstract methods.&amp;nbsp; The
code I was generating &lt;em&gt;did&lt;/em&gt; implement 'get_Id', but did not mark it as overriding
the interface's definition.&amp;nbsp; So, when creating the type, there was the interfaces'
unfinished implementation of 'get_Id', and my completely unrelated 'get_Id'.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
So anyway, hopefully this post will make it into Google and help some other poor guy
who, late one night, finds themselves turning over parts of the framework better left
alone.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.lifecycle-solutions.com/aggbug.ashx?id=f3b61d4a-4aac-4654-9d0c-3d39b2884429" /&gt;</description>
      <comments>http://blog.lifecycle-solutions.com/CommentView,guid,f3b61d4a-4aac-4654-9d0c-3d39b2884429.aspx</comments>
      <category>C#</category>
      <category>Reflection</category>
    </item>
  </channel>
</rss>