title: QuantLib+SWIG+C# 4.0+Visual Studio 2010: TypeInitializationException
body: <p>I would like to add a small feature to QuantLib and compile it together with SWIG bindings to use in a C# project in Visual Studio 2010.  I am however having problems at almost every turn.  What are the steps involved in building QuantLib in Visual Studio 2010, creating the SWIG bindings, and building the C# project?</p>
  
  <ol>
  <li>I downloaded QuantLib from <a href="http://sourceforge.net/projects/quantlib/files/" rel="nofollow noreferrer">http://sourceforge.net/projects/quantlib/files/</a></li>
  <li>I downloaded Boost from <a href="http://sourceforge.net/projects/boost/files/boost/1.49.0/" rel="nofollow noreferrer">http://sourceforge.net/projects/boost/files/boost/1.49.0/</a></li>
  <li>I downloaded the QuantLib+SWIG bindings from <a href="http://sourceforge.net/projects/quantlib/files/QuantLib/1.0/bindings/QuantLib-SWIG-1.0.zip/download" rel="nofollow noreferrer">http://sourceforge.net/projects/quantlib/files/QuantLib/1.0/bindings/QuantLib-SWIG-1.0.zip/download</a></li>
  <li>I set an environment variable QL_DIR to "C:\pathToFolder\QuantLib-1.2\lib" (computer > properties > advanced system settings > advanced > environment variables)</li>
  <li>I ran the swig.cmd file located in C:\pathToFolder\QuantLib-SWIG-1.0\CSharp</li>
  <li>I opened QuantLib_vc9.sln in Visual Studio 2010</li>
  <li>For the NQuantLibc project:
  
  <ol>
  <li>I included my Boost and QuantLib directories in the header directories.</li>
  <li>I included my QuantLib/lib directory in the library directories.</li>
  <li>I successfully built the NQuantLibc project</li>
  </ol></li>
  <li>For the NQuantLib_vc9 project:
  
  <ol>
  <li>I made it dependent on the NQuantLibc project.</li>
  <li>I successfully built the NQuantLib_vc9 project.</li>
  </ol></li>
  <li>For the EquityOption_vc9 project:
  
  <ol>
  <li>I made it dependent on the NQuantLib_vc9 project.</li>
  <li>I successfully built the EquityOption_vc9 project.</li>
  </ol></li>
  <li>When I try to run the EquityOption_vc9 project, I get a TypeInitializationException, "An attempt was made to load a program with an incorrect format."</li>
  </ol>
  
  <p>Here's the full exception:</p>
  
  <pre><code>System.TypeInitializationException was unhandled
    Message=The type initializer for 'QuantLib.NQuantLibcPINVOKE' threw an exception.
    Source=NQuantLib
    TypeName=QuantLib.NQuantLibcPINVOKE
    StackTrace:
         at QuantLib.NQuantLibcPINVOKE.new_Date__SWIG_1(Int32 jarg1, Int32 jarg2, Int32 jarg3)
         at QuantLib.Date..ctor(Int32 d, Month m, Int32 y) in C:\Users\JRobinson\Desktop\QuantLib-SWIG-1.0\CSharp\csharp\Date.cs:line 48
         at EquityOptionTest.EquityOption.Main(String[] args) in C:\Users\JRobinson\Desktop\QuantLib-SWIG-1.0\CSharp\examples\EquityOption.cs:line 43
         at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException: System.TypeInitializationException
         Message=The type initializer for 'SWIGExceptionHelper' threw an exception.
         Source=NQuantLib
         TypeName=SWIGExceptionHelper
         StackTrace:
              at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..ctor()
              at QuantLib.NQuantLibcPINVOKE..cctor() in C:\Users\JRobinson\Desktop\QuantLib-SWIG-1.0\CSharp\csharp\NQuantLibcPINVOKE.cs:line 126
         InnerException: System.BadImageFormatException
              Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
              Source=NQuantLib
              StackTrace:
                   at [long string removed]
                   at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..cctor() in C:\Users\JRobinson\Desktop\QuantLib-SWIG-1.0\CSharp\csharp\NQuantLibcPINVOKE.cs:line 106
          InnerException: 
  </code></pre>
  
  <p>Note that I built everything with the Debug configuation.  I also tried this using the Release configuration.  It didn't work.</p>
  
  <p>I wish I could find a complete set of instructions detailing how to build this type of project.  I found some instructions here, <a href="https://stackoverflow.com/questions/3334011/compiling-quantlib-via-swig-for-c-sharp">Compiling Quantlib via SWIG for C#</a> but i couldn't get it to work.</p>
  
  <p>The QuantLib page contains instructions for building QuantLib in Visual Studio 2010, <a href="http://quantlib.org/install/vc10.shtml" rel="nofollow noreferrer">http://quantlib.org/install/vc10.shtml</a> but I need help creating the SWIG bindings.</p>
  
  <p>Resolver Systems has pre-built C# bindings that work for me.  <a href="http://www.resolversystems.com/products/quantlib-binary/" rel="nofollow noreferrer">http://www.resolversystems.com/products/quantlib-binary/</a> I was able to run QuantLib code in C# just fine with this package.  My problem is that I need to add a small feature to the QuantLib code for use in my C# project.  This is the reason I need to re-build QuantLib and re-create the SWIG bindings.</p>
  
  <p>I know about QLNet, the C# port of QuantLib, <a href="http://sourceforge.net/projects/qlnet/" rel="nofollow noreferrer">http://sourceforge.net/projects/qlnet/</a>, but this project is missing some pieces and I think that it is no longer being actively developed.  Specifically, I need to be able to price options that pay discrete dividends.  QLNet is missing some of the code for this.  I tried porting the necessary code from QuantLib to QLNet, but my C++ must be rusty because I was getting incorrect output.</p>
  
  <p>Note that the small feature I need to add to QuantLib is the ability to handle fractional days.  I was able to add this feature to QLNet, and it is a small feature indeed.  This tiny edit is delaying my project.  I would greatly appreciate help on this issue.</p>