Scott Hanselman

Mixing Languages in a Single Assembly in Visual Studio seamlessly with ILMerge and MSBuild

October 6, '07 Comments [13] Posted in ASP.NET | Microsoft | Programming | XML
Sponsored By

I really like the new LINQ to XML direct language support in VB9. However, while it's cool, I'm not ready (or willing) to dump C# and start using VB. But, if I could only use VB9 just for the XML stuff...

Sure, I could create a VB assembly and a C# assembly and add them to a solution, but then I'd have two assemblies. I could add a PostBuildEvent batch file and call ILMerge myself (merging the two assemblies into one), but that just doesn't seem pure enough.

solutionWhat I really want is to be able to mark an assembly as merge-able and have it automatically merged in just because it's referenced.

Here's what I came up with. Thanks to Dan Moseley and Joshua Flanagan for their help. Thanks to Jomo Fisher for the Target File.

First, here's the sample project. There's a C# MergeConsole.exe that references a C# MainLibraryILMerge.dll that references a VB XmlStuffLibrary.

If I compile it as it is, I get a folder structure that includes three resulting assemblies.

folder1

Now, here's were we start messing around. Remember, we don't want to have the extra VB-specific XmlStuffLibrary right? We just want to use the XML features.

First, download and install ILMerge in the standard location.

Now, go to C:\Program Files\MSBuild (or C:\Program Files (x86)\MSBuild on x64) and make a new text file called "Ilmerge.CSharp.targets". This file is the start of a hack we're going to borrow from Jomo Fisher.

Note the red bits in the file below. We're creating an "AfterBuild" target...a Post Build Event in the MSBUILD world.

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> 
    
  <Target Name="AfterBuild">
    <CreateItem Include="@(ReferencePath)" Condition="'%(CopyLocal)'=='true' and '%(ReferencePath.IlMerge)'=='true'">
      <Output TaskParameter="Include" ItemName="IlmergeAssemblies"/>
    </CreateItem>
    <Message Text="MERGING: @(IlmergeAssemblies->'%(Filename)')" Importance="High" />
    <Exec Command="&quot;$(ProgramFiles)\Microsoft\Ilmerge\Ilmerge.exe&quot; /out:@(MainAssembly) &quot;@(IntermediateAssembly)&quot; @(IlmergeAssemblies->'&quot;%(FullPath)&quot;', ' ')" />
  </Target>
  <Target Name="_CopyFilesMarkedCopyLocal"/>
</Project>

In this target, we're going to look for assemblies that are marked CopyLocal but also that have IlMerge equal to true. Then we'll call IlMerge passing in those referenced assemblies.

Is this some undocumented MSBUILD thing? No, you can put whatever you want in an MSBUILD file and refer to it later. Since CSPROJ files (Visual Studio Projects) are MSBUILD files, we can open it in Notepad.

Open the CSPROJ for the C# project that references the VB one and make these changes: 

... 
<ItemGroup>
   <ProjectReference Include="..\XmlStuffLibrary\XmlStuffLibrary.vbproj">  
    <Project>{someguid}</Project>    
    <Name>XmlStuffLibrary</Name>
    <Private>True</Private>
    <IlMerge>True</IlMerge>
  </ProjectReference>
</ItemGroup>  
<Import Project="$(MSBuildExtensionsPath)\Ilmerge.CSharp.targets" />
<!-- <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -->
...

At the bottom there, we're commenting out Microsoft.CSharp.targets (but notice that it's included back in at the top of the new  Ilmerge.CSharp.targets.

Then a totally made-up element <IlMerge> is added. That's the most important part. We're saying we want this specific Reference (or References) merged into the final assembly. This made-up element is referenced in the conditional "'%(ReferencePath.IlMerge)" above in the AfterBuild.

The addition of this IlMerge element to Jomo's original hack gives me the flexibility to pick which references I want to merge in, and in my specific case, I'll use the VB9 new XML hotness inside my C# assemblies. Schwing.

Close and save. If you're running Visual Studio, switch back there and it'll prompt you to Reload your project file.Because we've messed it it, you'll get this warning dialog. Select "Load project normally" and click OK.

Security Warning for MainLibraryILMerge

At this point we can build either from the command-line using MSBUILD on the Solution (SLN) file, but more importantly we can (of course) build from inside Visual Studio.

MergeConsole - Microsoft Visual Studio

You can see the output in the Output Window in VS.NET.

MERGING: XmlStuffLibrary
"C:\Program Files (x86)\Microsoft\Ilmerge\Ilmerge.exe"
  /out:bin\Debug\MainLibrary.dll "obj\Debug\MainLibrary.dll"   
  "C:\dev\CSharpVBTogetherAtLast\MergeConsole\XmlStuffLibrary
  \bin\Debug\XmlStuffLibrary.dll"

