title: How to resolve the "Specified cast is not valid." error
body: <p>I have simple project, I dynamically execute a <code>string value</code> with <code>C# compiler</code>.</p>
  
  <p>follow code:</p>
  
  <pre><code>private void btntest_Click(object sender, EventArgs e)
          {
              string code = @"
  using System;
  using System.Text;
  using System.Data;
  using System.Reflection;
  using System.ComponentModel;
  
  namespace myNameSpace
  {
      public class DynamicClass
      {
          public bool myFunction(DataRow dr)
          {
              if (dr == null)
                  return false;
  
              if (dr.Table.Columns.Contains(""Col1"")  &amp;&amp; (bool)dr[""Col1""] == true)
                  return true;
              else
                  return false;
          }
      }
  }
  ";
  
              Assembly asm = BuildAssembly(code);
              object instance = null;
              Type type = null;
              instance = asm.CreateInstance("myNameSpace.DynamicClass");
              type = instance.GetType();
              DataTable dt = new DataTable();
              dt.Columns.Add("Col1");
              DataRow dr = dt.NewRow();
              dr[0] = true;
  
              MethodInfo method = type.GetMethod("myFunction");
              object returnVal = method.Invoke(instance, new object[] { dr });
              txtError.Text = returnVal.ToString();
  
          }
  
          Assembly BuildAssembly(string code)
          {
              CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
              CompilerParameters compilerparams = new CompilerParameters();
              compilerparams.GenerateExecutable = false;
              compilerparams.GenerateInMemory = false; 
  
              compilerparams.ReferencedAssemblies.Add("mscorlib.dll");
              compilerparams.ReferencedAssemblies.Add("System.dll");
              compilerparams.ReferencedAssemblies.Add("System.Data.dll");
              compilerparams.ReferencedAssemblies.Add("System.Xml.dll");
  
              CompilerResults results = codeProvider.CompileAssemblyFromSource(compilerparams, code);
              if (results.Errors.HasErrors)
              {
                  StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
                  foreach (CompilerError error in results.Errors)
                  {
                      errors.AppendFormat("Line {0},{1}\t: {2}\n",
                             error.Line, error.Column, error.ErrorText);
                  }
                  throw new Exception(errors.ToString());
              }
              else
              {
                  return results.CompiledAssembly;
              }
          }
  </code></pre>
  
  <p>when I run the project,I get the following error:</p>
  
  <blockquote>
    <p>Exception has been thrown by the target of an invocation. </p>
    
    <p>Inner Exception </p>
    
    <p>InvalidCastException: Specified cast is not valid.</p>
  </blockquote>
  
  <p>if I remove the <code>(bool)dr[""Col1""] == true</code> section , it's working well.</p>
  
  <p>what <code>assembly</code> or <code>namespace</code> should be added?</p>
  
  <p>How to fix this error?</p>
  
  <p>Any help is much appreciated.</p>