<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-497310774772282847</id><updated>2012-02-17T11:26:07.985Z</updated><category term='C#'/><category term='Voodoo'/><category term='Gaming'/><category term='Reflection'/><category term='Asp.Net MVC'/><category term='JavaScript'/><category term='WCF'/><category term='IIS'/><category term='WPF'/><category term='.Net'/><title type='text'>Court of Lord Zoltan v2</title><subtitle type='html'>Replacing my old blog at http://www.lordzoltan.org. C#, rants and other brain vomit</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>18</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-8028596050568290799</id><published>2011-08-05T23:00:00.000+01:00</published><updated>2011-08-05T23:00:57.401+01:00</updated><title type='text'>Story: A mystery of three</title><content type='html'>&lt;em&gt;I wrote this story a good few years ago now.&amp;nbsp;I've been meaning to put it up for a while now, and only now just got the wherewithall to do so.&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="center" class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: center;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;&lt;u&gt;&lt;span lang="EN-US" style="font-size: 12pt; mso-bidi-font-size: 10.0pt;"&gt;A mystery of three&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;span lang="EN-US" style="font-size: 12pt; mso-bidi-font-size: 10.0pt;"&gt; &lt;br /&gt;&lt;br /&gt;&lt;i style="mso-bidi-font-style: normal;"&gt;&lt;u&gt;By Andras Zoltan&lt;/u&gt;&lt;/i&gt; &lt;br style="mso-special-character: line-break;" /&gt; &lt;br style="mso-special-character: line-break;" /&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="MsoBodyTextIndent2" style="margin: 0cm 0cm 0pt;"&gt;&lt;span lang="EN-US"&gt;&lt;br /&gt;&lt;span style="font-family: inherit;"&gt; "Christ", thought Andras, as he sifted through the thousands of papers on the desk "Somewhere here are the words that'll elevate me to the next level, I need to find them, bring them together to make a collage of feelings and ideas; a catastrophe of logic and reason. But where do I start?"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoBodyTextIndent" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-family: inherit;"&gt;The bits of paper all had dark, angular scrawls all over them. On not one of them did any discernable sentence appear. Instead they seemed to have been leaked onto the paper both in the act of writing them as well as dreaming them up, as though a product of a deep trance. As soon as they caught his gaze, the words seemed to fly at him, each one's impact growing from the last; their chaos seeping into his mind as it tried to make sense of what he was seeing. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;He stared at one of the pages for a few seconds, his eyes glistening with intense concentration on the babble - using each word like a pin on a map; it was impossible to remember each of the words he was seeing, he'd thought earlier, so instead he would give them entry to his unconscious, by simply being seen. This way, the word would be stored but in a way such that only its trace remained; its meaning blurred by the randomness of the words before and after, a shadow, threatening to resurface at any time. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;All at once, with one of the pages in his hands, he got to his feet and started to shout the words; his voice would carry them to the heavens, their influence would be felt everywhere and by everyone. The reverberation of his voice seemed to magnify their impact on his mind, with each one a small change occurred inside him. Now he could &lt;i style="mso-bidi-font-style: normal;"&gt;feel&lt;/i&gt; them. Now he could take the power of these words and bring it together, focus it, &lt;i style="mso-bidi-font-style: normal;"&gt;use&lt;/i&gt; it. With a new purpose he started pacing, tossing the page aside and picking up a new one, he began once again to wreak language's havoc upon the world through his voice: "Elevation!!!" he cried, the room responding with a breath to acknowledge the signal being received. "&lt;b style="mso-bidi-font-weight: normal;"&gt;Liiiiiiiightning!!!!!!!&lt;/b&gt;", the images of huge sparks of white appearing around him; inside his head crashes of thunder echoed, reminding him of all the times he'd seen the sky turn into an electric playing field, smelled the sweet odour of clean air after a rainstorm and been petrified by the rumbling voice of the sky as a child. He paused, almost willing for there to be a crash of thunder in the distance, knowing that such an event would petrify him for a most different reason. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;Continuing on, he felt certain that right here, right now, something was happening to him, it must be embraced and encouraged. "These words have finally given one of their secrets up to me, it's now only time that decides when they come together, and me that decides exactly how they come together" he said to himself as he prepared for the  next incantation. It left his mouth in all directions like a bullet from a gun, bending the walls and shaking the windows, the room wasn't built for would-be opera singers.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"&lt;b style="mso-bidi-font-weight: normal;"&gt;HAAAUUUUNNNNTTTTT!!!!&lt;/b&gt;" &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;A door crashed far off down the corridor. Andras froze, the petrified feeling that he half-expected just before now visited upon him tenfold as a million nasty thoughts went through his mind as to &lt;i style="mso-bidi-font-style: normal;"&gt;what caused that noise&lt;/i&gt;? &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;Footsteps could be heard, approaching the other side of the door from the unknown; the being's appearance changing constantly inside Andras' mind as it advanced, his fear and anxiety increasing with each new form. He wondered if another echo from the depths of madness would cause the footsteps to go away, could a word be found to release him from this terrible nightmare? Is this the power of the words manifesting itself? In a last ditch effort he reached for the nearest page in his grasp, as he maneuvered it into view for him to incur it's wrath upon the demon outside, he filled his lungs with air and readied his body. His eyes scanned the letters, the syllables slotted into the empty spaces of his mind, all the strength inside him disappeared: "defeat?" he murmured, "but by what?"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;The footsteps stopped, the doorknob started turning slowly, scraping its way round to let the evil on the other side in. Suddenly the catch clicked free and as the door opened a hooded figure entered the room; not the six-legged hairy demon or chainsaw maniac he was expecting, but a figure he recognised. It was him. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"Hello, Andras", the other Andras said to him.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"Well, right about the last thing I expected to happen was for another me to appear", he said in a tone of disbelief.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;He dropped the page and stood motionless, staring at the image before him. With the hood still up, the face seemed far more angular and foreboding than he imagined his own face to be; and his eyes, they seemed to see straight through him - a gaze like that of a heroin addict. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;I stood, purposefully intimidating, and stared at him; his body frozen by fear and flummoxed by my realisation, it was amusing to see a man who'd been saturating his brain with words temporarily lost for them. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"What did you expect to happen? A genie to appear? All the world's problems solved in one fell swoop? Come on, Andras! The only reason I am here is because you summoned me here, you need me for something, to make sense of something for you."&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"That's ridiculous! In any case, it still doesn't explain how you can be standing in front of me.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;And… and why the hell should you come here, now, when all around me is nothing but madness?" His thought patterns were clearly messed up, reeling from the barrage of the chaos of vocabulary he'd inflicted on them before. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"I should have thought that was self-explanatory." A wry smile appeared on the other Andras' face.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;He started walking around the room, looking down at the floor, the desk and the pages strewn all over them. The glazed look disappeared from his eyes and he moved with a certain confidence and easiness, as if he knew everything around him as having once been home. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"Madness is not something I entertain, I was merely conducting an analytical approach to the problem I'm faced with; how that's led to your appearance I don't know. It seems strange to me that you've appeared in a guise that I would never consider, let alone hallucinate." The perplexed Andras straightened up, desperately trying to elude some sense of confidence in his situation.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"Don't talk rubbish, " the hooded man snapped straight back at him, "you felt something when those words charged out of you.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Release…&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;No, a satisfaction, at being able to create images with them, of old memories resurfacing and washing over you again. I could feel it in you. There may have been an element of the scientist in you, but you're not going to convince me you didn't start to let go juuuussst a little." Very briefly a friendliness appeared in his manner, he knew more about me than I was willing to give up to anybody and he was taking pleasure in that. I watched him survey the pages once again; it was uncanny the way he seemed to observe them in order, but an order that was discernable to no other than himself. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;Nothing about what I saw puzzled me.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;I remembered the intense cascade of feelings that gave birth to them, the hate, the pleasure, the confusion, and how they had all combined to produce a string of impressions on the paper; hurried along as if there were already millions upon millions queuing up to do the same.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Nothing came out in order, a complete mish mash; and here was this fool trying to make sense of it all, though I was certainly not going to let him in on the secret just yet.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;I didn't even know whether there was something to make sense of, although I guess if you look at anything long enough, you'll find something in there with meaning.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;He was even trying to make sense of me, like he did of everything else that confronted him.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"What are you doing with all this stuff?" The second Andras muttered, turning once again to face the first one.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"I... errr, it's kinda difficult to explain, but somewhere here are the words that will release me, but I have no way of knowing which of the words I have to use or in what order.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Do I have to make a sentence? Read all the words in turn? Create a play? A poem... who knows?"&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;I considered the possibility that this explanation was unnecessary, that the other Andras already knew what I was doing, but the drive to justify my ridiculous behaviour was impossible to fight.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"So, your first urge is to read each of the words randomly and then start shouting them out like a madman!? Do you have any idea what could happen to you if somebody found out? They'd have you locked and drugged up fast, you'd only be able to remember the word 'Daadaa' by the time they'd finished with you!" &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"I couldn't think of anything else, it seemed like the logical thing to do. In any case it wasn't really a decision but it, sort of, just happened.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;I swear something inside me has been changed or switched on after doing that." &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"Damn right, you’re standing here talking to me!&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;I think that constitutes a big change." he tapped his head through his hood thrice with his finger, making a deep hollow click with this tongue each time, insinuating insanity.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Then he started towards his opposite. "Y'know the universe is supposed to explode if we touch…" &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 1cm;"&gt;&lt;span lang="EN-US" style="mso-bidi-font-size: 10.0pt;"&gt;&lt;span style="font-family: inherit;"&gt;"B-but you're not made of antimatter, if you were, then you wouldn’t be able to touch anything in the room" Andras' heart started racing, he could feel his mind starting to crack, things were not right about all this, he stepped back, not sure whether to take this imposing shadow, himself, seriously - a position he'd never expected to be in. &lt;br /&gt;&lt;br /&gt;"Ah yes, that's true, but &lt;i style="mso-bidi-font-style: normal;"&gt;you&lt;/i&gt; could be. Maybe you're the imposter in this scene, the irregularity, the blip." he accentuated this last word with amazing precision, I felt condemned. &lt;br /&gt;&lt;br /&gt;"Think about it: I am at home here, you are not; I am in acceptance of my situation, you are not. You clearly cannot make head nor tail of what you see before you..." &lt;br /&gt;&lt;br /&gt;"And you're saying you can? There's nothing to make sense of here! The only person who can shed any light on all this is the mad bastard that vomited it all up, and he's disappeared from existence! There's no trace of him, except what's here." &lt;br /&gt;&lt;br /&gt;"So take a step back from the situation for the moment, isn't that what you're supposed to do? Who do you think that mystical person could be?" &lt;br /&gt;&lt;br /&gt;I stared at him, and he at me, throughout a moment of eerie silence, as if time had stopped everywhere in the universe just to allow me to catch up with my situation. I knew the answer, I just didn't want to say it. &lt;br /&gt;&lt;br /&gt;"You've got it, it's me. What you see in front of you is me, embodied in every word. I can't remember how long it took or why each of the words appeared, but it was me that brought them into existence." &lt;br /&gt;&lt;br /&gt;"You can't have written all this stuff, it's impossible... Why would you create a roomful of paper covered in random words? Why would I be placed here to sift through it all? How can a hallucination have his own private storeroom of crap? It doesn't make sense!" &lt;br /&gt;&lt;br /&gt;"You're here for your own reasons now, as I was when I wrote all this. If you feel that you're job is to make sense of what you see before you then go ahead and try. Just bear in mind that it was not sense and order that produced it, but chaos. In a way, there is a lot of you here, too, it's just that you've not realised it yet." &lt;br /&gt;&lt;br /&gt;"How can that be? I am not you, " another portion of Andras' consciousness started to crack, "I could not have produced any of what lies here, it's alien to me. You're alien to me. It is I that must find the combination that will release me, but it is you that produced it." Why would I put myself in such a predicament anyway? he asked himself, seating himself at the table and putting his hands on his head. Nothing could explain what was going on, not even the all-knowing, all-seeing phantasm before him. The more he thought about it, there was no reason for his drive to search through the randomness that surrounded him, it was just automatic - something that had to be done. &lt;br /&gt;&lt;br /&gt;"We are not the same person. Christ, if that were true then the universe would explode if we touched, but we are of the same essence. It must be obvious to you that we share something in common. I mean, come on! Hasn't my appearance demonstrated something to you? Doesn't my very appearance demonstrate something to you? Has it ever occured to you that the name Andras can represent two different identities, both intertwined with the same purpose?" &lt;br /&gt;&lt;br /&gt;"But what purpose? What is about you and me that is the same? What is it that holds the key to my release from this ever-increasing nightmare? You seem to be here only to confuse things to a greater degree, to encourage that which already lives in this room." I could not function, the sanity ebbing away from me. There I was, seated at a table covered with anonymous, yet supposedly familiar, oozed-out feelings and thoughts; I felt like screaming but could select no words to describe what was inside. The hooded-me produced an empty page from his coat and drew his finger across it's surface, on it the word bled, like a punchline delivered by a mute. &lt;br /&gt;&lt;br /&gt;Disbelief, followed by intense curiosity, appeared on Andras' face as my finger worked it's way up and down the page. I could sense him desperately trying to figure out whether or not what had appeared before him referred to him or to me. The drive for me to manufacture it was undefeatable, and there was no conscious thought given to it's inception, yet still there were ways of relating it back to our situation that he would no doubt find. Once finished I stood staring at it, held out for us both to see. It needed to be felt, even if it wasn't real. &lt;br /&gt;&lt;br /&gt;Everything settled in the room for a moment, the two Andrasses transfixed in time as the word joined the others; making the room more complete. &lt;br /&gt;&lt;br /&gt;"Defeat?" I asked him, dumbfounded at it's appearance yet again. He didn't reply, just stared back at me. "Am I defeated? Am I going to fail in my task to fathom your craziness? Or am I defeating you right now?" Nothing, no clue signalled by his gaze. The exasperation inside me was becoming unbearable, I felt I was being withheld something, a secret that I should know if was to continue on my travels. &lt;br /&gt;&lt;br /&gt;"Of course you're going to fail in your task to fathom these words," he started abruptly, snapping out of his trance. He started pacing around the floor, bending slowly to let the page from his grasp. "You're going to fail in trying to make sense of me, these words, this room and anything else you turn your head to. You need me to understand this, I have to be here for anything to make sense." &lt;br /&gt;&lt;br /&gt;I was being preached to and I knew it, dissected and repaired by this other me. I knew nothing about him, yet he knew me; perhaps he really is a figment of my imagination, I thought. If he is, he's an annoyingly mysterious one. "Without me being here you will never find the release you search for, your constantly trying to find a pattern or a solution to everything will not help you." &lt;br /&gt;&lt;br /&gt;It was like listening to a record impressed with the ramblings of an insane sage, the words buzzing through the air in search of a listener. Still, what he said made sense. All my efforts to make sense of the situation had failed and my coup de graçe had resulted in the scaring of my life. Even now, as we talked, I was still uneasily wary of him. &lt;br /&gt;&lt;br /&gt;"So, if you're purpose is the same as mine, and my purpose is to find the words that can release me, then..." no chance of finishing the sentence, as the hood rushed forward to interject. &lt;br /&gt;"Is your purpose to find words? What makes you so sure of that? Have you been given any instructions as to what to do in your time here? You've assumed a role from the moment you got here, much in the same way that I have." &lt;br /&gt;&lt;br /&gt;"I don't know any different to what I started doing back there." &lt;br /&gt;&lt;br /&gt;"Just as I don't know any different to what I'm doing now." &lt;br /&gt;&lt;br /&gt;"But, even though I feel driven to, you say my purpose here is not to find order?" &lt;br /&gt;&lt;br /&gt;"Perhaps it's to create the madness, the chaos, much like you think mine is? How do you think all the other pieces of paper have got here?" &lt;br /&gt;&lt;br /&gt;A bomb dropped somewhere in the distance, splintering everything within range into tiny pieces. &lt;br /&gt;&lt;br /&gt;"This... means... that you are here purely because I am; there's something inside of me that needs to get out and you're the key to it's... it's release." The walls began to pulsate, mimicking the blood pumping through my veins as the realisation hit me. I released my head from my hands' grasp and looked, once again, at my double. &lt;br /&gt;&lt;br /&gt;"Not just me, but the pages, too. The products of our previous visits here, and memories that have built up over the years; I haven't dreamed all of these words up on my own, y'know, just a lot of them. You're not likely to remember anything about that, though." The lack of seriousness in his voice hid the scale of what he was saying. I was standing in my (or should it be "our"?) own private storeroom of memories and ideas, represented by words on bits of paper, to be sifted through and added to at will and I didn't recognise any of it. What a ridiculous place to get stuck in with another me, I thought. &lt;br /&gt;&lt;br /&gt;"I don't want to create chaos, though, I have no desire to eek out any more madness than the cornucopia that already adorns this room. So why am I here?". &lt;br /&gt;&lt;br /&gt;"You think this is a one-way relationship? I may be the voice of your meanderings but I have a voice that needs to be heard too. To do this, I need a catalyst: You." He hoisted himself onto the table and sat hunched forward, staring sombrely at the opposite wall, upon which a page hung with the word "fountain" clearly etched into it; the hood chuckled shortly and turned to Andras. "It's just that, without you, my voice can emit nothing but total chaos, nothing worth saying. You inject the order into it through your presence and interaction with me." &lt;br /&gt;&lt;br /&gt;"But you seem to able to speak properly, I mean, there's no chaos in what you're saying to me right now." &lt;br /&gt;&lt;br /&gt;"That's as maybe, but consider what I'm like when I'm on my own. All this stuff bouncing around and nobody to express it to; I am limited just to this." His hand swooped around the room, indicating the pages. "This place is horrible on your own, let me tell you, the words around you choke the mind and confuse the soul - concentrating on them is a sure way of going mad, the only way to fit in is to continue to add more, expand the zoo as it were. With you here, though, the room and its contents are given a context, a place in time, the words and any subsequent additions take on a whole new meaning." &lt;br /&gt;&lt;br /&gt;"But where is the product of our interaction? From what you're telling me, us coming together is supposed to lead to something being created, I see nothing; apart from the random words you continue to write as normal." &lt;br /&gt;&lt;br /&gt;"The product is not for you, or I, to see. Only to create." &lt;br /&gt;&lt;br /&gt;As he said this he drew another page from his coat and began writing, again with his finger. He could not explain what was drawing the energy out of him, the lines appearing as if they were already there but needed his touch to make them visible. The other Andras stood up to see what was being written this time. What word would come of this? What was inside the hood that required everything to stand still in order for it to appear? &lt;br /&gt;&lt;br /&gt;Silence. &lt;br /&gt;&lt;br /&gt;It was broken, "Of all the words...", Andras started, and before he could finish he was no longer there - any trace of his being purged from the room. &lt;br /&gt;&lt;br /&gt;"Of all the words, indeed." The other Andras said, and disappeared into the air to be become nothing but a whisper… &lt;br /&gt;&lt;br /&gt;The page sank to find its place on top of all the others. For a moment it glowed there, in the middle of the floor, its scrawled companion shouting its presence to all the others: &lt;br /&gt;&lt;br /&gt;"Release". &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-family: inherit;"&gt;  &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-8028596050568290799?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/8028596050568290799/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2011/08/story-mystery-of-three.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/8028596050568290799'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/8028596050568290799'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2011/08/story-mystery-of-three.html' title='Story: A mystery of three'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-1938921160702229729</id><published>2011-02-11T22:26:00.001Z</published><updated>2011-02-11T22:26:13.188Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='IIS'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><title type='text'>Asp.Net 2 and 4 default application pool generates CS0016 IIS7.5</title><content type='html'>&lt;p&gt;Before I start – if you’ve found a bunch of other articles about this around the net, tried the fixes that are mentioned and still not getting any joy – then read on – you might find this solves your problem. &lt;p&gt;Earlier today I discovered that when I run any ASP.Net 2 or 4 application through IIS7.5 using the default application pools (which use ApplicationPoolIdentity) on Windows 2008 R2 x64 I get an error message similar to this:  &lt;h3&gt;&amp;nbsp;&lt;/h3&gt; &lt;h3&gt;Server Error in '/MvcApplication31' Application.  &lt;hr size="1" width="100%"&gt; &lt;/h3&gt; &lt;h4&gt;&lt;i&gt;Compilation Error&lt;/i&gt;&lt;/h4&gt;&lt;b&gt;Description: &lt;/b&gt;An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. &lt;br&gt;&lt;b&gt;Compiler Error Message: &lt;/b&gt;CS0016: Could not write to output file 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\mvcapplication31\222b4fe6\4e80a86\App_global.asax.clb4bsnc.dll' -- 'The directory name is invalid. '&lt;br&gt;&lt;b&gt;Source Error:&lt;/b&gt;  &lt;p&gt;&lt;code&gt;&lt;pre&gt;[No relevant source lines]&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Source File:&lt;/b&gt; &lt;b&gt;Line:&lt;/b&gt; 0 &lt;br /&gt;&lt;p&gt;(When running an ASP.Net 2 app, the version folder is of course different) &lt;br /&gt;&lt;p&gt;Now, I only just picked this up today (despite doing web dev on this machine build for the last 18 months) because early on I changed the Asp.Net 2 ‘DefaultAppPool’ to use Network Service as I needed the server to make web requests to web services (PayPal etc) through a corporate proxy that uses AD authentication.&lt;br /&gt;&lt;p&gt;Today, however, I started a project that will be using Asp.Net 4 for, and so I was using that app pool for the first time (and hadn’t touched it all since installing .Net 4.0, VS2010 etc).&lt;br /&gt;&lt;p&gt;I tried uninstalling .Net completely, reinstalling it, (re)installing Asp.Net 2 &amp;amp; 4 but no matter what, this always kept happening.&lt;br /&gt;&lt;p&gt;However, with either app pool set to use Network Service it all worked fine. &lt;br /&gt;&lt;p&gt;Now, using my google-fu I found a few links (&lt;a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;825791"&gt;http://support.microsoft.com/default.aspx?scid=kb;en-us;825791&lt;/a&gt;, &lt;a href="http://dudesdoingdotnet.blogspot.com/2007/11/compiler-error-cs0016-could-not-write_2486.html"&gt;http://dudesdoingdotnet.blogspot.com/2007/11/compiler-error-cs0016-could-not-write_2486.html&lt;/a&gt; and &lt;a href="http://wiki.evident.nl/Default.aspx?Page=Compiler%20error%20CS0016%20The%20directory%20name%20is%20invalid"&gt;http://wiki.evident.nl/Default.aspx?Page=Compiler%20error%20CS0016%20The%20directory%20name%20is%20invalid&lt;/a&gt;), most of which suggest that the problem is to do with %TMP% or %TEMP% folder access for Network Service, or that the folder isn't present.&lt;br /&gt;&lt;p&gt;Well, clearly that wasn’t the case here as these run as the dynamic IIS APPPOOL\[app pool name] built in accounts that are created for the app pool, and not Network Service.&amp;nbsp; That said, I realised it's possible that the dynamic account didn't have access (or the group it belongs too), so I granted it full permissions on my %TMP%folder (it and %TEMP% both equal to C:\windows\temp) and tried again. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Still no joy. &lt;br /&gt;&lt;p&gt;Now the strange thing was that the folder had some pdbs and compiler traces in it – just no DLLs.&amp;nbsp; So clearly files were getting created and the identity was able to write to the folder – but what was so special about the DLLs?&lt;/p&gt;&lt;br /&gt;&lt;p&gt;I tend to swear quite a lot in the office to be honest, even when something not very bad happens; but after about the 5th or 6th time of failing to fix it I was raging.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;So I came home, had some dinner and decided to jump on again.&amp;nbsp; This time I loaded up &lt;a title="Process Monitor" href="http://technet.microsoft.com/en-us/sysinternals/bb896645"&gt;Process Monitor&lt;/a&gt;, set the filter to w3wp.exe only and watched as I generated the error again.&amp;nbsp; After more filtering I found entries for the file reported in the error, with the result ‘NAME NOT FOUND’ – but noticed that other files that were also successfully created also had this, so that couldn’t be it.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Then it dawned on me that the process to watch was not w3wp – but csc.exe (if using c#).&amp;nbsp; So I changed the filter and found loads of entries that referenced &lt;strong&gt;c:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp&lt;/strong&gt;, with a status of ‘ACCESS DENIED’ – the user listed as IIS APPPOOL\Asp.Net v4 (for .Net 4 – it would be IIS APPPOOL\DefaultAppPool for .Net 2 on a default install of Windows Server 2008 R2).&lt;/p&gt;&lt;br /&gt;&lt;p&gt;So I gave the IIS_IUSRS groups full control of that folder – IISRESET for safety, and hit the site again.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Yay!&amp;nbsp; It worked!&lt;/p&gt;&lt;br /&gt;&lt;p&gt;So, in some cases, it’s not the root temp folder &lt;strong&gt;C:\Windows\Temp&lt;/strong&gt; that you need to be looking at – but this other one that belongs to the NetworkService user profile.&amp;nbsp; The question is: why was this not already properly configured on my machine or, equally, is this actually the correct folder for the built in Asp.Net accounts?&lt;/p&gt;&lt;br /&gt;&lt;p&gt;My colleague’s machine didn’t have the same problem at all – so I have no idea why my machine (and I &lt;em&gt;do&lt;/em&gt; look after it!) did.&amp;nbsp; Either way at least it’s now fixed!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-1938921160702229729?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/1938921160702229729/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2011/02/aspnet-2-and-4-default-application-pool.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/1938921160702229729'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/1938921160702229729'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2011/02/aspnet-2-and-4-default-application-pool.html' title='Asp.Net 2 and 4 default application pool generates CS0016 IIS7.5'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-9200480752632310514</id><published>2011-01-28T09:23:00.001Z</published><updated>2011-01-28T09:23:33.430Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='Reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><title type='text'>ObsoleteAttribute and the Expression Compiler</title><content type='html'>&lt;p&gt;I was going to post this as a question over at &lt;a href="http://stackoverflow.com/" target="_blank"&gt;SO&lt;/a&gt;, but as I got to the end I realised a blog would probably be better (apart from the fact that only five people will read it!).&lt;/p&gt; &lt;p&gt;The project I'm working uses runtime objects that are produced with configuration-driven delegates built using Expression trees.&lt;/p&gt; &lt;p&gt;I'm doing a big functionality merge at the moment and have reached a class that, because of another that has grown, is now obsolete. So I want to make it 'properly' obsolete (generate compiler errors), go through the static codebase and change all the references.&lt;/p&gt; &lt;p&gt;The problem is that the class library is used, through Expressions, by a few other apps so I can't yet get rid of the class completely - I'll perhaps look at farming that job off to somebody else.&lt;/p&gt; &lt;p&gt;So I thought I'd just check whether the expression compiler honours the ObsoleteAttribute when marked as an error, and it doesn't:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:41cd2ab3-7ac9-4bb4-a8bc-7f2f14df3b9f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;TestClass&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;]&lt;/span&gt;&lt;br&gt; &lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;UnitTest1&lt;/span&gt;&lt;br&gt; &lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;br&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Obsolete&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;obsolete&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;true&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)]&lt;/span&gt;&lt;br&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObsoleteClass&lt;/span&gt;&lt;br&gt;     &lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;br&gt; &lt;br&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;br&gt; &lt;br&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;TestMethod&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;]&lt;/span&gt;&lt;br&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; TestObsoletedClassViaExpression()&lt;/span&gt;&lt;br&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;br&gt;         &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Type&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; obsoleteType &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;typeof&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;UnitTest1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetNestedType(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;ObsoleteClass&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;);&lt;/span&gt;&lt;br&gt; &lt;br&gt;         &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#2cbe80"&gt;Func&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; f &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;br&gt;             &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Expression&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Lambda&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#2cbe80"&gt;Func&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;br&gt;                 &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Expression&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;New(obsoleteType)&lt;/span&gt;&lt;br&gt;             &lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Compile();&lt;/span&gt;&lt;br&gt; &lt;br&gt;         &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;var&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; o &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; f();&lt;/span&gt;&lt;br&gt; &lt;br&gt;         &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Assert&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;AreEqual(obsoleteType, o&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetType());&lt;/span&gt;&lt;br&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;br&gt; &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;In my case this is actually an advantage as it means the existing projects will carry on working with the updated codebase since the only reference to this type will be through dynamically generated code.&lt;/p&gt; &lt;p&gt;Of course once I finally remove the type those apps will no longer work - but I won't do that until they've all been updated. That said, it would be a lot easier to go through these apps an augment them to the replacement type if the Expression compiler *did* honour the ObsoleteAttribute - just follow the runtime errors (! - sorry I mean run the tests of course ;) ).&lt;/p&gt; &lt;p&gt;I understand, and have exploited, the fact that the Expression compiler is allowed to do things that aren't normal - skip visibility checks etc. I also understand that attributes such as ObsoleteAttribute require a compiler to honour them as, at the IL level, they are meaningless.&lt;/p&gt; &lt;p&gt;However, given Extression trees' ubiquity and growing importance, shouldn't its compiler honour (or have the option to honour) the ObsoleteAttribute? On the DLR, for example, this means that a language designer must code in reflection at every step to look for Obsolete(true) declarations if he/she wants to have this functionality in their language.&lt;/p&gt; &lt;p&gt;Like I say, it’s a tricky one because in this particular example it’s going to work in my favour – I’m just not sure it should.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-9200480752632310514?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/9200480752632310514/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2011/01/obsoleteattribute-and-expression.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/9200480752632310514'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/9200480752632310514'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2011/01/obsoleteattribute-and-expression.html' title='ObsoleteAttribute and the Expression Compiler'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-6347291701584489863</id><published>2010-11-03T15:05:00.001Z</published><updated>2010-11-03T15:05:10.883Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='Gaming'/><title type='text'>Colin McRae Dirt 2 – Choppy sound and crashes in multiplayer</title><content type='html'>&lt;p&gt;I like my driving games; always have done since the original Mario Kart on the SNES way back when.&lt;/p&gt; &lt;p&gt;So the other day I bought &lt;a href="http://store.steampowered.com/app/12840/" target="_blank"&gt;Dirt 2&lt;/a&gt; on &lt;a href="http://www.steampowered.com" target="_blank"&gt;Steam&lt;/a&gt; as I needed some driving action (my previous driving squeeze was &lt;a href="http://store.steampowered.com/app/12750/" target="_blank"&gt;Grid&lt;/a&gt; – and I can highly recommend that too).&amp;nbsp; Very cool, especially with XBox 360 wireless controllers.&lt;/p&gt; &lt;p&gt;After clocking up 20 hours or so on the solo missions I decided to give the online game a go.&amp;nbsp; Whilst in the trailer I navigated to the Multiplayer ‘board’ and hit ‘online’.&lt;/p&gt; &lt;p&gt;The instant I hit the button on the pad, the sound started to crackle &amp;amp; stutter; then I lost everything except the menu sound effects (only later for them to return for about 10 seconds). I’ve had this before (during the videos on Streetfighter 4, for example) and I just thought ‘well if the game still plays, I’ll put up with it for now’.&lt;/p&gt; &lt;p&gt;Only it didn’t play – not exactly anyway.&amp;nbsp; The first race went okay (apart from getting battered by the opposition!), and then I went back to the menu ready for the next race to begin.&amp;nbsp; When the next race began to load, the game just froze.&lt;/p&gt; &lt;p&gt;So I fired it up again and had another go.&amp;nbsp; Got through the first race again, only on the return to the menu, it got stuck when the camera zooms out from the ‘photo’ that’s taken at the end.&lt;/p&gt; &lt;p&gt;All the while, the sound’s doing crazy things.&amp;nbsp; Oh and the framerate in-race was not as good as it should have been either (of course it can drop slightly in Multiplayer given the extra work going on to do the networking).&lt;/p&gt; &lt;p&gt;Now my machine isn’t top-end (at least not until the year’s bonus comes in ;) ) but it’s still pretty beefy:&lt;/p&gt; &lt;p&gt;Intel Q9650 on an NVidia 780i Ultra Quad Sli board (can’t remember the manufacturer any more, possibly ASUS – but it cost a bit).&lt;br&gt;4Gb Corsair Ram as 2x2Gbs using stock timings&lt;br&gt;Two BFG GTX 280s (not OCd) in SLI&lt;br&gt;SB x-Fi Pci-ex card.&lt;/p&gt; &lt;p&gt;So there’s really no excuse there for the kind of crap that was going down!&lt;/p&gt; &lt;p&gt;I did a google search for the problem and lots of people have been reporting similar problems, but seemingly not to any decent conclusion.&amp;nbsp; Lots of talk of onboard hardware, out of date drivers etc.&amp;nbsp; One common thread seemed to be Win7 x64 (which I run), but ultimately my machine couldn’t be any more up-to-date driver-wise (I’m a habitual upgrader).&lt;/p&gt; &lt;p&gt;Lots of people were moaning about the Rapture sound system that the game uses as well – well, mine was setup at it’s defaults (and still is).&lt;/p&gt; &lt;p&gt;So I gave up.&lt;/p&gt; &lt;p&gt;A couple of days later I was mucking about on the Games for Windows (GfW) screens in-game and stumbled across the Voice settings.&amp;nbsp; Guess what – as soon as the UI for that settings panel was displayed, the exact-same sound interference kicked in again!&amp;nbsp; I exited that screen and it went away straight away too.&lt;/p&gt; &lt;p&gt;Now, I don’t use a headset with a microphone (don’t play any games where verbally abusing everyone else will either help or add anything); so what I’m about to say will NOT help those of you that do.&lt;/p&gt; &lt;p&gt;I tried disabling the voice stuff in GfW, but it didn’t make any difference.&lt;/p&gt; &lt;p&gt;So instead I pressed ALT+ENTER to get out of full screen and opened the Windows Recording Devices UI from the Volume Control.&amp;nbsp; I went through and disabled all the recording devices.&lt;/p&gt; &lt;p&gt;Went back to the game window and back into fullscreen (ALT+ENTER again) and all of a sudden the sound was clean again on the Voice UI in the GfW overlay.&lt;/p&gt; &lt;p&gt;So, immediately I went to the multiplayer menu again – hit ‘Online’ as I was before and this time – no crackle, no stuttering or anything: everything working exactly as it should.&lt;/p&gt; &lt;p&gt;I then went on to play (and lose!) a bunch of online races, no pauses between loading, frame-rate nice and crisp as in Single Player and, best of all, sound stayed intact all the way through.&lt;/p&gt; &lt;p&gt;So – there you have it – disable the recording devices (probably only have to disable one or two of them, but I don’t do any audio recording on my machine anyway, so I’m happy with them all switched off) and have your problem fixed!&lt;/p&gt; &lt;p&gt;There’s clearly an underlying bug here – possibly at the Dirt 2 level, or equally at the GfW level, so that needs to be fixed; although the game is nearly a year old now so I doubt it ever will be.&lt;/p&gt; &lt;p&gt;In the meantime I can now get back to getting my ass handed to me by supafly teenagers with too much time on their hands; like I used to be 15 years ago.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-6347291701584489863?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/6347291701584489863/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/11/colin-mcrae-dirt-2-choppy-sound-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/6347291701584489863'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/6347291701584489863'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/11/colin-mcrae-dirt-2-choppy-sound-and.html' title='Colin McRae Dirt 2 – Choppy sound and crashes in multiplayer'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-1119992528090447174</id><published>2010-09-21T15:18:00.001+01:00</published><updated>2010-09-21T15:18:13.161+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>Map System.TimeSpan to xs:duration for the DataContractSerializer</title><content type='html'>&lt;p&gt;I’m working on a new RESTful service (I’m using &lt;a title="Asp.Net MVC official site" href="http://www.asp.net/mvc" target="_blank"&gt;Asp.Net MVC&lt;/a&gt; for this) and I’m keen to make it as easy to integrate with as across as many client platforms as possible.&lt;/p&gt; &lt;p&gt;I’m going to be using XML exclusively, as I it enables me to produce schemas that our service clients will be able to download and then see the shape of each object that the service operations will expect.&lt;/p&gt; &lt;p&gt;I also want to employ XML schema validation on the data coming in from the request.&amp;nbsp; Doing this will trap most data errors it gets further down the request pipeline, thus protecting my code; but will also ensure that the caller gets nice verbose error messages – XML Schema Validation is pretty explicit on what’s gone wrong!&lt;/p&gt; &lt;p&gt;Thus, I’ve wired up an MVC action to produce a schema for the relevant objects using the &lt;a title="XsdDataContractExporter class on MSDN" href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.xsddatacontractexporter.aspx" target="_blank"&gt;XsdDataContractExporter&lt;/a&gt; class.&amp;nbsp; I will simply point users at this, alongside documentation for each of the operations; which will include schema type names from that ‘live’ schema.&lt;/p&gt; &lt;p&gt;While testing out one of the types, I noticed that a &lt;a title="TimeSpan structure on MSDN" href="http://msdn.microsoft.com/en-us/library/system.timespan.aspx" target="_blank"&gt;TimeSpan&lt;/a&gt; member was being serialized as &lt;a title="XML String data types on w3schools" href="http://www.w3schools.com/Schema/schema_dtypes_string.asp" target="_blank"&gt;xs:string&lt;/a&gt; and not as &lt;a title="XML Date data types on w3schools (duration near bottom)" href="http://www.w3schools.com/Schema/schema_dtypes_date.asp" target="_blank"&gt;xs:duration&lt;/a&gt;.&amp;nbsp; I consulted &lt;a title="DataContractSerializer Supported Types on MSDN" href="http://msdn.microsoft.com/en-us/library/ms731923.aspx" target="_blank"&gt;an MSDN topic I should now know by heart&lt;/a&gt; (given how many times I’ve looked at it) to check the support for TimeSpan in the &lt;a title="DataContractSerializer class on MSDN" href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx" target="_blank"&gt;DataContractSerializer&lt;/a&gt; and sure enough it’s there; but I couldn’t understand why, if DateTime is indeed mapped to the XML DateTime type, it’s not mapped to the XML Duration type.&lt;/p&gt; &lt;p&gt;So I’ve written a TimeSpan type called XmlDuration that is implicitly convertible to System.TimeSpan but which, when you expose it as a member on a &lt;a title="Using Data Contracts" href="http://msdn.microsoft.com/en-us/library/ms733127.aspx" target="_blank"&gt;Data Contract&lt;/a&gt;, presents itself as xs:duration.&amp;nbsp; This then means that if you enable schema validation on your incoming XML, the input string will be validated against the rules attached to the XML Duration type, instead of being simply a string that allows any content.&lt;/p&gt; &lt;p&gt;The code is as follows.&amp;nbsp; There’s one really long line in there which is a Regex that I’ve split into multiple string additions purely for this post; you can join the strings back up again if you so desire:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; width: 100%; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f5eff584-e8bf-4aa9-8098-77adce7e7c7f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; This type is a TimeSpan in the .Net world but, when &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; serialized as Xml it behaves like an XML duration type.  &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; For more about the duration data type in XML - see&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; http://www.w3.org/TR/xmlschema-2/#duration&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; You should use this on types that intend to send a &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; timespan to ensure clients can read the data in a &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; conformant manner.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; Note that when the type writes out to XML, it starts &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; with days; not years.  That is because .Net timespan &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; only expresses days.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; If the original duration string is preserved from &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; the input XML, then the same duration instance will &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; serialize out using that same string.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlSchemaProvider&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;GetTypeSchema&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)]&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;sealed&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlDuration&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; : &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IXmlSerializable&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; When this instance is loaded from XML, this is &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; the original string.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; ValueString { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;private&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;set&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; The inner .Net TimeSpan value for this instance&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Value { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;private&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;set&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; XmlDuration() { }&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; XmlDuration(&lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; duration)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;Value &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; duration;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; XmlDuration(&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlDuration&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; duration)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;Value &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; duration&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Value;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;ValueString &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; duration&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ValueString;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; XmlDuration(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; duration)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#1aff36"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Value &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; TimeSpanFromDurationString(duration);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;ValueString &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; duration;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;catch&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ArgumentException&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; aex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;throw&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ArgumentException&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                &lt;span style="background:#000000;color:#1affff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;Invalid duration (see inner exception&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;duration&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;aex);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;implicit&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;operator&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlDuration&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; source)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlDuration&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(source);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;implicit&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;operator&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlDuration&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; source)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; source&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Value;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;private&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Regex&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; RxRead &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Regex&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#fedc76"&gt;@&amp;quot;^(?&amp;lt;ISNEGATIVE&amp;gt;-)?&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;+&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#fedc76"&gt;@&amp;quot;P((?&amp;lt;YEARS&amp;gt;[0-9]+)Y)?((?&amp;lt;MONTHS&amp;gt;([0-9])+)M)?((?&amp;lt;DAYS&amp;gt;([0-9])+)D)?&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;+&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#fedc76"&gt;@&amp;quot;(T((?&amp;lt;HOURS&amp;gt;([0-9])+)H)?((?&amp;lt;MINUTES&amp;gt;([0-9])+)M)?((?&amp;lt;SECONDS&amp;gt;([0-9]&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;+&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#fedc76"&gt;@&amp;quot;)+(&amp;#92;.[0-9]{1,3})?)S)?)?$&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;RegexOptions&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Compiled);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; Constructs a new TimeSpan instance from the pass XML &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; duration string (see summary on this type for a link &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; that describes the format).&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; Note that if the input string is not a valid XML &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; duration, an argument exception will occur.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;param name=&lt;/span&gt;&lt;span style="background:#000000;color:#73a7e6"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; TimeSpanFromDurationString(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; value)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; toReturn &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;MinValue;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;var&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; match &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; RxRead&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Match(value);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;match&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ThrowIf(m &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; m&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Success &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;false&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;The string {0} is not a valid XML duration&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;FormatWith(value));&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;bool&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; isNegative &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;false&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; years &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, months &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, days &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, hours &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, minutes &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;double&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; seconds &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;var&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; group &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; match&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Groups[&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;ISNEGATIVE&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;];&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;isNegative &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Success;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;group &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; match&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Groups[&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;YEARS&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;];&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Success)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;years &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Parse(group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Value);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;group &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; match&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Groups[&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;MONTHS&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;];&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Success)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;months &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Parse(group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Value);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;group &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; match&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Groups[&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;DAYS&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;];&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Success)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;days &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Parse(group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Value);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;group &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; match&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Groups[&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;HOURS&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;];&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Success)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;hours &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Parse(group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Value);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;group &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; match&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Groups[&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;MINUTES&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;];&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Success)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;minutes &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Parse(group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Value);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;group &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; match&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Groups[&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;SECONDS&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;];&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Success)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;seconds &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;double&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Parse(group&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Value);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//now have to split the seconds into whole and fractional.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//note - there is clearly a potential for a loss of fidelity&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//here given that we&amp;#39;re expanding years and months to 365 and&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//30 days respectively. There&amp;#39;s no perfect solution - although&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//you can simply ask your web service clients to express all&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//durations in terms of days, hours, minutes and seconds.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; wholeSeconds &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)seconds;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;seconds &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;-=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; wholeSeconds;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;toReturn &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;((years &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;*&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;365&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;) &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;+&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (months &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;*&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;30&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;) &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;+&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; days,&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;hours, minutes, wholeSeconds, (&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)(seconds &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;*&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;1000&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;));&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (isNegative)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;toReturn &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; toReturn&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Negate();&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; toReturn;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ff5555"&gt;#region&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; IXmlSerializable Members&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; Returns a qualified name of &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; http://www.w3.org/2001/XMLSchema:duration&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#038bb8"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;param name=&lt;/span&gt;&lt;span style="background:#000000;color:#73a7e6"&gt;&amp;quot;xs&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;///&lt;/span&gt;&lt;span style="background:#000000;color:#038bb8"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#5c72d6"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#5c72d6"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlQualifiedName&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; GetTypeSchema(&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlSchemaSet&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; xs)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlQualifiedName&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#1affff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;duration&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; System&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Xml&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Schema&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlSchema&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; GetSchema()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//see the static GetTypeSchema method.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; ReadXml(System&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Xml&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlReader&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; reader)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; s &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; reader&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ReadElementContentAsString();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (s&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;IsNotWhitespaceOrNull())&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;Value &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; TimeSpanFromDurationString(s);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;ValueString &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; s;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#1aff36"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Value &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;MinValue;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; WriteXml(System&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Xml&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlWriter&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; writer)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;StringBuilder&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; sb &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;StringBuilder&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//if we have the original duration string then we write that back out.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (ValueString&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;IsNotWhitespaceOrNull())&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;writer&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;WriteValue(ValueString);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#1aff36"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (Value&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Ticks &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;sb&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Append(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;#39;-&amp;#39;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;bool&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; isFractionalSeconds &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;((&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;double&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)((&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)Value&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;TotalSeconds)) &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;!=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Value&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;TotalSeconds;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;sb&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;AppendFormat(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;P{0}D&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, (&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)Value&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;TotalDays);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;sb&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;AppendFormat(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;T{0}H&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, Value&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Hours);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;sb&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;AppendFormat(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;{0}M&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, Value&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Minutes);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;sb&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;AppendFormat(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;{0}S&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;isFractionalSeconds &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;?&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;{0}.{1}&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;FormatWith(Value&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Seconds, Value&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Milliseconds) &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;: Value&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Seconds&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ToString());&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;writer&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;WriteValue(sb&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ToString());&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ff5555"&gt;#endregion&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ff5555"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;I’ve taken this class and merged it into the System.Xml namespace – because clearly this will also work with the XmlSerializer as well as for the DataContractSerializer.&lt;/p&gt; &lt;h3&gt;A few notes.&lt;/h3&gt; &lt;p&gt;The nifty part of this class is in the use of the &lt;a title="XmlSchemaProviderAttribute on MSDN" href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlschemaproviderattribute.aspx" target="_blank"&gt;XmlSchemaProviderAttribute&lt;/a&gt;.&amp;nbsp; This is the .Net framework’s preferred mechanism for mapping types to Xml schema.&amp;nbsp; In theory, if you were writing a more complex custom type for which Schema simply cannot be auto-generated, you could manually inject the schema into the XmlSchemaSet passed into the GetTypeSchema method (the name of which is determined by the parameter you pass to the attribute constructor).&amp;nbsp; You would then return the XmlQualifiedName of this schema type to satisfy the framework.&lt;/p&gt; &lt;p&gt;In this case all we have to do is to return the well-known qualified name of the xml duration type: ‘duration’ from the namespace ‘http://www.w3.org/2001/XmlSchema’.&amp;nbsp; We should be able to rely on anyone working with XML to have mapped this namespace already, and we know that a schema exporter will be doing the same since most of the .Net fundamental types are mapped to the same namespace.&lt;/p&gt; &lt;p&gt;Under the hood I’ve written a simple regex parser based on the format for the Duration data type.&amp;nbsp; It will recognise all valid strings, but it also lets one or two invalid ones through (notably ‘PT’).&amp;nbsp; However, if you are also using schema validation then this will not trouble you.&lt;/p&gt; &lt;p&gt;As the comments state - years and months are a problem; since the .Net TimeSpan chickens out and doesn’t encode years/months (presumably because it makes it much easier to calculate them from the difference of two DateTimes, as well as to add one onto a DateTime).&amp;nbsp; Of course neither have a fixed number of days; so I’ve gone for a reasonable average.&amp;nbsp; You could be more clever and take the current month’s number of days plus a strict average of 365.25 days per year – it depends on how accurate you really need it to be.&lt;/p&gt; &lt;p&gt;If you’re writing a new web service, you can simply make sure that all your clients express durations starting with the number of days – e.g. ‘P400D’ which will be deserialized exactly into a .Net TimeSpan representing 400 days.&lt;/p&gt; &lt;p&gt;In situations where a duration is received from a client and might need to be sent back to them – the original input string is preserved (but it will be up to you to persist that server side).&lt;/p&gt; &lt;p&gt;So now you can change a DataContract class like this:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5bd7f830-e4f4-4713-9752-09ccd79199de" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Class1&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Duration { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;set&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;And change it over to this:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:81e04109-c6d2-4cb0-ab46-cdce7d40dc4c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Class1&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlDuration&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Duration { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;set&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;In this case these types aren’t annotated of course (whereas in my case all my exported types are).&lt;/p&gt; &lt;p&gt;If you’re publishing a data contract for a type that must also implement some internal interface that exposes a TimeSpan like this:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:393c0880-7cf0-46ce-b1d8-79311ea3785f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;interface&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IClassInternal&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Duration { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;set&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Then your best policy is to write the DataContract class as follows:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e38b7513-c44a-4c88-a733-ee33dec694ef" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Class1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; : &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IClassInternal&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;XmlDuration&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Duration { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;set&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ff5555"&gt;#region&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; IClassInternal Members&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#e7a972"&gt;TimeSpan&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IClassInternal&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Duration&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#1aff36"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//use implicit casting operator&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Duration;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#1aff36"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;value&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;!=&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;Duration &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;value&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//and here&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ff5555"&gt;#endregion&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ff5555"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Obviously, there is an argument here that the XmlDuration should, in fact, be a value type and not a class.&amp;nbsp; I’ll leave that up to you to decide.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-1119992528090447174?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/1119992528090447174/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/map-systemtimespan-to-xsduration-for.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/1119992528090447174'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/1119992528090447174'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/map-systemtimespan-to-xsduration-for.html' title='Map System.TimeSpan to xs:duration for the DataContractSerializer'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-6412855580079034485</id><published>2010-09-16T22:15:00.001+01:00</published><updated>2010-09-16T22:17:13.303+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Asp.Net MVC'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><title type='text'>MVC Bug: The virtual path '[path]' maps to another application, which is not allowed</title><content type='html'>&lt;p&gt;I’ve being doing a lot of work on Asp.Net MVC (now v2) over the past few months and, firstly, I have to say that it totally rocks.&lt;/p&gt; &lt;p&gt;I will not go into any real detail about MVC here – this post is about the method System.Web.Mvc.HtmlHelper.AntiForgerytoken and that it highlights a bug in an internal class TokenPersister that you’re potentially going to get if you ever use spaces in your virtual directory names.&amp;nbsp; Ironically, the class in question is marked with a comment that says it’s difficult to unit test because of the way it fakes Asp.Net requests – ironic that it should be such a class in which we find a bug!&amp;nbsp; A better argument for unit-testing you’re unlikely to find.&lt;/p&gt; &lt;h2&gt;Detail – Reproduce that bug&lt;/h2&gt; &lt;p&gt;Using MVC it’s very easy to reproduce.&amp;nbsp; I should start by declaring my system configuration:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Windows 2008 R2  &lt;li&gt;IIS 7.5 installed and configured  &lt;li&gt;Visual Studio 2008 with all service packs etc  &lt;li&gt;.Net 3.5 sp1 plus security patches &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;VS2010 and .Net 4 are also on this machine – and I would welcome anybody following the same steps to confirm that i:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Create a new Asp.Net MVC2 Web Application (from the template) called ‘Asp Net Bug 2008’  &lt;ul&gt; &lt;li&gt;Don’t bother with the unit tests project &lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Open the project web properties, and set the project to use the Local IIS Web Server, the location should automatically be set to http://localhost/Asp Net Bug 2008/ (you might need to add the trailing slash here) – &lt;strong&gt;&lt;em&gt;the spaces here are important so leave them in&lt;/em&gt;&lt;/strong&gt;.  &lt;ul&gt; &lt;li&gt;Create the Virtual Directory  &lt;li&gt;Don’t forget to save the project file afterwards (like I just did as I created this walkthrough!) &lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Open the view &lt;strong&gt;Views/Account/Register.aspx&lt;/strong&gt;  &lt;li&gt;Just after the line  &lt;div style="border-bottom: #000080 1px solid; border-left: #000080 1px solid; font-family: 'Courier New', courier, monospace; color: #000; font-size: 10pt; border-top: #000080 1px solid; border-right: #000080 1px solid"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="padding-bottom: 0px; margin: 0px; padding-left: 5px; padding-right: 0px; background: #000000; padding-top: 0px"&gt; &lt;li&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1aff36"&gt;using&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; (Html&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;BeginForm()) { &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt; &lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;Add the following:  &lt;div style="border-bottom: #000080 1px solid; border-left: #000080 1px solid; font-family: 'Courier New', courier, monospace; color: #000; font-size: 10pt; border-top: #000080 1px solid; border-right: #000080 1px solid"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="padding-bottom: 0px; margin: 0px; padding-left: 5px; padding-right: 0px; background: #000000; padding-top: 0px"&gt; &lt;li&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="background: #000000; color: #ffff00"&gt;=&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; Html&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;AntiForgeryToken() &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt; &lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt; &lt;li&gt;Compile and run  &lt;li&gt;Navigate to the [Log On] link at the top of the page  &lt;li&gt;Hit the ‘Register’ link &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;You will see this exception helper:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="mvcexception" border="0" alt="mvcexception" src="http://lh5.ggpht.com/_f-d_NdW7K_Q/TJKJWLkC6YI/AAAAAAAAACE/55xx_I4kwKs/mvcexception%5B5%5D.png?imgmax=800" width="644" height="261"&gt; &lt;/p&gt; &lt;p&gt;And then on the Asp.Net error page, you’ll see this stack trace:&lt;/p&gt;&lt;pre style="width: 640px; display: block; overflow: scroll"&gt;[ArgumentException: The virtual path '/Asp%20Net%20Bug%202008/Account/Register' maps to another application, which is not allowed.]&lt;br /&gt;   System.Web.CachedPathData.GetVirtualPathData(VirtualPath virtualPath, Boolean permitPathsOutsideApp) +11193138&lt;br /&gt;   System.Web.HttpContext.GetFilePathData() +61&lt;br /&gt;   System.Web.Configuration.HttpCapabilitiesBase.GetBrowserCapabilities(HttpRequest request) +124&lt;br /&gt;   System.Web.HttpRequest.get_Browser() +168&lt;br /&gt;   System.Web.UI.Page.SetIntrinsics(HttpContext context, Boolean allowAsync) +207&lt;br /&gt;   System.Web.UI.Page.ProcessRequest(HttpContext context) +232&lt;br /&gt;   System.Web.Mvc.TokenPersister.CreateFormatterGenerator() +459&lt;br /&gt;   System.Web.Mvc.FormatterGenerator..cctor() +10&lt;br /&gt;&lt;br /&gt;[TypeInitializationException: The type initializer for 'FormatterGenerator' threw an exception.]&lt;br /&gt;   System.Web.Mvc.AntiForgeryDataSerializer.get_Formatter() +38&lt;br /&gt;   System.Web.Mvc.AntiForgeryDataSerializer.Serialize(AntiForgeryData token) +181&lt;br /&gt;   System.Web.Mvc.HtmlHelper.GetAntiForgeryTokenAndSetCookie(String salt, String domain, String path) +405&lt;br /&gt;   System.Web.Mvc.HtmlHelper.AntiForgeryToken(String salt, String domain, String path) +13&lt;br /&gt;   System.Web.Mvc.HtmlHelper.AntiForgeryToken() +17&lt;br /&gt;   ASP.views_account_register_aspx.__RenderregisterContent(HtmlTextWriter __w, Control parameterContainer) in c:\Users\andras.zoltan\documents\visual studio 2008\projects\aspnetbug2008\aspnetbug2008\Views\Account\Register.aspx:17&lt;br /&gt;   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +115&lt;br /&gt;   ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Users\andras.zoltan\documents\visual studio 2008\projects\aspnetbug2008\aspnetbug2008\Views\Shared\Site.Master:26&lt;br /&gt;   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +115&lt;br /&gt;   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240&lt;br /&gt;   System.Web.UI.Page.Render(HtmlTextWriter writer) +38&lt;br /&gt;   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +89&lt;br /&gt;   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4240&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;A closer look at this, you’ll see that the method GetBrowserCapabilities, called from the accessor of the HttpRequest.Browser is the culprit.&amp;nbsp; Opening Reflector, we see that this method is attempting to load the site configuration (web.config presumably) – which is where GetVirtualPathData comes in.&amp;nbsp; When I was confronted with this issue, it was obvious that the check which determines if the current request is in the current application root was failing.&amp;nbsp; So the first thing I thought was to change our app root to be something more friendly.&lt;/p&gt;&lt;br /&gt;&lt;h2&gt;The Fix&lt;/h2&gt;&lt;br /&gt;&lt;p&gt;Take the spaces out of the virtual directory name; perhaps replacing with hyphens, or just zeroing them down completely.&amp;nbsp; It’ll now work perfectly.&lt;/p&gt;&lt;br /&gt;&lt;h2&gt;The Cause&lt;/h2&gt;&lt;br /&gt;&lt;p&gt;Well, of course I haven’t analysed down to the last IL opcode here, but the first thing I thought was whether or not this was an Asp.Net bug, since the last few items in the call stack are from there.&amp;nbsp; So you can try another test, if you simply add a standard WebForm to the project (making sure it’s hosted in a vdir again that has spaces in it), and put in the following:&lt;/p&gt;&lt;br /&gt;&lt;div style="border-bottom: #000080 1px solid; border-left: #000080 1px solid; font-family: 'Courier New', courier, monospace; color: #000; font-size: 10pt; border-top: #000080 1px solid; border-right: #000080 1px solid"&gt;&lt;br /&gt;&lt;div style="background: #fff; overflow: auto"&gt;&lt;br /&gt;&lt;ol style="padding-bottom: 0px; margin: 0px; padding-left: 5px; padding-right: 0px; background: #000000; padding-top: 0px"&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="background: #000000; color: #ffff00"&gt;=&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; (Request&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;Browser&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;!=&lt;/span&gt;&lt;span style="background: #000000; color: #1aff36"&gt;null&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;)&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;ToString() &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt; &lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Compile and run that, and you’ll correctly get ‘True’ output in the page.&amp;nbsp; So, when the current HttpRequest is produced from the normal Asp.Net pipeline, everything works correctly.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;So, let’s take another look at that stack trace – we have the call to &lt;/p&gt;&lt;pre&gt;System.Web.Mvc.TokenPersister.CreateFormatterGenerator&lt;/pre&gt;Which the yields a call to &lt;pre&gt;System.Web.UI.Page.ProcessRequest&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;But we’re already in that method further down, so why?&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The reason can be found if you open CreateFormatterGenerator, either by getting the source code from CodePlex or by opening reflector.&amp;nbsp; Let’s take a look at the method body (which I have ripped straight out of the published project from CodePlex):&lt;/p&gt;&lt;br /&gt;&lt;div style="border-bottom: #000080 1px solid; border-left: #000080 1px solid; font-family: 'Courier New', courier, monospace; color: #000; font-size: 10pt; border-top: #000080 1px solid; border-right: #000080 1px solid"&gt;&lt;br /&gt;&lt;div style="background: #fff; overflow: auto"&gt;&lt;br /&gt;&lt;ol style="padding-bottom: 0px; margin: 0px; padding-left: 5px; padding-right: 0px; background: #000000; padding-top: 0px"&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="background: #000000; color: #1aff36"&gt;public&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1aff36"&gt;static&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #2cbe80"&gt;Func&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: #000000; color: #d9537b"&gt;IStateFormatter&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; CreateFormatterGenerator()&lt;/span&gt; &lt;br /&gt;&lt;li style="background: #0c0c0c"&gt;&lt;span style="background: #000000; color: #ffffff"&gt;{&lt;/span&gt; &lt;br /&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #008800"&gt;// This code instantiates a page and tricks it into thinking &lt;/span&gt;&lt;br /&gt;&lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #008800"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #008800"&gt;// that it's servicing a postback scenario with encrypted &lt;/span&gt;&lt;br /&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #008800"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #008800"&gt;// ViewState, which is required to make the StateFormatter &lt;/span&gt;&lt;br /&gt;&lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #008800"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #008800"&gt;// properly decrypt data. Specifically, this code sets the&lt;/span&gt; &lt;br /&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #008800"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #008800"&gt;// internal Page.ContainsEncryptedViewState flag.&lt;/span&gt; &lt;br /&gt;&lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #008800"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;TextWriter&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; writer &lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;=&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;TextWriter&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;Null;&lt;/span&gt; &lt;br /&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;HttpResponse&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; response &lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;=&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1aff36"&gt;new&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;HttpResponse&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;(writer);&lt;/span&gt; &lt;br /&gt;&lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;HttpRequest&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; request &lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;=&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1aff36"&gt;new&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;HttpRequest&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;(&lt;/span&gt;&lt;span style="background: #000000; color: #fdbe02"&gt;"DummyFile.aspx"&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;,&lt;/span&gt; &lt;br /&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;HttpContext&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;Current&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;Request&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;Url&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;ToString(), &lt;/span&gt;&lt;span style="background: #000000; color: #fdbe02"&gt;"__EVENTTARGET=true&amp;amp;__VIEWSTATEENCRYPTED=true"&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;);&lt;/span&gt; &lt;br /&gt;&lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;HttpContext&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; context &lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;=&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1aff36"&gt;new&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;HttpContext&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;(request, response);&lt;/span&gt; &lt;br /&gt;&lt;li&gt;&amp;nbsp; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;Page&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; page &lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;=&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1aff36"&gt;new&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;Page&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;()&lt;/span&gt; &lt;br /&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;{&lt;/span&gt; &lt;br /&gt;&lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;EnableViewStateMac &lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;=&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1aff36"&gt;true&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;,&lt;/span&gt; &lt;br /&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;ViewStateEncryptionMode &lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;=&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #a7c62b"&gt;ViewStateEncryptionMode&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;Always&lt;/span&gt; &lt;br /&gt;&lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;};&lt;/span&gt; &lt;br /&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;page&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;ProcessRequest(context);&lt;/span&gt; &lt;br /&gt;&lt;li style="background: #0c0c0c"&gt;&amp;nbsp; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #000000; color: #ffffff"&gt;&lt;/span&gt;&lt;span style="background: #000000; color: #1aff36"&gt;return&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; () &lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1aff36"&gt;new&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt; &lt;/span&gt;&lt;span style="background: #000000; color: #1affff"&gt;TokenPersister&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;(page)&lt;/span&gt;&lt;span style="background: #000000; color: #fffa2f"&gt;.&lt;/span&gt;&lt;span style="background: #000000; color: #ffffff"&gt;StateFormatter;&lt;/span&gt; &lt;br /&gt;&lt;li style="background: #0c0c0c"&gt;&lt;span style="background: #000000; color: #ffffff"&gt;}&lt;/span&gt; &lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Well, the embedded comment there explains what’s going on – and it’s this fake request that’s causing the problem, clearly.&amp;nbsp; Whatever it is that Asp.Net does to make sure we don’t get this error on a standard WebForm is not being done properly – although I’m not sure what that could be.&amp;nbsp; My initial guess is that the %20 is being compared to the space in the path name, rather than being url-decoded first – and therefore a simple fix might be to change the call to HttpContext.Current.Request.Url.ToString() on the fourth code line above to pass in the url-decoded string.&amp;nbsp; If I was feeling ambitious, I’d hack into the project and pursue a fix.&amp;nbsp; However, my primary feeling is to report it to the Mvc team – &lt;a title="Bug reported on codeplex" href="http://aspnet.codeplex.com/workitem/6577" target="_blank"&gt;which I have done here&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-6412855580079034485?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/6412855580079034485/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/mvc-bug-virtual-path-maps-to-another.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/6412855580079034485'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/6412855580079034485'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/mvc-bug-virtual-path-maps-to-another.html' title='MVC Bug: The virtual path &amp;#39;[path]&amp;#39; maps to another application, which is not allowed'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_f-d_NdW7K_Q/TJKJWLkC6YI/AAAAAAAAACE/55xx_I4kwKs/s72-c/mvcexception%5B5%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-5718771871659521481</id><published>2010-09-16T22:14:00.001+01:00</published><updated>2010-09-16T22:14:11.188+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>Serializing to attributes in WCF with DataContractSerializer</title><content type='html'>&lt;p&gt;It’s a common problem – you want to return an object from a WCF service as XML, but you either want, or need, to deliver some or all of the property values as XML Attributes instead of XML Elements; but you can’t &lt;a href="http://stackoverflow.com/questions/591907/how-can-you-control-wcf-serialization-so-it-uses-attributes-instead-of-elements" target="_blank"&gt;because the DataContractSerializer doesn’t support attributes&lt;/a&gt; (you’re most likely to have seen this StackOverflow QA if you’ve done a web search).&amp;nbsp; Most likely you’ve then migrated all your WCF service code to using the XmlSerializer (with all the XmlElement/XmlAttribute/XmlType attributes et al) – and you’ve cursed loudly.&lt;/p&gt; &lt;p&gt;Well, I’m here to rescue you, because it &lt;em&gt;is&lt;/em&gt; possible – and the answer to the problem is actually inferred from the MSDN article entitled ‘&lt;a href="http://msdn.microsoft.com/en-us/library/ms731923.aspx" target="_blank"&gt;Types supported by the Data Contract Serializer&lt;/a&gt;’.&lt;/p&gt; &lt;p&gt;The example I’m going to give is purely for illustration purposes only.&amp;nbsp; I don’t have a lot of time, so work with me!&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Create a new Asp.Net WCF service application, you can use Cassini as your web server (probably easier – otherwise you might have to enable Asp.Net compatibility mode).  &lt;li&gt;Open the web.config and delete the &amp;lt;service&amp;gt; element that was created for the new service.  &lt;li&gt;The interface and implementation model for this example is overkill.&amp;nbsp; Move the [ServiceContract] and [OperationContract] declarations from the interface that was created for you new service to the class that was also created.&amp;nbsp; Delete the interface.  &lt;li&gt;Open the .svc markup file and add the following at the end: &lt;font color="#fb0006" face="Courier New"&gt;Factory="System.ServiceModel.Activation.WebServiceHostFactory"&lt;/font&gt; – this enables the zero-configuration WCF model for this service (we’re going to create a RESTful service).  &lt;li&gt;Paste the following class declarations into your svc codebehind: &lt;/li&gt;&lt;/ul&gt;&lt;pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IExampleData&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Description { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; ID { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ExampleData : IExampleData&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Description { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; ID { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ExampleDataAttributed : ExampleData, IXmlSerializable&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	#region IXmlSerializable Members&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; System.Xml.Schema.XmlSchema GetSchema()&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ReadXml(System.Xml.XmlReader reader)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;		&lt;span style="color: #008000"&gt;//implement if remote callers are going to pass your object in&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; WriteXml(System.Xml.XmlWriter writer)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;		writer.WriteAttributeString("&lt;span style="color: #8b0000"&gt;id&lt;/span&gt;", ID.ToString());&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;		writer.WriteAttributeString("&lt;span style="color: #8b0000"&gt;name&lt;/span&gt;", Name);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;		&lt;span style="color: #008000"&gt;//we'll keep the description as an element as it could be long.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;		writer.WriteElementString("&lt;span style="color: #8b0000"&gt;description&lt;/span&gt;", Description);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	#endregion&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Just to demonstrate the point, the class that will be part-serialized to attributes simply derives from one that will be serialized as normal.&lt;/p&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Now add the following two methods to your service class: &lt;/li&gt;&lt;/ul&gt;&lt;pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;[OperationContract]&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;[WebGet(UriTemplate = "&lt;span style="color: #8b0000"&gt;/test1&lt;/span&gt;")]&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ExampleData Test1()&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ExampleData() { ID = 1, &lt;br&gt;           Name = "&lt;span style="color: #8b0000"&gt;Element-centric&lt;/span&gt;", &lt;br&gt;           Description = &lt;br&gt;"&lt;span style="color: #8b0000"&gt;The contents of this item are entirely serialized to elements - as normal&lt;/span&gt;" };&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;[OperationContract]&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;[WebGet(UriTemplate = "&lt;span style="color: #8b0000"&gt;/test2&lt;/span&gt;")]&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ExampleDataAttributed Test2()&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ExampleData_Attributed() { ID = 2, &lt;br&gt;          Name = "&lt;span style="color: #8b0000"&gt;Mixed&lt;/span&gt;", &lt;br&gt;          Description = &lt;br&gt;"&lt;span style="color: #8b0000"&gt;Everything except this description will be serialized to attributes&lt;/span&gt;" };&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Cover, and bake for 40 minutes (that is – Build it).&lt;/p&gt;&lt;br /&gt;&lt;p&gt;If you left your service as Service1.svc, then run it and open up IE and browse to &lt;em&gt;http://localhost:[port of cassini]/test1&lt;/em&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The result should look something like this:&lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;JSLabs.ExampleData&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; &lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"http://schemas.datacontract.org/2004/07/ExampleNamespace"&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; &lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;i&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"http://www.w3.org/2001/XMLSchema-instance"&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Description&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;		The contents of this item are entirely serialized to elements - as normal&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Description&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;		1&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;		Element-centric&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;JSLabs.ExampleData&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Now browse to &lt;em&gt;http://localhost:[port of cassini]/test2&lt;/em&gt;&lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;JSLabs.ExampleDataAttributed&lt;/span&gt; &lt;span style="color: #ff0000"&gt;id&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"2"&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"Mixed"&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  &lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"http://schemas.datacontract.org/2004/07/JobServe.Labs.Web"&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;description&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Everything except this description will be &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	serialized to attributes&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;description&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;JSLabs.ExampleDataAttributed&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;It’s made a little less impressive by that nasty ‘orrible “xmlns=” attribute that the WCF data contract serializer automatically puts on the type – but, as you can see, the ‘ID’ and ‘Name’ properties have indeed been pushed out as attributes!&lt;/p&gt;&lt;br /&gt;&lt;p&gt;We could have made both methods return IExampleData and then used &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.knowntypeattribute.aspx" target="_blank"&gt;the KnownType attribute&lt;/a&gt; on that interface in order to get it to support either (according to what the code of the methods returned).&lt;/p&gt;&lt;br /&gt;&lt;p&gt;To support deserializing an object from the attributes, all you have to do is to implement the IXmlSerializable.ReadXml method.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Finally, as the aforementioned MSDN article says about the supported types – you should also be able to use XmlElement/XmlNode types as a way of representing XML directly – the DataContractSerializer, like in this case, take the short route and simply gets the Xml.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;This also shouldn’t affect JSON formatting if you’re dual-outputting objects for either XML or JSON clients.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-5718771871659521481?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/5718771871659521481/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/serializing-to-attributes-in-wcf-with.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/5718771871659521481'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/5718771871659521481'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/serializing-to-attributes-in-wcf-with.html' title='Serializing to attributes in WCF with DataContractSerializer'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-9008714922051307046</id><published>2010-09-16T22:09:00.001+01:00</published><updated>2010-09-16T22:09:39.028+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>Enabling Custom Username/Password Authentication WCF in IIS</title><content type='html'>&lt;p&gt;A couple of posts back I was bemoaning the &lt;a href="http://lordzoltan.blogspot.com/2010/09/wcf-transport-level-custom-username.html" target="_blank"&gt;inability to use custom username/password authentication&lt;/a&gt; for all endpoints (WsHttp, BasicHttp and WebHttp) for a WCF service hosted in IIS.&amp;nbsp; The problem arises when you get to the WebHttp binding, because you can’t use message-level authentication (since it’s just a JSON packet), therefore you have to use Transport-level authentication.&amp;nbsp; Then you get to the biggest issue - that using Transport-level means having to use Basic/Windows/Digest authentication and IIS always automatically intercepts those authentication headers and attempts to authenticate to the server’s default windows account store (typically the domain it’s on, or it’s own local user store).&lt;/p&gt; &lt;p&gt;In this case, even if you configure your Custom UserName Password validator, it’ll never fire.&lt;/p&gt; &lt;p&gt;My solution to do a full, roll-your-own solution using Message Inspectors etc.&amp;nbsp; This still wouldn’t have solved the JSON problem fully, but it did mean that I could use my own Http headers to transmit my secure credentials.&lt;/p&gt; &lt;p&gt;I had a thought, though – what if IIS’ authentication could be extended, so that Basic authentication headers could be used on a per-endpoint basis (meaning that I was still free to use message-level for SOAP and Ws-Http if need-be)?&lt;/p&gt; &lt;p&gt;My initial google results were poor, &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/5ee5b07c-ed45-4685-bdb5-976ad5021610/" target="_blank"&gt;and so I decided to use the MSDN Forums&lt;/a&gt; to get some ideas.&amp;nbsp; Needless to say – the answer that Marco Zhou has been exactly the thing I was looking.&amp;nbsp; All I did was to download the source for the excellent &lt;a href="http://www.codeplex.com/CustomBasicAuth" target="_blank"&gt;CustomBasicAuth&lt;/a&gt; project from CodePlex, and by following Dominick’s walkthrough was then able to extrude my own implementation that I could then plug into any WCF service.&lt;/p&gt; &lt;p&gt;The extra upshot of doing all this was that I can now use Role-based security on all my WCF methods, and the client just has to know how to send the credentials according to the type of endpoint they are using.&amp;nbsp; With the JSON endpoints I’m using the trick I posted earlier on &lt;a href="http://lordzoltan.blogspot.com/2010/09/adding-custom-http-headers-with.html" target="_blank"&gt;how to intercept web service calls from Asp.Net Ajax client code in order to inject extra headers&lt;/a&gt;, but with the SOAP endpoints (currently from Silverlight) I’m using message-level headers and a Message Inspector (TIP – if you follow the CustomBasicAuth walkthrough exactly, you’ll add an Authorization provider to your WCF service.&amp;nbsp; If you then implement a message inspector to extract authentication details from a SOAP message, you can set the current Principal in that code – but here’s the beauty of it – it happens &lt;em&gt;before&lt;/em&gt; WCF security kicks in, so the same Authorization provider will also work!).&lt;/p&gt; &lt;p&gt;So while it is a bit of a pain – once you’ve got the skeleton up and running, it’s actually pleasingly simple.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-9008714922051307046?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/9008714922051307046/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/enabling-custom-usernamepassword.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/9008714922051307046'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/9008714922051307046'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/enabling-custom-usernamepassword.html' title='Enabling Custom Username/Password Authentication WCF in IIS'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-2749644147153450887</id><published>2010-09-16T22:07:00.001+01:00</published><updated>2010-09-16T22:07:21.030+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>WCF URI Template Url-Encoded Character Issues</title><content type='html'>&lt;p&gt;I've been playing around with &lt;a href="http://msdn.microsoft.com/en-us/library/dd203052.aspx" target="_blank"&gt;RESTFul services in WCF following by the MSDN topic&lt;/a&gt;, and have found the guide to be both extremely helpful, and the overall support provided to be excellent.&amp;nbsp; If you've been writing WCF services for your cloud service offerings, or even just AJAX methods for web-page code, you should find the migration over to RESTful services relatively easy to manage. &lt;/p&gt; &lt;p&gt;It also increases the accessibility of your services a huge amount - very basic jQuery code can be used, for example, to bind to these services. &lt;/p&gt; &lt;p&gt;However, there are some issues surrounding the types of values for parameters that can be bound in either a path or query string segment - and it's all down to some url-encoded characters, and how they get interpreted (or rather not interpreted) before being serialised into method parameters. &lt;/p&gt; &lt;p&gt;The following example is taken from a Microsoft Connect bug that I have posted: &lt;/p&gt; &lt;p&gt;&lt;em&gt;Setup a project to host a WCF .svc and create a method with two parameters:&lt;/em&gt; &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;[WebGet(UriTemplate = "&lt;span style="color: #8b0000"&gt;{willfail}?willwork={willwork}&lt;/span&gt;")]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;[OperationContract]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Foo(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; willfail, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; willwork)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;Trace.WriteLine("&lt;span style="color: #8b0000"&gt;willfail: &lt;/span&gt;" + willfail);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;Trace.WriteLine("&lt;span style="color: #8b0000"&gt;willwork: &lt;/span&gt;" + willwork);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; toreturn = &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;	&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format("&lt;span style="color: #8b0000"&gt;willfail: {0}. willwork : {1}&lt;/span&gt;", &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;			willfail, willwork);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; toreturn;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;In my example, I set the service to be hosted in Asp.Net under IIS (optional, as it turns out) - and I'm using the zero-configuration WebServiceHostFactory factory in the .svc file. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Now, after building, start up a browser and browse to the url that the service is configured on - e.g http://localhost/[baseuriofservice]/[svcfile.svc]/c%23?willwork=c%23.&amp;nbsp; '%23' is Url-Encoding-speak for '#'. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;What you expect to see is "willfail: c#. willwork : c#". &lt;/p&gt;&lt;br /&gt;&lt;p&gt;What you actually see is "willfail: c. willwork: c#". &lt;/p&gt;&lt;br /&gt;&lt;p&gt;So, when url-encoded characters appear in path segments bound to parameters, they won't always be read correctly (incidentally '%20' is read correctly); but it appears that query-string ones will. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;As I mentioned, I posted an Connect bug under the title '&lt;a href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=413312" target="_blank"&gt;WCF UriTemplate Strips Some UrlEncoded characters from bound path segment&lt;/a&gt;' - and it turns out that this behaviour doesn't just affect WCF, or even Asp.Net (since the MS investigator also tried self-hosting the service and it still fails). &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Even better news, however, is that this bug has been marked as fixed - so hopefully in the next framework version, we can expect to be able to use path segments with the whole gamut of URL-encodings! &lt;/p&gt;&lt;br /&gt;&lt;p&gt;This is one in the eye for Ayende Rahien - &lt;a href="http://ayende.com/Blog/archive/2009/03/24/microsoft-connect-fail-yet-again.aspx" target="_blank"&gt;who blogged very recently about Connect failing all over the place&lt;/a&gt;. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-2749644147153450887?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/2749644147153450887/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/wcf-uri-template-url-encoded-character.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/2749644147153450887'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/2749644147153450887'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/wcf-uri-template-url-encoded-character.html' title='WCF URI Template Url-Encoded Character Issues'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-6830948302825246752</id><published>2010-09-16T22:06:00.001+01:00</published><updated>2010-09-16T22:06:02.402+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>Adding custom HTTP headers with Sys.Net.WebRequestManager</title><content type='html'>&lt;p&gt;Following from my &lt;a href="http://lordzoltan.blogspot.com/2010/09/wcf-transport-level-custom-username.html" target="_blank"&gt;last post&lt;/a&gt; bemoaning the lack of transport-level client crediential authentication when running inside IIS, I've started implementing my own encrypted session-transfer between clients and the server. &lt;/p&gt; &lt;p&gt;I want to to use Http Headers as a primary way of broadcasting session information, and also because it's difficult for a hacker to know exactly what value to add to the http headers if it's been encrypted left, right and centre.&amp;nbsp; I might extend this to using a cookie as well; but the advantage of relying on http headers is that it means clients other than web browsers will be able to access the services; so long as they can authenticate. &lt;/p&gt; &lt;p&gt;The primary focus of my proof of concepts at the moment are on the AJAX side, both Asp.Net Ajax and 'vanilla' Ajax (e.g. jQuery or direct XmlHttp) - since I know that in Silverlight, for example, I'll be using a different endpoint and can probably add the same authentication data to the message itself. &lt;/p&gt; &lt;p&gt;The scenario is this: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;A service issues encrypted authentication 'tokens' which a client then sends to other protected services when it makes a request.&amp;nbsp; These tokens are time limited and have to be refreshed by the client within a certain time period (possibility to make it request-limited as well)  &lt;li&gt;A protected service is called by Asp.Net AJAX through use of the auto-generated proxy code that you the ScriptManager control gets - via the /js or /jsdebug endpoint that is added to the webscript-enabled service. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Now, the issue with this is that the proxy code that Asp.Net AJAX is generating is so 'friendly' that the underlying Web requests are abstracted away from your method calls, giving you no direct way of adding any custom headers you might want to add when you make the call. &lt;/p&gt; &lt;p&gt;Never fear - the Sys.Net.WebRequestManager class (part of Microsoft's Asp.Net AJAX client library) provides a hook which gives you the opportunity to examine each and every web request it sends, and even modify it's contents before it leaves the browser.&amp;nbsp; In fact, setting up the handler is so simple, I'm almost embarrassed to have it sitting at the bottom of such a long preamble... &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #008000"&gt;//method which captures requests and adds headers etc&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; InterceptOutgoingRequest(sender, args)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  3: {&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  4:   &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; currentRequest = args.get_webRequest();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  5:   currentRequest.get_headers()["&lt;span style="color: #8b0000"&gt;MYCUSTOMHEADER&lt;/span&gt;"] = "&lt;span style="color: #8b0000"&gt;MYCUSTOMVALUE&lt;/span&gt;";&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  6: }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  7: &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  8: &lt;span style="color: #008000"&gt;//adding the callback to the WebRequestManager&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  9: Sys.Net.WebRequestManager.add_invokingRequest(InterceptOutgoingRequest);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;The 'sender' parameter will be an instance of the &lt;a href="http://msdn.microsoft.com/en-us/library/bb397435.aspx" target="_blank"&gt;Sys.Net.WebRequestManager&lt;/a&gt;, and the 'args' parameter will be an instance of &lt;a href="http://msdn.microsoft.com/en-us/library/bb310979.aspx" target="_blank"&gt;Sys.Net.WebRequest&lt;/a&gt;. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Now, every single Asp.Net Ajax call made by the WebRequestManager will call your custom code. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-6830948302825246752?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/6830948302825246752/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/adding-custom-http-headers-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/6830948302825246752'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/6830948302825246752'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/adding-custom-http-headers-with.html' title='Adding custom HTTP headers with Sys.Net.WebRequestManager'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-7734816107106384694</id><published>2010-09-16T22:04:00.001+01:00</published><updated>2010-09-16T22:11:33.085+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>WCF Transport-level Custom Username Authentication won't work in IIS</title><content type='html'>&lt;p&gt;&lt;em&gt;Update – I have found a way (with a little help from the community!) around the issues discussed in this post – please see &lt;a href="http://lordzoltan.blogspot.com/2010/09/enabling-custom-usernamepassword.html" target="_blank"&gt;this&lt;/a&gt; post&lt;/em&gt;&lt;/p&gt; &lt;p&gt;I'm working on some WCF stuff at the moment whereby I want to expose business layer functionality to multiple clients (Javascript, Silverlight and WSHttp) and I want to have a security model that can be supported on all of them.&amp;nbsp; My authentication is totally custom, since we have all of our users in a custom database. &lt;/p&gt; &lt;p&gt;Logically I want to be able to use the WCF security model since it is very rich, and automatic once you've done the configuration.&amp;nbsp; However - it's a nightmare - because when you expose AJAX - friendly endpoints, you're doing it with WebHttpBinding, and it's security modes are, well, frankly crap; in fact, in almost every way WebHttpBinding should be thought of as a 'special case', which goes against everything that WCF seeks to achieve. &lt;/p&gt; &lt;p&gt;The problem is this - if you have a custom user name and password store, then you have to use either a membership provider (therefore relying on Asp.Net authorisation, and also in my case having to implement or stub out a whole host of features that our user store doesn't itself support), or you can write an entirely custom validator. &lt;/p&gt; &lt;p&gt;Once that's done your options - taken as a whole - are to use user name client credentials on either the transport or message security (using the 'clientCredentialType' attribute available on the &amp;lt;transport&amp;gt; and &amp;lt;message&amp;gt; security elements of a binding).&amp;nbsp; You also need to wire up the custom username validator - which is done at the service behaviour level: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;serviceBehaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  2:   &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;behavior&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"MyServiceBehavior"&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  3:   &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;serviceCredentials&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  4:     &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;userNameAuthentication&lt;/span&gt; &lt;span style="color: #ff0000"&gt;userNamePasswordValidationMode&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"Custom"&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  5:                             &lt;span style="color: #ff0000"&gt;customUserNamePasswordValidatorType&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"YourType, AssemblyName"&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  6:   &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;serviceCredentials&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  7:   &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;behavior&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;  8: &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;serviceBehaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;In a wsHttp environment you can do anything - so I'll leave that one out. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;In a SOAP environment, too, it's fine: You'd probably use Message-based security, so that you also have a way clear to enable transport-level security as well.&amp;nbsp; Not to mention being also able to lock down the transport to further credentials if need be. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;When, however, you hit the WebHttpBinding you also hit a massive brick wall.&amp;nbsp; You can only enable Transport security or TransportCredentialOnly security.&amp;nbsp; The first is just SSL, the second is no-SSL, but also gives space for the client credentials to be sent, using one of the Http authentication mechanisms, such as Basic, Digest etc (the same ones available on the IIS security tab). &lt;/p&gt;&lt;br /&gt;&lt;p&gt;This is where it gets really amusing - custom user name/password combos with Http Transport-based security do not work when hosted in IIS - because IIS steps in and authenticates against the windows user store (AD, local machine etc) automatically.&amp;nbsp; For a double-kick in the teeth, it's not supported at all when the service is not self-hosted: &lt;/p&gt;&lt;br /&gt;&lt;p&gt;For confirmation of this - see &lt;a href="http://blogs.msdn.com/phenning/archive/2008/01/11/custom-usernamepassword-validators-in-net-framework-3-5.aspx" target="_blank"&gt;this blog article&lt;/a&gt;, and &lt;a href="http://msdn.microsoft.com/en-gb/library/system.servicemodel.security.usernamepasswordservicecredential.usernamepasswordvalidationmode.aspx" target="_blank"&gt;this&lt;/a&gt; link on MSDN about the UserNamePasswordValidationMode property. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;I'm now having to circumvent WCF security and am rolling my own based on an IDispatchMessageInspector implementation, and custom authentication headers. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;The next post will be about how Asp.Net Ajax makes it super-easy to add extra headers to requests as they go out of the WebRequestManager class. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-7734816107106384694?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/7734816107106384694/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/wcf-transport-level-custom-username.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/7734816107106384694'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/7734816107106384694'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/wcf-transport-level-custom-username.html' title='WCF Transport-level Custom Username Authentication won&amp;#39;t work in IIS'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-3276348882747134589</id><published>2010-09-16T21:58:00.000+01:00</published><updated>2010-09-16T21:59:01.509+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='WPF'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><title type='text'>Using WPF to Render Bitmaps</title><content type='html'>&lt;p&gt;There are a few posts around the net about this, but a lot of them focus on taking XAML controls or pages, and turning parts of them into bitmaps. &lt;/p&gt; &lt;p&gt;This is fine, but I want to be able to use WPF &lt;em&gt;entirely off-screen&lt;/em&gt; in order to create rich graphical content. &lt;/p&gt; &lt;p&gt;What I'm going to present here is a way of doing just that, in a console application just to press home the point. &lt;/p&gt; &lt;p&gt;This is the image that you're going to produce - it's a little bit, well, crap - but it does the job. &lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="wpfinabitmap" border="0" alt="wpfinabitmap" src="http://lh4.ggpht.com/_f-d_NdW7K_Q/TJKFE0F--XI/AAAAAAAAACA/XU7lVJ5Y4g8/wpfinabitmap%5B8%5D.png?imgmax=800" width="400" height="300"&gt; &lt;/p&gt; &lt;p&gt;So, it's an &lt;font color="#0000ff" face="Courier New"&gt;Ellipse&lt;/font&gt; with a &lt;font color="#0000ff" face="Courier New"&gt;RadialGradientFill&lt;/font&gt; (off-centre, white-to-blue gradient stops), and a &lt;font color="#0000ff" face="Courier New"&gt;TextBlock&lt;/font&gt; in Arial Black/Bold font of around 40px size. &lt;/p&gt; &lt;p&gt;The edges around the ellipse are going to be output as transparent (open up this file in Photoshop or Paint.Net and notice the checkerboard patterns around the edge - useful for web images! &lt;/p&gt; &lt;h3&gt;Step 1: Create the project&lt;/h3&gt; &lt;p&gt;Open a new Visual Studio, and create a new Console Application Project.&amp;nbsp; Make sure it targets .Net 3.5. &lt;/p&gt; &lt;p&gt;Add references to: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;PresentationCore  &lt;li&gt;PresentationFramework  &lt;li&gt;WindowsBase &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;These are the core WPF libraries that are going to be required. &lt;/p&gt; &lt;p&gt;&lt;em&gt;Warning - if you're going to be repeating this code in a Windows Forms application, you should be careful - because many of the classes in WPF clash with those in the standard windows forms framework.&lt;/em&gt; &lt;/p&gt; &lt;h3&gt;Step 2: Basic setup&lt;/h3&gt; &lt;p&gt;Now, a quick google or MSDN search will show that the main class you're going to need is called &lt;font color="#0000ff" face="Courier New"&gt;RenderTargetBitmap&lt;/font&gt;.&amp;nbsp; This class has a method called &lt;font color="#0000ff" face="Courier New"&gt;Render&lt;/font&gt;, to which you pass a &lt;font color="#0000ff"&gt;Visual&lt;/font&gt;.&amp;nbsp; Anything that can be displayed in a WPF window is a visual (in fact the window itself is) which means, in theory, that anything you can do in WPF to screen can be done to a bitmap. &lt;/p&gt; &lt;p&gt;To produce the image file you're going to use one of the &lt;font color="#0000ff" face="Courier New"&gt;BitmapEncoder&lt;/font&gt;-implementing classes (there are classes for creating PNGs, Gifs, JPGs and more). &lt;/p&gt; &lt;p&gt;Open up Program.cs file and add the following &lt;font color="#0000ff" face="Courier New"&gt;using&lt;/font&gt;s: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Windows.Media.Imaging;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Windows.Media;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Windows.Controls;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Windows.Shapes;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Windows;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.IO;&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Modify the Main() method in Program.cs so it looks like this: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  1: [STAThread]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  2: &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  3: {&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  4: 	&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; height, width;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  5: &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  6: 	height = 300;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  7: 	width = 400;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  8: &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  9: 	&lt;span style="color: #008000"&gt;//going to place all our stuff in here - same as on a standard WPF form.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 10: 	Grid mainContainer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Grid();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 11: 	mainContainer.HorizontalAlignment = HorizontalAlignment.Stretch;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 12: 	mainContainer.VerticalAlignment = VerticalAlignment.Stretch;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 13: &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 14: 	Ellipse e = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Ellipse();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 15: 	e.Stroke = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SolidColorBrush(Color.FromArgb(255, 0, 0, 255));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 16: 	&lt;span style="color: #008000"&gt;//setup an off-centre gradient fill for a 3D effect&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 17: 	&lt;span style="color: #008000"&gt;//Of course - we could instead use a Viewport3D with Visual3D elements :)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 18: 	RadialGradientBrush rFill = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RadialGradientBrush();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 19: 	GradientStopCollection gradientStops = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; GradientStopCollection(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; GradientStop[] { &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 20: 		&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; GradientStop(Color.FromArgb(255, 255, 255, 255), 0.0),&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 21: 		&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; GradientStop(Color.FromArgb(255, 60, 90, 255), 1.0)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 22: 	});&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 23: 	rFill.GradientStops = gradientStops;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 24: 	rFill.GradientOrigin = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Point(0.65, 0.25);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 25: 	rFill.Center = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Point(0.5,0.5);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 26: 	rFill.RadiusX = 0.5;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 27: 	rFill.RadiusY = 0.5;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 28: 	e.Fill = rFill;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 29: &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 30: 	e.HorizontalAlignment = HorizontalAlignment.Stretch;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 31: 	e.VerticalAlignment = VerticalAlignment.Stretch;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 32: &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 33: 	mainContainer.Children.Add(e);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 34: &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 35: 	TextBlock message = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; TextBlock();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 36: 	message.Text = "&lt;span style="color: #8b0000"&gt;WPF In a Bitmap!&lt;/span&gt;";&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 37: 	message.HorizontalAlignment = HorizontalAlignment.Stretch;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 38: 	message.TextAlignment = TextAlignment.Center;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 39: 	message.VerticalAlignment = VerticalAlignment.Stretch;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 40: 	&lt;span style="color: #008000"&gt;//use margin for placement&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 41: 	&lt;span style="color: #008000"&gt;//could use canvas instead - but don't like the static 'Canvas.SetLeft/SetTop'&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 42: 	&lt;span style="color: #008000"&gt;//methods.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 43: 	message.FontFamily = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FontFamily("&lt;span style="color: #8b0000"&gt;Arial Black&lt;/span&gt;");&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 44:    	message.FontSize = 40.0;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 45: 	message.FontWeight = FontWeights.Bold;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 46: 	message.Margin = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Thickness(0.0, 120, 0.0, 0.0);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 47: 	mainContainer.Children.Add(message);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 48: &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 49: 	PngBitmapEncoder encoder = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PngBitmapEncoder();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 50: 	RenderTargetBitmap render = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RenderTargetBitmap(&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 51: 		width, &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 52: 		height, &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 53: 		96, &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 54: 		96, &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 55: 		PixelFormats.Pbgra32);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 56: 	render.Render(mainContainer);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 57: 	encoder.Frames.Add(BitmapFrame.Create(render));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 58: 	&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (Stream s = File.Open("&lt;span style="color: #8b0000"&gt;outputfile.png&lt;/span&gt;", FileMode.Create))&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 59: 	{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 60: 		encoder.Save(s);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 61: 	}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #ced2fd; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 62: }&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Notice a few things: &lt;/p&gt;&lt;br /&gt;&lt;p&gt;1) [STAThread] is applied to the Main method as WPF requires this.&amp;nbsp; If you omit it, an exception is raised telling you as much. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;2) Transparency is enabled in the output image by using a pixel format that includes an alpha channel.&amp;nbsp; Line 55 specifies 32-bit rgba - same as most people's desktop. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;3) Horizontal/Vertical Alignment being set to stretch in most places - has the same effect as it would on a XAML form. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Run the app and open up outputfile.png in the output folder. &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Oops...&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;What's that?&amp;nbsp; The image is empty? &lt;/p&gt;&lt;br /&gt;&lt;p&gt;What's happened is that, although the grid, ellipse and textblock are all present and, in theory, ready to be rendered, they haven't actually been told to lay themselves out according to their visual container.&amp;nbsp; Of course, they don't actually have one, since they are just objects in memory, so what do we do? &lt;/p&gt;&lt;br /&gt;&lt;p&gt;We have to take the place of the layout engine that is usually wired up automatically by a real WPF form; which it achieves through the use of all the events that WPF elements expose (i.e. for when controls get added or updated etc). &lt;/p&gt;&lt;br /&gt;&lt;p&gt;On line 48 (or at least between 47 and 49) you need to add this line of code: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;mainContainer.Arrange(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Rect(0, 0, width, height));&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Thankfully, this is recursive. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Save and run again, and this time the image should be as above. &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Animation&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;If you're going to try and create pre-rendered animations using WPF, there are a couple of gotchas - and one outright brick wall if animated gifs are your thing. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;The brick wall is that &lt;font color="#0000ff" face="Courier New"&gt;GifBitmapEncoder&lt;/font&gt; is not capable of producing correct animatable gifs.&amp;nbsp; While you can successively call the AddFrame method of it's Frames collection property, and create a single gif file containing all the individual frames, you can't (out of the box) add the necessary timing information and looping information that most browsers/image viewers require.&amp;nbsp; You'll get a non-looping default-speed animation in IE7, for example, but in other browsers (apparently IE8 is one of them) it just won't animate at all. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;It is possible to add this functionality into the Gif encoder - but it's not a job I want to take on (see &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6ef358a7-d1ac-4267-91d9-166024aad8ca/" target="_blank"&gt;this&lt;/a&gt; MSDN forum post and it's answer).&amp;nbsp; In the meantime, it's still possible to use .Net and WPF to generate the intermediate gif frames on disk, and then either manually stitch the animation together with timing in a dedicated tool. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;The gotchas for animating in this way are: &lt;/p&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Once a BitmapEncoder has saved a file, it cannot be used to save another; so you'll need to create a new encoder for each frame. &lt;br /&gt;&lt;li&gt;You can re-use the RenderBitmapTarget between each frame, but... &lt;br /&gt;&lt;li&gt;In the same way that we had to manually call the 'Arrange' method of the Grid (or whichever root container we're going to be using), we also have to manually Invalidate any controls whose visual state changes between frames.&amp;nbsp; You can achieve this, by sticking this line before the next call to the Render(Visual) method of the RenderBitmapTarget object: &lt;/li&gt;&lt;/ol&gt;&lt;pre&gt;&lt;pre style="background-color: #c0c7fe; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;mainContainer.InvalidateVisual();&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Now, in theory it might be possible to use the story-boarding animation features of WPF, but since they rely on animation timers (which will not be present since there's no actual WPF app or window here) I'm not sure how effective it'll be.&amp;nbsp; My guess is that you would manually visit the timeline, explicitly setting the current position from 0 -&amp;gt; 1, then invalidate anything that might be affected, and then do the Invalidate/Render code. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;If that were possible, then it would certainly make animating rotations etc a lot easier. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;In any case - the ability to render stills using WPFs frankly rather swish rasterisation layer is, in itself, a real boon if looking to create dynamic images on a web page, for example, or even for any other scenario.&amp;nbsp; Automatic anti-aliasing and transparency, for example, being two of the biggest boons. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-3276348882747134589?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/3276348882747134589/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/using-wpf-to-render-bitmaps.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/3276348882747134589'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/3276348882747134589'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/using-wpf-to-render-bitmaps.html' title='Using WPF to Render Bitmaps'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_f-d_NdW7K_Q/TJKFE0F--XI/AAAAAAAAACA/XU7lVJ5Y4g8/s72-c/wpfinabitmap%5B8%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-3518326679097546820</id><published>2010-09-16T21:51:00.001+01:00</published><updated>2010-09-16T21:51:58.848+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Voodoo'/><title type='text'>Passing ref parameters in a params object[] array</title><content type='html'>&lt;p&gt;Ever wanted to call a method that takes a ref &lt;em&gt;something &lt;/em&gt;in a generic (but not using generics!) way, say through a params object[] parameter array? &lt;/p&gt; &lt;p&gt;I did, and was most frustrated that you can't. &lt;/p&gt; &lt;h3&gt;The Scenario&lt;/h3&gt; &lt;p&gt;Firstly - the scenario I encountered is pretty specialist - I'm dynamically generating types which override/implement base/interface methods with methods provided by XML files.&amp;nbsp; The target methods of the implementations are static methods, and therefore cannot use the 'base' keyword when calling 'up' the virtual call chain.&amp;nbsp; As a result, I have to have a common delegate through which the next method can be called.&amp;nbsp; My solution to this is to have two standard delegates: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ActionWithParameters(&lt;span style="color: #0000ff"&gt;params&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[] parms)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; FuncWithParameters(&lt;span style="color: #0000ff"&gt;params&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[] parms)&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;I like the params object[] route because it then means I can provide a public layer of methods, which are generics, which allow the developer to be more expressive with the types that they are passing to the next method, e.g: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;//overload for two parameters&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CallNext&amp;lt;T1, T2&amp;gt;(T1 p1, T2 p2)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	ActionWithParameters del = [Some Method to Get Delegate];&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;//call it.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	del(p1, p2);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;//the great thing being - for no parameters, it's the same code:&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CallNext()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	ActionWithParameters del = [Some Method to Get Delegate];&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	del();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;However, this breaks down if one of the parameters is a 'ref'.&amp;nbsp; We can overload the generic method with one or more 'ref' versions for each of the parameters coming in (lots of copying and pasting!), but then we get stuck when trying to invoke the delegate, which expects a params object[] array (incidentally, the same is true when using the InvokeMethod of MethodInfo). &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Now, we can declare a delegate thus: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; FooDelegate(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; something);&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Then we can bind new instance of the delegate to a method with the same signature, and call it: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; MyFoo(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; input)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #a5afdc; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	input = "&lt;span style="color: #8b0000"&gt;Hello world!&lt;/span&gt;";&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #a5afdc; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #a5afdc; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; BindADelegateAndCallWith(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; input)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #a5afdc; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	FooDelegate foo = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FooDelegate(MyFoo);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	foo(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; input);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #a5afdc; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;So, obviously the inability to pass parameters by reference is not a limitation of the Delegate itself, it's a limitation of the means by which we can call it when not using it directly as a function (note - in the above example, the C# compiler redirects the 'foo(ref input)' call to 'foo.Invoke(null, ref input)' - calling a method which the compiler is auto-generating - open up Reflector and switch to IL mode on the 'FooDelegate' type). &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Why?&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;Let's look at why we can't pass these reference parameters into a params object[] array - if you already know why, then just skip this bit! &lt;/p&gt;&lt;br /&gt;&lt;p&gt;All types in the .Net framework inherit from System.Object, right?&amp;nbsp; Wrong.&amp;nbsp; Typed references created using the C# keywords ref and out (i.e. 'System.String&amp;amp;' or 'System.Int32&amp;amp;') do not.&amp;nbsp; In fact, they inherit from nothing, nor do they have any public/private members that can be discovered through reflection.&amp;nbsp; Nevertheless, they are still discreet types in their own right.&amp;nbsp; Indeed, to construct the 'Type' for a typed reference, would require something like the following code: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;Type refstring = &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;).MakeByRefType()&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Similarly, to get from the reference type back to the type to which it references you use the method: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;//assuming that we still have the type from&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #a5afdc; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;//the previous example&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;Type nonref = refstring.GetElementType();&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;So, 'typed reference' types are not really part of the class hierarchy of a type and it's subtypes - but special types that exist in parallel to each class in that hierarchy - but which are only connected back to their non-reference bigger brothers.&amp;nbsp; The same is also true for pointer types.&amp;nbsp; So, for example, 'ref string' does not inherit from 'ref object' which is also the reason why, given a method like this: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PassObjectByRef(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; obj)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #a5afdc; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #a5afdc; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;You cannot call it with a line of code like this: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; mystring = "&lt;span style="color: #8b0000"&gt;hello world!&lt;/span&gt;";&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #a5afdc; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;PassObjectByRef(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; mystring);&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Again, the compiler will say something like "No overload which takes an argument 'ref string'", in addition to the other error "Cannot convert parameter 1 from 'ref string' to 'ref object'".&amp;nbsp; The number of times I've been frustrated by that I can't count - you can of course sidestep this problem by having a temporary reference to the base class of the same object (i.e. declare an 'object' local variable, assign it to the local string, and then pass the object by reference instead). &lt;/p&gt;&lt;br /&gt;&lt;p&gt;So this explains why you can't pass a 'ref string' to a method whose signature is 'params object[]' - that notation indicates that the caller can pass zero or more parameters that have System.Object as a base-class and, as we've just seen, &lt;em&gt;Type&lt;/em&gt;&amp;amp; does not. &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;The Solution&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;Now, the technique I'm about to show here is probably not the safest in the world - however so long as your code is tightly controlled it should be fine.&amp;nbsp; It does require you to have an assembly somewhere which at least has the attribute: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification=&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;And if you're going to use this code, I'd suggest you place this attribute on the assembly in which you paste the code, because when it creates the DynamicMethods it associates them with the parent module of the type itself. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;What we want to achieve is to be able to convert our typed reference parameter into a non-typed reference parameter on the caller side, push it through a params object[] function, convert that value back into a typed reference again, before passing it to the actual method - or even modifying the target reference indirectly through that value. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;The code exploits the fact that a .Net typed reference is just a platform pointer under the hood and is, therefore, a value that can be stored in a System.IntPtr structure.&amp;nbsp; It also exploits the fact that there are some things that we can do in IL that cannot be done in C# - like pushing typed reference's value on to the stack, and simply returning it without dereferencing it back to an object reference or value(the C# compiler automatically dereferences 'ref' parameters whenever they appear on the left or right hand side of an expression- either using &lt;strong&gt;OpCodes.LdInd_Ref&lt;/strong&gt; or &lt;strong&gt;OpCodes.StInd_Ref, &lt;/strong&gt;or similar, use Reflector or IL DASM to see what I mean). &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Anyway, here's the magic generic class: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;// Static class uses dynamic methods to do all the funky stuff, and&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;// because these are static they only get generated once for each type&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;// passed into the Type parameter T.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;// Means that the first call will be a little slow, but the subsequent &lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;// calls should be lightning fast.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; RefParam&amp;lt;T&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;/*************************************************************/&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// The delegate types - these have to be declared using the&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// 'proper' delegate-keyword mechanism because only that&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// supports ref parameter types.  Would be nice to use&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// Func&amp;lt;T, IntPtr&amp;gt;, but we can't&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// Delegate which returns the actual object reference caused &lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// by passing the input parameter by reference.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; IntPtr MakeDelegate(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; T input);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// Delegate which can be used to dereference an object &lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// reference and get the value currently stored in it.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; T GetValueDelegate(IntPtr objref);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// Delegate which can be used to modify an object reference&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SetValueDelegate(IntPtr objref, T newvalue);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;/*************************************************************/&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// this is the method that we actually call to turn a ref T&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// into an IntPtr.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; MakeDelegate _Make;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;// this method &lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; GetValueDelegate _GetValue;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; SetValueDelegate _SetValue;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; RefParam()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		CompileDelegates();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CompileDelegates()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		&lt;span style="color: #008000"&gt;//the dynamic method is targetted at the module that this&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		&lt;span style="color: #008000"&gt;//class is placed - that assembly needs to have the &lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		&lt;span style="color: #008000"&gt;//SkipVerification security permission.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		DynamicMethod dm = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DynamicMethod(&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format("&lt;span style="color: #8b0000"&gt;MakeRef_{0}&lt;/span&gt;", &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T).TypeHandle.Value), &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IntPtr), &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Type[] { &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T).MakeByRefType() }, &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(RefParam&amp;lt;T&amp;gt;).Module);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		&lt;span style="color: #008000"&gt;//method simply loads the reference value and returns it as&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		&lt;span style="color: #008000"&gt;//an IntPtr - if you were to disassemble any other method that&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		&lt;span style="color: #008000"&gt;//sucks in a ref parameter and returned it, you'd see a couple of&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		&lt;span style="color: #008000"&gt;//other opcodes - to do with dereferencing the reference - an &lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		&lt;span style="color: #008000"&gt;//operation that cannot be performed in straight C#&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		ILGenerator gen = dm.GetILGenerator();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen.Emit(OpCodes.Ldarg_0);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen.Emit(OpCodes.Ret);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		_Make = (MakeDelegate)dm.CreateDelegate(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(MakeDelegate));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		dm = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DynamicMethod(&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format("&lt;span style="color: #8b0000"&gt;GetValue_{0}&lt;/span&gt;", &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T).TypeHandle.Value), &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T), &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Type[] { &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IntPtr) }, &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(RefParam&amp;lt;T&amp;gt;).Module);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen = dm.GetILGenerator();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen.Emit(OpCodes.Ldarg_0);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen.Emit(OpCodes.Ldind_Ref);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen.Emit(OpCodes.Ret);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		_GetValue = (GetValueDelegate)dm.CreateDelegate(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(GetValueDelegate));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		dm = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DynamicMethod(&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format("&lt;span style="color: #8b0000"&gt;SetByRef_{0}&lt;/span&gt;", &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T).TypeHandle.Value), &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;), &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Type[] { &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IntPtr), &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T) }, &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;			&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(RefParam&amp;lt;T&amp;gt;).Module);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen = dm.GetILGenerator();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen.Emit(OpCodes.Ldarg_0);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen.Emit(OpCodes.Ldarg_1);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen.Emit(OpCodes.Stind_Ref);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		gen.Emit(OpCodes.Ret);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		_SetValue = (SetValueDelegate)dm.CreateDelegate(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(SetValueDelegate));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SetValue(IntPtr reference, T newvalue)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		_SetValue(reference, newvalue);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; T GetValue(IntPtr reference)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _GetValue(reference);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; IntPtr Make(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; T &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _Make(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Some people might find bits of this code scary - from the point of view of safety (indeed, unless you associate the dynamic methods with a module within an assembly that has the aforementioned 'SkipVerification' permission enabled, these dynamic methods simply do not compile). &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Now that we've got our static class, we can look at using it: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;//copy this code into a standard unit test&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;[TestMethod]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; TestRefPassing()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;//hello world!&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; input = "&lt;span style="color: #8b0000"&gt;&lt;/span&gt;";&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	TakesAParamsObject(RefParam&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;.Make(input));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	Assert.AreEqual("&lt;span style="color: #8b0000"&gt;Hello world!&lt;/span&gt;", input);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;//expects a single parameter to be passed of type IntPtr&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;//that can be converted back into a String&amp;amp;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; TakesAParamsObject(&lt;span style="color: #0000ff"&gt;params&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[] parms)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	RefParam&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;.SetValue((IntPtr)parms[0], "&lt;span style="color: #8b0000"&gt;Hello world!&lt;/span&gt;");&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Run the test - it should pass. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;So what if your params object[] method needs to take a parameter that is known to be a typed reference, and pass it to another method that expects a 'ref T'?&amp;nbsp; This is a problem I've encountered in my project: even though I'm emitting the IL that wires this params object[] method to a 'proper' method with 'proper' parameters, I've not been able to produce IL that can safely turn an IntPtr back into a ref T without the runtime going monkey-poo (that's 'ape-shit') on me.&amp;nbsp; I'm sure it can be done, but until I can figure it out, the simplest solution is to declare a local, copy the value of the object referenced by the IntPtr, pass the local by reference to the target method, and then write the local value back to the IntPtr reference afterwards.&amp;nbsp; You can write C# to do the same - here's a slight modification of the above code: &lt;/p&gt;&lt;pre&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;[TestMethod]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; TestRefPassing2()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; input = "&lt;span style="color: #8b0000"&gt;&lt;/span&gt;";&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	TakesAParamsObjectAndForwardsItOn(RefParam&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;.Make(input));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	Assert.AreEqual("&lt;span style="color: #8b0000"&gt;Hello universe!&lt;/span&gt;", input);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;//same again, except this time the modification is being done in a method that&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;//actually expects a ref string, and the params object[] method simply cracks it&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #008000"&gt;//out.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; TakesAParamsObjectAndForwardsItOn(&lt;span style="color: #0000ff"&gt;params&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[] parms)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;//declare a string local, and use the 'GetValue' method on the &lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;//RefParam&amp;lt;T&amp;gt; static type to retrieve the reference to copy into it.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; topass = RefParam&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;.GetValue((IntPtr)parms[0]);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;//now pass the local string by reference&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	TakesARefString(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; topass);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	&lt;span style="color: #008000"&gt;//copy the reference back&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	RefParam&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;.SetValue((IntPtr)parms[0], topass);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; TakesARefString(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; input)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9fb; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;	input = "&lt;span style="color: #8b0000"&gt;Hello universe!&lt;/span&gt;";&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="background-color: #cacaff; margin: 0em; width: 100%; font-family: monospace; font-size: 11px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Now then, anybody out there who is also dynamically generating code to bind to methods like the 'TakesARefString' method through a similar params object[] method should be able to see how easy it would be to write the IL body for the middle method, given the target method's MethodInfo (being able to reflect the parameters and get their types). &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Incidentally, I'm also using the same strategy for Value types passed by reference, and it all works fine. &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;What about garbage collection?&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;I'm &lt;em&gt;pretty&lt;/em&gt; sure that, whilst this code might not be &lt;em&gt;type&lt;/em&gt;-safe, it isn't '&lt;em&gt;unsafe&lt;/em&gt;' in the same way as using pointers.&amp;nbsp; A typed reference in .Net incorporates information about an object within the managed heap (unlike a pointer which goes directly to the actual unmanaged heap) - so if the underlying storage is moved, the typed reference is still valid.&amp;nbsp; If this wasn't the case, then any code which uses 'ref T' would also have to be unsafe, which it plainly does not.&amp;nbsp; All we've done is to take the underlying data for the typed reference - which is basically just a smart pointer from what I can gather - and get it's actual value.&amp;nbsp; There's no way that .Net writes back to these typed reference values if objects get moved about (far too much overhead, .Net would be a lot slower than it is), so we must consider the underlying value as sacrosanct. &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Playing with fire might get you burned&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;Using this code is not without risks!&amp;nbsp; I'm not going to bother with a disclaimer on this code; I'd be here forever.&amp;nbsp; A few points of note, though: &lt;/p&gt;&lt;br /&gt;&lt;p&gt;We can now take that reference and pass it around outside of the call stack of the method that originally declared that reference - which the standard 'ref' mechanism prevents us from doing.&amp;nbsp; Effectively, this can mean holding on to a reference to something that has since gone out of scope - usually a problem that you don't have to think about in .Net.&amp;nbsp; Any attempt to do this (and then dereference the typed reference using the RefParam&amp;lt;T&amp;gt; class) is likely to end up with an ExecutionEngineException or something similar - and to get an idea of just how potentially dangerous that can be for the health of your application - consider the opening sentence for the remarks of the class from the documentation: &lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;em&gt;"Execution engine errors are fatal errors that should never occur"&lt;/em&gt; &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Nuff said, really.&amp;nbsp; When one of these occurs, you can't even 'catch' them they're that bad. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Also - DO NOT take an IntPtr reference made with, say, RefParam&amp;lt;int&amp;gt; and attempt to read or write it through RefParam&amp;lt;string&amp;gt; - that's really bad medicine.&amp;nbsp; And don't even try to write to base types through a reference to the derived type - which as explained earlier the standard mechanism prevents you from doing.&amp;nbsp; That said - I've not tried it... so it might work ;) &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Beyond that, so long as you are diligent and ensure that use of this class is only used in place of typed references into method calls, you should not get any problems whatsoever. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Stopping the caller from passing in a reference type that you don't expect is another matter - however this can be achieved instead by wrapping the IntPtr in another type that encodes the original type of reference (something that the data referenced by the value of the typed reference encodes itself, but is unfortunately not something that you can get hold of), and then having your code check that before dereferencing it - throwing a much more friendly exception if it's the wrong one. &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;And finally...&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;If anybody's got any ideas how to inline the conversion from IntPtr back to 'ref T' for the purposes of passing to a 'proper' method, rather than having to use an intermediate variable, I'd love to hear from you - I have a feeling that I've missed something glaringly obvious. &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Update (29th October 2008)&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;On this final point - I have solved the problem.&amp;nbsp; This code is only going to be of interest if you are also code-generating a params object[] method which in turn calls a method with 'real' parameters - of which some could be ref params. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Here's a snippet of the IL I was using originally to load the original ref parameter (that was turned into an IntPtr using the RefParam&amp;lt;T&amp;gt; class) and store it in a local variable which then gets passed to the target method by reference &lt;em&gt;in place &lt;/em&gt;of the reference that was passed (assuming that arg.0 is the params object[] array, and the IntPtr for the reference is the first item in it).&amp;nbsp; I have not included the IL which synchronises the value back to the passed reference (using RefParam&amp;lt;T&amp;gt;::SetValue): &lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;.locals init( &lt;br&gt;&amp;nbsp;&amp;nbsp; [0] &lt;em&gt;paramelementtype&lt;/em&gt; param) &lt;br&gt;ldarg.0 &lt;br&gt;ldc.i4.0 &lt;br&gt;ldelem.ref &lt;br&gt;unbox native int &lt;br&gt;ldobj native int &lt;br&gt;call RefParam&amp;lt;&lt;em&gt;paramelementtype&lt;/em&gt;&amp;gt;::GetValue(native int) //abbreviated the method name here for ease of reading &lt;br&gt;stloc.0 //storing the value in the local variable &lt;br&gt;ldloca.s param &lt;br&gt;//now call the target method&lt;/font&gt; &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Now, when I originally tried to avoid using a local variable, I was experimenting with the refanytype and refanyval opcodes - thinking they would be of help.&amp;nbsp; But no, they weren't, and so I just abandoned the idea in favour of that one above. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;However, this morning I decided to take a look at it again - feeling sure that I could inline the conversion from IntPtr back to the &lt;em&gt;Type&amp;amp;&lt;/em&gt;.&amp;nbsp; My reasoning being that my GetValue method in RefParam&amp;lt;T&amp;gt; relies on this ability in order to work.&amp;nbsp; Embarrassingly enough, the solution was simple - do away with all the extra code after the 'ldobj': &lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;font face="Courier New"&gt;ldarg.0 &lt;br&gt;ldc.i4.0 &lt;br&gt;ldelem.ref &lt;br&gt;unbox native int &lt;br&gt;ldobj native int&lt;/font&gt; &lt;br&gt;&lt;font face="Courier New"&gt;//now call the target method&lt;/font&gt; &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Why?&amp;nbsp; Because after the ldobj instruction the 4/8 bytes of the managed pointer are now on the stack, and it is exactly that which the function call expects. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-3518326679097546820?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/3518326679097546820/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/passing-ref-parameters-in-params-object.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/3518326679097546820'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/3518326679097546820'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/passing-ref-parameters-in-params-object.html' title='Passing ref parameters in a params object[] array'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-82558953649602591</id><published>2010-09-14T00:15:00.001+01:00</published><updated>2010-09-14T00:15:24.399+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><title type='text'>Generic overloads and ref parameter hell (the return of the Stack overflow)</title><content type='html'>&lt;p&gt;In a framework that I have written for my dev team, I have a class that has a method like so: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:94866838-dd11-4d29-ae0a-2cf2029807c8" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Clone(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;ref&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectBase&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; target, &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectBase&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; parent);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;This method (virtual in my library, in fact) has two functions:&amp;nbsp; When the target ref is null, then a new object should be created, but when it's not, then the contents of the object on which it's being called should be cloned into the passed object.&amp;nbsp; The parent of the new object is required because in the framework our objects have keys that can come from multiple sources - including an expression on the parent. &lt;/p&gt; &lt;p&gt;Anyway, whilst this method is particularly useful, it's a little unfriendly to use from your code when you don't have a direct ObjectBase reference to pass, but instead a reference to an object derived from it: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8d799072-0237-445e-b3ed-69cd426705c1" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyObject&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; : &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; main()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyObject&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; obj &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyObject&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyObject&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; clone &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;obj&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Clone(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;ref&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; clone, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;font face="courier new,courier"&gt;&lt;/font&gt; &lt;p&gt;That red line indicates where the compiler blows - when the parameter type is 'ref &lt;em&gt;T&lt;/em&gt;', you can't pass a reference to '&lt;em&gt;TDerived : T&lt;/em&gt;', because although the two types are compatible, the method expects a 'ref T', not anything else, and at that level 'ref T' and 'ref TDerived' are two incompatible types. &lt;/p&gt; &lt;p&gt;Not a problem, we can of course get around this: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:74b45c4b-97f1-4537-b286-bba98693a756" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; main()&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyObject&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; obj &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyObject&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyObject&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; clone &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectBase&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; clonebase &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;obj&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Clone(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;ref&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; clonebase, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;clone &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; clonebase &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;as&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyObject&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;I don't need to say it, but I will anyway: &lt;font size="5"&gt;URGH!&lt;/font&gt; &lt;/p&gt; &lt;p&gt;So, I thought, 'I know, I'll create a helpful generic method InObjectBase instead'.&amp;nbsp; Here it is: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b431f0a6-5fcd-4a59-a1ad-c033dfba46ef" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Clone&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;T&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;ref&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T target, &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectBase&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; parent)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;where&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T : &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, ObjectBase&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectBase&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; basetarget &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; target;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;Clone(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;ref&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; basetarget, parent);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (target &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;target &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; basetarget &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;as&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T; &lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//&amp;#39;class&amp;#39; generic constraint &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                                                            &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//allows us to use &amp;#39;as&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#008800"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Now I know the old adage about 'assume' making an ass out of you and me - but feeling very clever, I rolled out the code and started it up.&amp;nbsp; I get a StackOverflowException almost immediately - and I'm concerned - because the code which uses the new generic version hasn't even been called! &lt;/p&gt; &lt;p&gt;So I go and investigate that method, and see that it is, in fact the second line - intellisense shows that it is now a recursive method.&amp;nbsp;&amp;nbsp; Even worse, every other call to 'Clone' (used perhaps about 1,000 times in many different projects at this point!) will now point to the generic version instead of the non-generic - meaning that I've just broken everybody's code. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt; &lt;/p&gt; &lt;p&gt;The compiler matches methods initially by name, and then by signature (minus return type).&amp;nbsp; It also factors in 'convertibility' from method call parameters to the types of the parameters in those methods that might be called, in case exact matches cannot be found.&amp;nbsp; When the compiler performs a search for the target method of a method call, as well, it has to choose a sort order for the candidate methods when that method call is to an overloaded method.&amp;nbsp; If you take a look at the work that the System.Reflection.Binder class has to do you'll get a good idea of this. &lt;/p&gt; &lt;p&gt;It would appear that generic method overloads are always placed at the 'top' of the sort order, meaning that they will always be used, even if there is a specific non-generic overload that matches the input parameters exactly. &lt;/p&gt; &lt;p&gt;Interestingly - this behaviour also seems to be the case even when the use of constraints on a generic type parameter effectively remove the generic method from being a possible candidate method for a call.&amp;nbsp; My framework has an extra class on top of 'ObjectBase' which in fact any custom types would inherit from, so I modified the constraint on my generic to specify that type instead, and move the generic method declaration to that type (to avoid it appearing in misleading situations): &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d84d523e-075c-4edd-bc2d-53c8c91f90fa" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectCustomBase&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; : &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Clone&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;T&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;ref&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T target, &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectBase&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; parent) &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;where&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T : &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, ObjectCustomBase&lt;/span&gt;&lt;/li&gt; &lt;li&gt;   &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//can safely cast down to ObjectBase from ObjectCustomBase&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ObjectBase&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; basetarget &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; target;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;Clone(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;ref&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; basetarget, parent); &lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//&amp;lt;--compiler error&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(target &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;target &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; basetarget &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;as&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T;  &lt;/span&gt;&lt;/li&gt; &lt;li&gt;   &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;This is where I start to wonder if there is a bug in the compiler, we get a compilation error on the line indicated in the comments with the following error: &lt;/p&gt; &lt;p&gt;&lt;font face="Courier New"&gt;The type 'ObjectBase' cannot be used as type parameter 'T' in the generic type or method 'ObjectCustomBase.Clone&amp;lt;T&amp;gt;(ref T, ObjectBase)'. There is no implicit reference conversion from 'ObjectBase' to 'ObjectCustomBase'.&lt;/font&gt; &lt;/p&gt; &lt;p&gt;Even though we now have an unambiguous call to the base 'Clone' method in ObjectBase - because its signature matches exactly the parameter types being passed in, and yet the compiler is totally ignoring it in favour of the generic, whose type constraint means that it should not be entertained at all.&amp;nbsp; If anyone has any ideas how to sidestep this I'd be interested to here about them.&lt;font face="Courier New"&gt;&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;strong&gt;Annoyed&lt;/strong&gt; &lt;/p&gt; &lt;p&gt;What's annoying is, whereas you can force a call to a generic method overload (say, for the purposes of intellisense or whatever) by explicitly specifying the angle brackets - but you cannot explicitly call a non-generic overload.&amp;nbsp; Nor can I think of a suitable way that the language spec can be modified to include such a feature.&amp;nbsp; Basically here we have a feature of the language that prevents us from doing something that would otherwise be trivial. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;Solutions&lt;/strong&gt; &lt;/p&gt; &lt;p&gt;There are two.&amp;nbsp; &lt;/p&gt; &lt;p&gt;1) Silly, but allows us to maintain our intended design - reflect-invoke the non-generic overload of the 'Clone' method instead of directly calling it.&amp;nbsp; We can cache the MethodInfo for it to avoid having to repeat those slow operations (see my previous posts on C# Meta-programming), however, it's a lot of work when we could instead just... &lt;/p&gt; &lt;p&gt;2) Change the generic method name! &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-82558953649602591?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/82558953649602591/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/generic-overloads-and-ref-parameter.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/82558953649602591'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/82558953649602591'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/generic-overloads-and-ref-parameter.html' title='Generic overloads and ref parameter hell (the return of the Stack overflow)'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-4251238617983810274</id><published>2010-09-14T00:03:00.001+01:00</published><updated>2010-09-14T00:03:27.216+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>Exception when using WCF Client to Web Service Reference behind a proxy</title><content type='html'>&lt;p&gt;Are you getting the following exception when calling a simple ASMX service via a WCF service reference from behind a firewall (in this case ISA Server):&amp;nbsp; &lt;/p&gt; &lt;p&gt;"Server encountered an internal error. For more information, turn off customErrors in the server's .config file." &lt;/p&gt; &lt;p&gt;You'd expect there to be an inner exception, but there isn't.&amp;nbsp; You can get to the actual exception by enabling all exceptions to cause a breakpoint in VS2005/2008 (Menu: Debug-&amp;gt;Exceptions and then click the checkbox at the root of all .Net exceptions - be careful doing this in a web site, though, because a tonne of exceptions get thrown and swallowed by the Asp.Net engine!). &lt;/p&gt; &lt;p&gt;The actual error that is occuring is a WebException: [407] Proxy Authentication Required. &lt;/p&gt; &lt;p&gt;In my case, I've also been using &lt;a title="Fiddler" href="http://www.fiddlertool.com/fiddler/" target="_blank"&gt;Fiddler&lt;/a&gt; to debug the SOAP that's going to and from (if you haven't got it, then get it now!) my machine, and had also noticed that when it was running, the exception would go away.&amp;nbsp; This is probably because of some behaviour on ISA Server where it will reuse existing credentials for a connection to a particular outgoing domain until a certain expiry time is reached - did throw me a curve however, until I figured that out! &lt;/p&gt; &lt;p&gt;&lt;em&gt;Quick note - if you're looking at writing stuff to leverage TFS via it's web services and want to know how the Team Explorer and the Source Control Explorer do the things they do - it's a good idea to use Fiddler to examine the SOAP.&lt;/em&gt; &lt;/p&gt; &lt;p&gt;The problem here is an age old one where you have a proxy (in this case ISA Server) through which all your outgoing network traffic is performed.&amp;nbsp; In my environment, the ISA Server requires credentials for a user on the domain that is allowed through the proxy.&amp;nbsp; Without it, the connection will be refused, causing a RemotingException. &lt;/p&gt; &lt;p&gt;Now, in the past this could be solved using the System.Net.CredentialCache.DefaultNetworkCredentials property, and assigning it to a property of the SOAP client proxy that is generated - done.&amp;nbsp; At first glance, there appears to be nothing as simple as that on the ClientBase&amp;lt;T&amp;gt;-deriving type that the WCF service reference generator in VS creates for you. &lt;/p&gt; &lt;p&gt;Looking closer, however, we see the member ClientBase&amp;lt;T&amp;gt;.ClientCredentials, which has a 'Windows' property, which in turn has a 'ClientCredential' property of type NetworkCredentials.&amp;nbsp; Thinking that this would solve my problem, I wrote the following code: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:29b7ff5a-5b71-495f-8d3c-e2db48427eb4" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyWebServiceClient&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; client &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyWebServiceClient&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;client&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ClientCredentials&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Windows&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ClientCredential &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;System&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Net&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;CredentialCache&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;DefaultNetworkCredentials; &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&lt;em&gt;I should mention at this point, that the project I'm working on doesn't bind it's web service references using the WCF config files - because I have to bind to a dynamic endpoint.&amp;nbsp; Anybody working with the configs, however, should still be able to use this post to correct this problem when using the config files.&lt;/em&gt;&amp;nbsp; &lt;/p&gt; &lt;p&gt;Anyway, the exception still gets raised.&amp;nbsp; &lt;/p&gt; &lt;p&gt;I then tried manually filling out the user name, password and domain, but that didn't work either [bangs head on wall]. &lt;/p&gt; &lt;p&gt;Found &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1172919&amp;amp;SiteID=1" target="_blank"&gt;this&lt;/a&gt; MSDN forum where the guy solved his problem using config.&amp;nbsp; not a great use for me, given that I'm configuring declaratively but there is one nugget in there - where the guy sets the HttpTransportBindingElement.UseDefaultWebProxy property to false, and then specifies the address of the proxy in the HttpTransportBindingElement.ProxyAddress property. &lt;/p&gt; &lt;p&gt;This involved also using the CustomBinding class - which I don't want to use.&amp;nbsp; I simply want to create a BasicHttpBinding object, set some properties, and have done with it. &lt;/p&gt; &lt;p&gt;So I tried switching off the default proxy behaviour, and then give WCF the address of the default proxy(!) - read it again if you can't see why I think this is ludicrous. &lt;/p&gt; &lt;p&gt;Here's my eventual code: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8e2168ef-8ce4-4d30-83d4-8889cfe55882" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;TestMethod&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; TestMethod1()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//create a binding&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;BasicHttpBinding&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; binding &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;BasicHttpBinding&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#1affff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BasicHttpSecurityMode&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;TransportCredentialOnly);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;binding&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Security&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Transport&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ClientCredentialType &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;HttpClientCredentialType&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Ntlm;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;binding&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Security&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Transport&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ProxyCredentialType &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;HttpProxyCredentialType&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Ntlm;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//don&amp;#39;t read the system proxy&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;binding&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;UseDefaultWebProxy &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;false&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//in my case - the URL I placed here was the same &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//as the one assigned by group policy! (WTF?)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;binding&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ProxyAddress &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Uri&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;http://proxy:portnum&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//configure the endpoint address&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;EndPointAddress addr &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; EndPointAddress(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;http://domain/service.asmx&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//create an instance of the client type generated by the add &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//service reference tool using the binding and the endpoint &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//address that we&amp;#39;ve configured manually &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyWebServiceClient&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; c &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyWebServiceClient&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(binding, addr);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//make sure that the network credentials are set on the client.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ClientCredentials&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; creds &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;c&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ClientCredentials;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;creds&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Windows&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ClientCredential &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;CredentialCache&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;DefaultNetworkCredentials;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//should bee true by default - but be safe.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;creds&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Windows&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;AllowNtlm &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;true&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//if the web service at the other end needs to use these credentials &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//for impersonation or whatever, then this level should be set &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//accordingly.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;creds&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Windows&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;AllowedImpersonationLevel &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;System&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Security&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Principal&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;TokenImpersonationLevel&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Identification;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//now call the method&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyWebServiceClient&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Foo();&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;And it works fine. &lt;/p&gt; &lt;p&gt;A quick note on this.&amp;nbsp; I am a great fan of MSDN - typically my first port of call is the F1 help, go to the index, type the class into that, and then browse through.&amp;nbsp; Typically you get some choice examples, with some extended documentation on the impact of different values of various settings etc.&amp;nbsp; In some parts of the framework, you even find links out to high-level conceptual topics to see 'where you are' in the overall process of whatever that framework is trying to achieve.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Not with WCF - the F1 help for all the WCF classes is shoddy.&amp;nbsp; It has to be the least well-documented significant framework since SMO (don't even get me started on that) - not joined up in any way. &lt;/p&gt; &lt;p&gt;Anyway, I hope this post helps somebody out! &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-4251238617983810274?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/4251238617983810274/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/exception-when-using-wcf-client-to-web.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/4251238617983810274'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/4251238617983810274'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/exception-when-using-wcf-client-to-web.html' title='Exception when using WCF Client to Web Service Reference behind a proxy'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-4021942987599846683</id><published>2010-09-13T23:49:00.001+01:00</published><updated>2010-09-13T23:49:16.257+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><title type='text'>More C# generic extension methods - non-interface interface methods</title><content type='html'>&lt;p&gt;(This blog post is copied from my old blog at www.lordzoltan.org, it was originally written 1st August 2008)&lt;/p&gt; &lt;p&gt;Somebody asked me the other day if there was a generic way of providing a property-based clone operation for classes.&amp;nbsp; The operation didn't have to recurse into complex objects (just copy the references), but the value types had to be copied. &lt;/p&gt; &lt;p&gt;The first part of the solution was a little bit of reflection: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3f5ed6c1-a571-4e0a-ae67-02365a7d0d96" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Clone(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; source, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; dest)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//(omitted) check that the source and destination types &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//are equal with Type.IsAssignableFrom&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;PropertyInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] props &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; source&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetType()&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetProperties(&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Public &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;|&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Instance &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;|&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;FlattenHierarchy);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MethodInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; getmethod;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MethodInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; setmethod;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;foreach&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;var&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; p &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;in&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; props)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//make sure the get and set accessors are available.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;getmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; p&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetGetMethod();&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;setmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; p&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetSetMethod();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (getmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;||&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; setmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;continue&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//invoke the getmethod on the source, and use the result to pass &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//to the set method on the destination&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;setmethod&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Invoke(dest, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { getmethod&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Invoke(source, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { }) });&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;All great, so now we have a static method we can call - but, however nice it is, it's not that gawk-able. &lt;/p&gt; &lt;p&gt;I wanted to have this method appear in the member list of objects that we wanted to be able to clone - which meant either a base or an interface.&amp;nbsp; Interfaces are less intrusive, because you can still apply them to classes with bases, of course, and you can have multiple interfaces - but having the method as an interface member didn't make any sense.&amp;nbsp; However, if you've read my previous post: &lt;a href="http://lordzoltan.blogspot.com/2010/09/pseudo-template-meta-programming-in-c_10.html" target="_blank"&gt;http://lordzoltan.blogspot.com/2010/09/pseudo-template-meta-programming-in-c_10.html&lt;/a&gt;, which uses some generic extensions alongside static generics to provide meta info, the solution becomes easy.&amp;nbsp; However you have to do something that a lot of people would seriously moan about...&amp;nbsp; an empty interface. &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9d350f15-92a4-40ff-9e2e-7960c3054f48" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;interface&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IGenericCloneable&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;IGenericCloneableExtensions&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Clone&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;T&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;this&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T source, T dest)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;where&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T : &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IGenericCloneable&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;PropertyInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] props &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;typeof&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(T)&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetProperties(&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Public &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;|&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Instance &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;|&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;FlattenHierarchy);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MethodInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; getmethod;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MethodInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; setmethod;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;foreach&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;var&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; p &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;in&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; props)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;getmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; p&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetGetMethod();&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;setmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; p&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetSetMethod();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (getmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;||&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; setmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;continue&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;setmethod&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Invoke(dest, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { getmethod&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Invoke(source, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { }) });&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Notice that the reflection is performed on typeof(T) instead of source.GetType() - which should be faster at runtime. &lt;/p&gt; &lt;p&gt;There is a gotcha here, though, and it's to do with the T that is passed when this method called for base classes of a superclass, consider the following: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:da98285b-f550-4197-9b1f-b640415fa9bb" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; : &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IGenericCloneable&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;int&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; ID { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;set&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable2&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; : &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable1&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Name { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;set&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Now some method declared in another static class, no attempt made to contrive a complicated scenario, instead we just have a rather pointless method, but it demonstrates the issue at hand: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:715431c7-4103-4131-896d-14665f451a95" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Utility&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; GetClone(&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; source, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; dest)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;source&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Clone(dest);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Now let's test the method for an instance of Cloneable1 and for Cloneable2: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:434b6e2f-6f1e-4234-92b0-2ea2e5233cbb" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;TestMethod&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; TestClone1()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; c1 &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;() { ID &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;10&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; };&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; c2 &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Utility&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetClone(c1, c2);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Assert&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;AreEqual(c1&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ID, c2&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ID); &lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//should be fine.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#008800"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;TestMethod&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; TestClone2()&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable2&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; c1 &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable2&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;() { ID &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;10&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, Name &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;Fred&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; };&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable2&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; c2 &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Cloneable2&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Utility&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetClone(c1, c2);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Assert&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;AreEqual(c1&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ID, c2&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ID); &lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//should be fine.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Assert&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;AreEqual(c1&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Name, c2&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Name); &lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//will fail&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#008800"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The second test will fail, because the GetClone method implicitly passes Cloneable1 as the &amp;lt;T&amp;gt; parameter for the Clone&amp;lt;T&amp;gt; extension method, since T is taken from the left-hand side of the '.' operator which selects the method.&lt;br&gt;&lt;br&gt;One way to get around this problem is to write another extension method for IGenericCloneable in a new IGenericCloneableExtensions2 class (simplifies the reflection), this time which takes IGenericCloneable and is not generic, we can give it the same name because the compiler will intelligently select the method that matches the MOST derived version of the input parameters when it is called: &lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:badecb99-3daf-49fc-bfb7-1dda1172bc0e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;IGenericCloneableExtensions2&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Clone(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;this&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IGenericCloneable&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; source, &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IGenericCloneable&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; dest)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;typeof&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;IGenericCloneableExtensions&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetMethod(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;Clone&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, System&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Reflection&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Static &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;|&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; System&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Reflection&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Public)&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;MakeGenericMethod(&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Type&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { source&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetType() })&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Invoke(&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { source, dest });&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;Now we change the signature of our utility method to take instances of IGenericCloneable instead of the Cloneable1 class, and the compiler will redirect calls to the intermediate method instead of the generic.  &lt;p&gt;&lt;/p&gt; &lt;p&gt;This is a useful practise anyway, because it means you can make the IGenericCloneable(2) extensions available to code that only has instances of the interface (which, after all, is what most people will expect of an interface).&amp;nbsp; However, we can bypass the need to regress the Utility.GetClone method to using IGenericCloneable instance, by tightening up our original generic version with a little bit of reflection and dynamic generic compilation magic: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c3f79c08-6174-4ee3-a732-7ce75c2a00f6" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#008800"&gt;//modify the original version to check the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#008800"&gt;//actual instance types of source and dest&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Clone&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;T&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;this&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T source, T dest) &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;where&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T : &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IGenericCloneable&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Type&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] instancetypes &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Type&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;source&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetType(), dest&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetType() };&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (instancetypes[&lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;]&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Equals(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;typeof&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(T)) &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;false&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//check ultimate&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (instancetypes[&lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;]&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Equals(instancetypes[&lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;]) &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;false&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; instancetypes[&lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;]&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;IsSubclassOf(instancetypes[&lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;1&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;]) &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;             &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;false&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;throw&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;ArgumentException&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;If the two instance types are not equal, &amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;+&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                            &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;the first instance type must be derived from the target type&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;((&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MethodInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MethodInfo&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetCurrentMethod())&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetGenericMethodDefinition()&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                    &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; MakeGenericMethod(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Type&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { instancetypes[&lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;] })&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                            &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Invoke(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { source, dest });&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//the rest is the same as before&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;PropertyInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] props &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;typeof&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(T)&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetProperties(&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Public &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;|&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Instance &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;|&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;FlattenHierarchy);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MethodInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; getmethod;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MethodInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; setmethod;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;foreach&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;var&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; p &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;in&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; props)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;getmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; p&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetGetMethod();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;setmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; p&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetSetMethod();&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (getmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;||&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; setmethod &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;continue&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;setmethod&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Invoke(dest, &lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { getmethod&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Invoke(source, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { }) });&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Now, we can return our original GetClone method to take instances of Cloneable1, and the two tests should pass. &lt;/p&gt; &lt;p&gt;Don't be worried about the MakeGenericMethod call - the type engine will only actually &lt;em&gt;compile&lt;/em&gt; the method once - after that, it will simply go and obtain a handle to the compiled method.&amp;nbsp; &lt;strong&gt;&lt;u&gt;[Note – with the advent of expression trees I now do things like this by dynamically compiling methods and then caching them – the result is far faster and much tidier :)]&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;There are other things we could do here, for example make another version of Clone which takes two type parameters: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1ed40da0-7341-4368-8222-22a8243fc81b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Clone&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;T1, T2&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;this&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T1 source, T2 dest)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;where&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T1 : T2&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;where&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T2 : &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IGenericCloneable&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//same code as the original Generic one we wrote, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//using the property list of T2 instead of T1.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#008800"&gt;}&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;This method automatically allows us to clone an instance of T1 to T2 because T1 is inherited from T2, and it'll still appear as an extension method to the instance of T1 because T2 is constrained to implement the IGenericCloneablee interface.&lt;br&gt;&lt;br&gt;We could then modify the reflection code at the head of the modified generic Clone&amp;lt;T&amp;gt; method so that it picks up this generic method when the instance type of the destination object is a base class of the instance type of the source. &lt;/p&gt; &lt;p&gt;We could write a CloneNew&amp;lt;T&amp;gt; method, by specifying an additional constraint on &amp;lt;T&amp;gt;: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c2b0cc6d-9524-4039-9bdf-5723c5a392e2" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T CloneNew&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;T&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;this&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T source)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;where&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T : &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IGenericCloneable&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;T result &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;source&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Clone&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;T&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(result);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; result;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;And if you started stacking these empty interfaces on top of others, you can start building whole libraries of useful generic extension methods that provide a whole host of common functionality to otherwise method-less types - and still be able to inherit those types from concrete classes (because the interface list is unbounded). &lt;/p&gt; &lt;p&gt;Have fun!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-4021942987599846683?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/4021942987599846683/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/more-c-generic-extension-methods-non.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/4021942987599846683'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/4021942987599846683'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/more-c-generic-extension-methods-non.html' title='More C# generic extension methods - non-interface interface methods'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-6353498321971381119</id><published>2010-09-10T13:39:00.001+01:00</published><updated>2010-09-10T13:39:52.661+01:00</updated><title type='text'>Pseudo-Template Meta-Programming in C# - Part 2</title><content type='html'>&lt;p&gt;Following on from a taster that I posted before: &lt;a href="http://lordzoltan.blogspot.com/2010/09/pseudo-template-meta-programming-in-c.html"&gt;http://lordzoltan.blogspot.com/2010/09/pseudo-template-meta-programming-in-c.html&lt;/a&gt;, here goes with the next piece. &lt;/p&gt; &lt;p&gt;Firstly, the design I'm showing here can be applied to any reflection scenario - I've deployed it in various guises, including for storing meta-data about methods in a class, properties, and even for compiling dynamic methods that provide late-bound functions.&amp;nbsp; The primary reason why this design, I feel, is particularly useful is that you're making the .Net type engine do all the hard work of storing all this meta-data for you. &lt;/p&gt; &lt;p&gt;I'll go through this by looking at a really simple scenario: &lt;/p&gt; &lt;p&gt;You want to write a system whereby you can reflect properties of a type that a developer writes in order to provide some form of functionality.&amp;nbsp; Let us say that you want to write a dictionary state-bag wrapper, where a property is wrapped around an internal dictionary of values.&amp;nbsp; What you want is to have a code-generation friendly way for developers to add such properties to their class with the minimum of fuss.&amp;nbsp; So, retrieving a property becomes as simple as: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7dfa3105-6b38-49ed-aad1-bcc7a928ef48" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; SomeProperty&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;base&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetValue(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;SomeProperty&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;); }&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;You don't necessarily want to have the names in the state bag to be the same as the names of the properties, so you need to be able to allow the developer to specify, with the minimum of fuss, what the backer for the property is actually called. &lt;/p&gt; &lt;p&gt;The perfect solution to this is an attribute: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ddc9bf21-2ff5-477f-9b26-48f2b60584ec" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;AttributeUsage&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;AttributeTargets&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Property)]&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryPropertyAttribute&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; : &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Attribute&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; Name { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;set&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Now we can use the attribute as follows (now making a class that we can use later): &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:46dced6e-b18a-4802-a40b-9cbd28993553" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyClass&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryProperty&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(Name &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;PROP_SomeProperty&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)]&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; SomeProperty&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;base&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;SetValue(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;SomeProperty&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;); }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;So far so good, but we need a base class to be called.&amp;nbsp; What we're going to do here is to produce two levels of abstraction: an interface and a base class that we can inherit from.&amp;nbsp; The base class will implement the interface to provide a simple implementation of a dictionary.&amp;nbsp; Other implementations could wrap around a data row, ASP.Net ViewState or SessionState and so on. &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ccf5b4d0-56c0-47f8-9a33-c161dc221495" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;interface&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IDictionaryProps&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; SetValue(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; name, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; value);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; GetValue(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; name);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;For simplicity's sake we'll say that an implementation of GetValue must return null if a value with the supplied name doesn't exist.&amp;nbsp; And SetValue should quietly overwrite existing values, while adding new ones. &lt;/p&gt; &lt;p&gt;Our base class, then, looks like this: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:23422ac4-c197-4278-a5c3-d7a3ae7b4b6c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryPropertyClass&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; : &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Dictionary&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IDictionaryProps&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; SetValue(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; name, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; value)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;this&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[name] &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; value;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; GetValue(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; name)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; toreturn &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;this&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;TryGetValue(name, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;out&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; toreturn);&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; toreturn;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;And that's it, now we can write the outer class, with a couple of minor adjustments, for the wrapped property that I outlined first, so that it inherits from DictionaryPropertyClass. &lt;/p&gt; &lt;p&gt;But wait, what about this mapping business?&amp;nbsp; Well, that's where we enter the realm of static generics. &lt;/p&gt; &lt;p&gt;For anyone still getting to grips with generic types, they are basically classes with type parameters - unlike C++ templates, however, which are built at compile time in a template-style, the generic class itself (without any type parameters) is a type in its own right - except it cannot be used to create an instance. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;em&gt;TIP: For anyone already doing run-time wizardry with generics - try &lt;font face="courier new,courier"&gt;typeof(MyGeneric&amp;lt;&amp;gt;)&lt;/font&gt; - it returns a reference to the generic type rather than an instance of the generic type.&amp;nbsp; If your generic has multiple parameters, then you simply include a comma for each additional parameter.&amp;nbsp; &lt;font face="courier new,courier"&gt;typeof(MyGeneric&amp;lt;,,&amp;gt;)&lt;/font&gt;, for example, returns a reference to the System.Type of a generic that takes three type parameters.&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt; &lt;p&gt;What we're going to do is to create a static generic class (i.e. we never actually create an instance of it) which is going to act as a container for the reflected property information for any type: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b22d0472-65e1-4980-b035-e7b9cc7433ef" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryClassMetaData&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;T&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#fffa2f"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;where&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T : &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IDictionaryProps&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#d9537b"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//stores the mappings for properties to dictionary keys for T &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;private&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Dictionary&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; _mappings;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//obtains the mapping for a particular property by its name.  Caution, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//this is case-sensitive of course (use &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//StringComparer.CurrentCultureIgnoreCase  in the constructor for &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//_mappings to avoid this problem if case-sensitivity isn&amp;#39;t required). &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; GetKey(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; propertyname)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (_mappings &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; toreturn &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;_mappings&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;TryGetValue(propertyname, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;out&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; toreturn);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; toreturn;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//static constructor - a lot of debate about these - mainly because of&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//the dreaded TypeInitializationException when the constructor raises &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//an uncaught exception, but if you are careful with your boot-strap code &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//(and religious with try/catch blocks where necessary) you don&amp;#39;t have &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//anything to worry about. &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; DictionaryClassMetaData()&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;BuildPropertyMap();&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//the method that does all the work &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;private&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;void&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; BuildPropertyMap()&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//get the property list for T &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//note the use of System.Linq here and a lambda expression that filters&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//the PropertyInfo objects returned from the GetProperties call so that &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//we only get those properties which have our DictionaryPropertyAttribute &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//note also the use of typeof(T) - allows us to reflect on the T that &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//is fed into the generic type parameter of the static class. &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//finally, note the use of the FlattenHierarchy binding flags - this &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//means that our T can inherit from other types that also declare mapped &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//properties and still be able to get the entire mapping for the whole type. &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IEnumerable&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;PropertyInfo&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; props &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;typeof&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(T)&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetProperties(&lt;/span&gt;&lt;/li&gt; &lt;li&gt;             &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Instance &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;|&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Public &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;|&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;FlattenHierarchy)&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Where(&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                                    &lt;span style="background:#000000;color:#ffffff"&gt;pinfo &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; pinfo&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;IsDefined(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;typeof&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryPropertyAttribute&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;), &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;true&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;));&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Dictionary&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; tocreate &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Dictionary&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryPropertyAttribute&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; attr &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//now to build our internal dictionary, if any properties were found &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (props &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;foreach&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;PropertyInfo&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; pinfo &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;in&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; props)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;attr &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryPropertyAttribute&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)pinfo&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetCustomAttributes(&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;typeof&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryPropertyAttribute&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;), &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;true&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)[&lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;];&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//make sure the name is unique.  If so, add it. If not, then some form of &lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//error reporting should be performed. &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//a good implementation should have a structure within this static type &lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//that can be used to obtain errors instead of &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//throwing exceptions &lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (tocreate&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;ContainsKey(attr&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Name) &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;false&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;                &lt;span style="background:#000000;color:#ffffff"&gt;tocreate&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Add(pinfo&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Name, attr&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Name);   &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#008800"&gt;//if any properties found, set the dictionary, otherwise don&amp;#39;t do anything &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#000000;color:#008800"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (tocreate&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Count &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;!=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#d3d3d3"&gt;0&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;_mappings &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; tocreate;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Believe it or not, that's basically it.&amp;nbsp; From outside of our class &lt;font face="courier new,courier"&gt;MyClass&lt;/font&gt;, we can now interrogate property mappings with the following line of code: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:40d3d5d8-8f68-4e1b-b611-1c81c4b5e240" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; valuekey &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryClassMetaData&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyClass&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetKey(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;SomeProperty&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;); &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The first time a call like this is made, the .Net runtime will fire the static constructor for &lt;font face="courier new,courier"&gt;DictionaryClassMetaData&amp;lt;MyClass&amp;gt;&lt;/font&gt; (remember it's not a proper type until it's actually got a generic parameter in it!), after which it calls the GetKey method.&amp;nbsp; After that initial call, however, it knows that it doesn't need to call it again, so it just fires the method.&amp;nbsp; What's more, the .Net runtime ensures thead safety for that call, too, so you don't have to worry about locking anything in a multithreaded environment. &lt;/p&gt; &lt;p&gt;How does this help us with the initial plan?&amp;nbsp; At the moment, we could reimplement our &lt;font face="courier new,courier"&gt;SomeProperty&lt;/font&gt; property in &lt;font face="courier new,courier"&gt;MyClass&lt;/font&gt; as follows (only the get accessor code is listed now): &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:80eea753-a1bd-4365-baea-c93be8dbb28e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; GetValue(&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryClassMetaData&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyClass&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetKey(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;SomeProperty&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)); &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Hmmm, smelly isn't it...&amp;nbsp; What's more - the developer implementing their class needs to know about the meta class - which is no good. &lt;/p&gt; &lt;p&gt;This is where I admit that I've led us down a blind alley - the truth is that the original idea of using &lt;font face="courier new,courier"&gt;base.Foo(bar)&lt;/font&gt; as a methodology isn't going to cut it.&amp;nbsp; Although, as you'll see in a minute, we could implement it that way, there is a better way that we can do it.&amp;nbsp; Generic extension methods: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:40d797ee-dbf9-48e8-b552-7cd0efad9e9c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;IDictionaryPropsExtensions&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{ &lt;/span&gt;&lt;/li&gt; &lt;li&gt;   &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;static&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; GetMappedValue&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;T&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;this&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T instance, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; propertyname) &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;         &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;where&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; T:&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;,&lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IDictionaryProps&lt;/span&gt;&lt;/li&gt; &lt;li&gt;   &lt;span style="background:#000000;color:#ffffff"&gt;{ &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;      &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; datakey &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryClassMetaData&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;T&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetKey(propertyname); &lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;if&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(datakey &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;==&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;) &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;         &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; instance&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetValue(datakey); &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;   &lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Now, we can rewrite our implementation of MyClass' SomeProperty as follows (this time, I'll include the full code): &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c7dc07b4-294c-4db4-ab00-4bc5e6ff934b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;class&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;MyClass&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; : &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryPropertyClass&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#1affff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryPropertyAttribute&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(Name &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;PROP_SomeProperty&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)]&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; SomeProperty&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;        &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;get&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; { &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;this&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetMappedValue(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;SomeProperty&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;); }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Note how the implementation now uses 'this' directly.&amp;nbsp; This is a strange feature of extension methods: when inside class scope, you will not see extension methods defined for that type unless you explicitly use 'this' - this is because without the 'this', the intellisense (and presumably the compiler) has no way of knowing that you actually mean 'this'.&amp;nbsp; This is different from instance members - because they are within scope at the point where you are writing the code (when inside a class definition), therefore they are implicitly available. &lt;/p&gt; &lt;p&gt;We're also using the 'this' as the type parameter for our GetMappedValue extension method.&amp;nbsp; If you were to breakpoint the extension method and do a get on the property, you will see that 'T' has been implicitly fed into it as MyClass.&amp;nbsp; This is how we are then able to invoke the static meta class without any real effort whatsoever. &lt;/p&gt; &lt;p&gt;And finally... &lt;/p&gt; &lt;p&gt;Now that you have a static meta class that you can invoke in a single line of code, you can also do it with reflection.&amp;nbsp; Consider this line of extremely generic code, which could be thought of as being similar to how the databound controls in Windows Forms, WPF and ASP.Net get hold of values when using the 'DataTextField' and 'DataValueField' properties: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:576d9711-4ab9-427f-a58d-a2db4997533e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; GetObjectPropertyValue(&lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IDictionaryProps&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; instance, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; propertyname) &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{ &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;So now we're faced with a slight problem - we can't talk directly to the meta data class, because we don't have a T to work with.&amp;nbsp; If only we could dynamically invoke the meta class...&amp;nbsp; Hopefully, if you didn't already know it, you have probably guessed that we can.&amp;nbsp; This time we're using dynamic generics - and this is where my little tip earlier on of using &lt;font face="courier new,courier"&gt;typeof(SomeGeneric&amp;lt;&amp;gt;)&lt;/font&gt; comes in really handy: &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b70e7194-0900-44b2-8fb2-eea9e41e84a3" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #000000; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt; &lt;li&gt;&lt;span style="background:#000000;color:#1aff36"&gt;public&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; GetObjectPropertyValue(&lt;/span&gt;&lt;span style="background:#000000;color:#d9537b"&gt;IDictionaryProps&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; instance, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; propertyname)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;&lt;span style="background:#000000;color:#ffffff"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Type&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; t &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; instance&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;GetType();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Type&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; tmeta &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;=&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;typeof&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;(&lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;DictionaryClassMetaData&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;MakeGenericType(&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1affff"&gt;Type&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { t });&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;    &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;return&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; (&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;string&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;)tmeta&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;InvokeMember(&lt;/span&gt;&lt;span style="background:#000000;color:#fdbe02"&gt;&amp;quot;GetKey&amp;quot;&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;,&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;System&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Reflection&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Public &lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;|&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; System&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Reflection&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#a7c62b"&gt;BindingFlags&lt;/span&gt;&lt;span style="background:#000000;color:#fffa2f"&gt;.&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;Static,&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #0c0c0c"&gt;            &lt;span style="background:#000000;color:#ffffff"&gt;&lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;null&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;, &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;new&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt; &lt;/span&gt;&lt;span style="background:#000000;color:#1aff36"&gt;object&lt;/span&gt;&lt;span style="background:#000000;color:#ffffff"&gt;[] { propertyname });&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#000000;color:#ffffff"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;An interesting note about this code - even when you access a static class via reflection in this way, .Net will still maintain it's rule of firing the static constructor just once.&amp;nbsp; Also, I'll wager that the speed with which it can obtain the type instance after the initial call is pretty good. &lt;/p&gt; &lt;p&gt;You can (and I have) cache the method for future use in a Delegate stored within the instance (think '&lt;font face="courier new,courier"&gt;Delegate.MakeDelegate(typeof(Func&amp;lt;string, string&amp;gt;), reflectedmethod);&lt;/font&gt;') - this speeds up future access a great deal, because the JIT can optimize away a lot of the method-jumping. &lt;/p&gt; &lt;p&gt;There are some truly crazy things I've done with this model and I might blog about them in the future when I get a chance - in the meantime I hope this provides satisfaction for your coding needs! &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-6353498321971381119?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/6353498321971381119/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/pseudo-template-meta-programming-in-c_10.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/6353498321971381119'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/6353498321971381119'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/pseudo-template-meta-programming-in-c_10.html' title='Pseudo-Template Meta-Programming in C# - Part 2'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-497310774772282847.post-7862659871919752916</id><published>2010-09-10T13:04:00.001+01:00</published><updated>2010-09-10T13:04:29.614+01:00</updated><title type='text'>Pseudo-Template Meta-Programming in C#</title><content type='html'>&lt;p&gt;Not full details here, but I'm writing this post as part-memo to myself, but also in the hope that one or two of you might make a mental bookmark to keep checking back. &lt;/p&gt; &lt;p&gt;In the course of my most recent project, which involve classes which wrap a common data storage mechanism (a bit like DataRows) I encountered a problem which was best solved with a solution called (coining a phrase) 'self-aware' properties.&amp;nbsp; Assuming all the storage access code is provided in a common base, and using attributes to define the string name of the 'column' (to continue the DataRow analogy) to which a property is mapped: &lt;/p&gt; &lt;p&gt;&lt;font face="courier new,courier"&gt;class X&lt;br&gt;{&lt;br&gt;&amp;nbsp; [MappedProperty("actualvaluename")]&lt;/font&gt;&lt;font face="courier new,courier"&gt;&lt;br&gt;&amp;nbsp; public string SomeValue&lt;br&gt;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return base.GetPropertyValue("SomeValue");&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;font face="courier new,courier"&gt;&lt;br&gt;&amp;nbsp; }&lt;/font&gt;&lt;font face="courier new,courier"&gt;&lt;br&gt;}&lt;/font&gt; &lt;/p&gt; &lt;p&gt;So far so rudimentary.&amp;nbsp; However, I wanted my framework to be able to support inheritance, so that a class inheriting from another one not only could inherit it's properties, but also so it could actually &lt;em&gt;override&lt;/em&gt; the mappings for a property defined by a base, just by using the same attribute. &lt;/p&gt; &lt;p&gt;Also, what I didn't want to have to do was: &lt;/p&gt; &lt;p&gt;a) Store mapping information in every instance of such a class (to weighty)&lt;br&gt;b) Write some behemoth registry which keeps track of all the mappings (initialisation problems, potential threading issues, not to mention a nightmare to extend and maintain) &lt;/p&gt; &lt;p&gt;The solution that I have produced takes a few ideas from the ideas laid out by C++ Template Meta-Programming (read this book if you want to know more: &lt;a href="http://www.amazon.co.uk/Template-Metaprogramming-Concepts-Techniques-Beyond/dp/0321227255"&gt;http://www.amazon.co.uk/Template-Metaprogramming-Concepts-Techniques-Beyond/dp/0321227255&lt;/a&gt;), but of course it all happens at run time - because Templates in C++ are not the same as C# Generics.&amp;nbsp; It takes advantage of an immensely powerful feature of Extension Methods - generic extension methods (think &lt;font face="courier new,courier"&gt;public static void Foo&amp;lt;T&amp;gt;(this T instance, ...)&lt;/font&gt; ),&amp;nbsp; as well dynamic Generic class instantiation using reflection to achieve the generic recursion required to build the data for each level in the class hierarchy. &lt;/p&gt; &lt;p&gt;All it took was a common base (with practically no methods), a static generic class plus some extension methods - and the C# type engine does the rest for you. &lt;/p&gt; &lt;p&gt;In a future post I will lay out the design, but for now I've run out of time (it's Fathers' day - which as many fathers will know doesn't necessarily mean that Father gets to choose what he does!) &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/497310774772282847-7862659871919752916?l=lordzoltan.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lordzoltan.blogspot.com/feeds/7862659871919752916/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/pseudo-template-meta-programming-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/7862659871919752916'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/497310774772282847/posts/default/7862659871919752916'/><link rel='alternate' type='text/html' href='http://lordzoltan.blogspot.com/2010/09/pseudo-template-meta-programming-in-c.html' title='Pseudo-Template Meta-Programming in C#'/><author><name>Lord Zoltan</name><uri>http://www.blogger.com/profile/18416046190284445908</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_f-d_NdW7K_Q/TI8fVueKmEI/AAAAAAAAABY/B8jedcWcE0M/S220/LZGravatar.png'/></author><thr:total>0</thr:total></entry></feed>