And the results in the bin folder:

folder2

Looks like the VB XmlStuffLibrary is gone! But where it is? Let's load up MainLibrary in Reflector:

reflector1

Looks like they are both in there. To refresh, if I want to merge in VB assemblies:

  • Change the referencing CSPROJ to import "IlMerge.CSharp.targets"
  • Add <IlMerge>True</IlMerge> to the references you want merged in.
  • Save, Reload, Build

C# and VB, living together, mass hysteria!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

The Red Ring of Death makes itself known

October 4, '07 Comments [32] Posted in Gaming
Sponsored By

CIMG7489Crap. Now what?

I wish I know exactly what causes/caused this.

Last night Mo said the Xbox locked up hard while she was listening to her Zune. She thought she broke it. Reboot and we were fine.

Later, I wasn't able to connect to the Windows Home Server to stream pictures. Reboot and the Xbox locked up while in the "splash screen" presentation.

Smells like heat. I unplugged everything and plugged it back in again and it's working fine. Haven't had a problem since.

However, this was the FIRST and only time I ever had any trouble with the thing. It's a "launch day" Xbox360 with the add-on 120gig drive. I'm wondering if I should send it in for a replacement or just get an HDMI-enabled 1080p model.

Sigh.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Hanselminutes Podcast 83 - Microsoft to release .NET Framework Libraries Source

October 3, '07 Comments [22] Posted in Learning .NET | Microsoft | Podcast
Sponsored By

2swan-asa3 Yes, you read that correctly. Microsoft is releasing the source code to a bunch of .NET Framework Libraries under the "Microsoft Reference License." This means you can view the source for reference, but not for recompilation, etc.

This is a lot like Symbol Servers, which I've talked about on the blog before, but this'll be a "Public Source Server" (Source Servers are an old concept, but rare to see in the wild) so that you be a able to debug seamlessly between your code and the Framework's code.

In my first "scoop" in my new job at Microsoft, I've already got a show ready for you, recorded last week! See, I told you I'd take care of you. 

This show is actually an interview with Shawn Burke the mastermind behind this move who posted his intent to make this happen on his personal blog in 2005! His current job is being in charge of the Ajax Toolkit, but he's been working on this source code release idea for years - plural.

Support for this new public Microsoft .NET Source Server feature will be in Visual Studio 2008 when it comes out later this year. It'll be a simple setup process and you'll be on your way to "Stepping Into" the code.

We talk on the podcast about the differences between using Reflector to look at source and looking at the source itself. We ask Shawn about Source Servers vs. Symbol Servers, and how Microsoft is going to size their network to support the demand.

dlprocess_thumb

As soon as I have details on how this configuration process will work, I'll let you know. I suspect it'll be very similar to the way Symbol and Source Servers work today in Visual Studio.

Enjoy!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Getting Organized While Drinking from the (Outlook) Fire Hose

October 2, '07 Comments [49] Posted in Microsoft | Musings
Sponsored By

fireboss Microsoft appears to run on Outlook and Exchange. Seems like an obvious statement as many places run on Outlook and Exchange. But I'm saying it with emphasis. These guys used Outlook like it's an IM client. There's also a distribution list for everything.

New job, new fresh email box, time to get organized and serious about ZEB (Zero Email Bounce.) You can't really get your Inbox to zero and stay there but you can "bounce" against zero, which I do a few times a day. If it's in your inbox it's not been processed.

I used a number of techniques and features in Outlook to make my life (theoretically) easier:

Folders

outlookfolders[1]I use a "Getting Things Done" style of organization. I've got my folders laid out like this.

I've got Outlook Rules for the Various Mailing Lists I've gotten myself on in the last few weeks. That's the other other "Inboxes" are for.

The "Inbox - CC" folder is for when I'm cc'ed on an email. The actual Inbox itself is only for when an email is sent directly to me. Everything else goes somewhere else.

The Action folders start with an @ sign and are at the top.

  • There's things that require @Action that aren't projects.
  • Topics to @Blog about
  • People to see and things to do on my @Next Redmond Trip.
  • Emails I need to @Reply to that will take longer than 5 minutes to respond.
  • There's things I'll do @Someday soon, but just not now.
  • Things I'm @Snooze-ing on, but I'm not willing to move them completely out of mind.
  • Finally there's things I'm @Waiting For other people do to for me.
  • The Conversation History folder is where internal Instant Message conversations go. This is REALLY useful for reference. Missed IMs appear in the Inbox and are filed as needed.

Anything that comes into the inbox needs to be processed and one of the following GTD things needs to happen:

  • Do It
  • Drop It
  • Defer It
  • Delegate It

Then there's the Projects folder. These are long-running (more than a few days) projects that I'm actively working on. Emails that are important to those Projects go in those folders. It's pretty minimal reference stuff.

The Reference folder is just that, it's Reference stuff. Things I'll want to search for later, and under it is the IT Issues folder which is also for reference, but specific to IT stuff I'm suffering with working through.

When my inbox is at zero I do a quick sweep through my @ folders before continuing work on a Project.

Calendars

I really like Outlook 2007's calendaring and it grows on me more and more. I use colors to categories my appointments, but I also use multiple calendars more and more using the ICS Webcal standard. I've got four different calendars in addition to my standard calendar.

  • One is fed from TripIt.com - I'll blog about them later, but they're freaking brilliant.
  • One is the feed from my wife's Google Calendar (this is also fantastic)
  • One is fed from my wife and my Project at BaseCamp. We're building a house and we're managing the project, the move and appointments with many subcontractors via this tool and I'm subscribed to the Milestones in Outlook, and she in Google Calendar.
  • One is from my publisher something we're working on.

Note that the tabs are next to each other near the top (under where it says "October 2007"). This way, rather than split-screen, each calendar is transparently overlaid over the other. I've found this to be a REALLY effective way of visualizing up to six different calendars while keeping each one separate.

cal[1]

If you're not using this feature, I'd encourage you to check it out.

The last thing I've done to make it easier for external folks to schedule meetings with me is I've made a redirect from http://www.hanselman.com/freebusy to my published Free/Busy information. This can be done by right-clicking on your calendar and publishing just the free/busy information to the Internet. I just made a single default.aspx file to rediect to the ultimate URL. This makes scheduling meetings with folks outside Microsoft just that much easier.

Mobile

s620_141x228 Last and least, I've got a Windows Mobile phone and it's hooked up to Exchange and I've made the (difficult) conscious decision to only sync the "Inbox" and the "Inbox - CC" Folders to the device.

The thinking being that there's little that's totally crucial that could happen on a Mailing List (as I don't yet own any) that would require my immediate and mobile attention.

This system has worked for me so far - three-plus weeks. We'll see if it holds up under the weight of the unknown future.

I'd be interested to see if there's anything in this system that you find either interesting/compelling or totally lame. You and your 6000 inbox emails. You freak. ;)

P.S. The fire hose child is not my son.

Update: The flickr member who took the Fire House photo asked me to take it down. I replaced it with another. Bummer. Fair-use and copy-right is a confusing thing.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Typing Test twice, once with Voice Recognition

September 29, '07 Comments [13] Posted in Musings
Sponsored By

People give voice recognition a bad name. I use it all the time. I took the typing test that Daniel Moth blogged about (typing test is here) typing the way I usually do. I can't type as fast as I could as my hands are a little rough, but I do OK for old hands.

Here's my results, first try, on a Natural (split) Keyboard:

YOUR RESULTS ARE:

Number of words typed: 217
Test duration: 3 min
Speed: 72.6 words/min. (363 keystrokes/min.)
Error penalty: 27
Accuracy: 87.6%

Meh. I used to do 100, no longer. (Plus, it's a cheesy test because you have to read, manage the scrolling and spit it back out again on the keyboards. I got flummoxed with the scrolling. And I'm notoriously sloppy.

Here's the SAME test but using Vista Voice Recognition and a cheapo Logitech USB Headset Microphone (I didn't even use my $300 podcast microphone):

YOUR RESULTS ARE:

Number of words typed: 377
Test duration: 3 min
Speed: 125.6 words/min. (628 keystrokes/min.)
Error penalty: 42
Accuracy: 88.9%

Note the number of errors. I know it's not 98% like some people, but I can almost double my personal typing speed with comparable errors using Voice Recognition.

Note that I only took these tests once each, both times cold, one with hands, one with voice and  the test gave me different text each time.

You have to talk like a newscaster, but seriously, stop giving Voice Recognition a hard time. Videos of Voice Recognition trying to write Perl code are cutesy, but not reality. Yes, it'd be nice to have custom templates out there for writing code with voice (there are some) but CodeRush helps me greatly. Ultimately voice recognition is for things like blogs, email and prose.

Here's a VERY boring, very poor sound/visual quickie video of me actually taking the test. Note that I (gratuitously) stopped to correct two errors, using only voice in the middle. Otherwise I'd probably have gotten 140wpm. Sorry about the poor focus, I probably should have used Camtasia.

And yes, this blog post was written with voice recognition.  So phooey on you. I encourage you to give it a try with a nice microphone, if even only for email and blog posts.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.