title: Powershell calling a constructor from custom DLL "Exception calling ".ctor" with "1" argument(s): "Index 1 is out of range.""
body: <p>I am hoping someone can help with this, I searched and can't seem to find an answer or even the exact same problem. </p>
  
  <p>I am loading an assembly from file (even though I have v2 Powershell add-type has some strange behaviour in this instance).</p>
  
  <pre><code>[System.Reflection.Assembly]::LoadFrom("$env:userprogile\path\to\my\dll\mydll.dll")
  </code></pre>
  
  <p><code>$taskId = 1
  $ts = New-Object mydll.myclass -ArgumentList @(,$taskId)</code></p>
  
  <p>DLL loads fine and the constructor becomes available as expected but when trying to parse arguments into it, infact it takes just one in this case it throws this error</p>
  
  <p><code>New-Object : Exception calling ".ctor" with "1" argument(s): "Index 1 is out of range."
  At line:1 char:7
  + $ts = New-Object mydll.myclass
  +       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
      + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand</code></p>
  
  <p>The unit test and test app for this works just fine when an integer of 1 is passed to it.</p>
  
  <p><code>$taskId -is [int]</code> gives true, I tried passing it these ways:
  <code>-ArgumentList 1</code> <code>-ArgumentList (1)</code> <code>-ArgumentList @(1)</code> and I am sure there were some other more ludicrous ones 
  Funnily enough all give the same error...</p>
  
  <p>Constructor signature:</p>
  
  <pre><code>public sealed class myclass: IDisposable
  {
      private readonly int _taskId = 0;
      private ScheduledTask scheduledTask = null;
  
      public myclass(int taskId)
      {
          if (taskId == 0)
              throw new ApplicationException("Task Id is not valid");
  
          _taskId = taskId;
          scheduledTask = TaskJobFactory.CreateScheduledTask(this._taskId);
      }
  
      public void RunTask()
      {
  
          DataTable dt = null;
          String csvString = String.Empty;
  
          try
          {
              dt = TaskJobFactory.CreateTableTask(scheduledTask.EnumTaskType, scheduledTask.campaignID.Value);
              csvString = Csv.DataTableToCsc(dt);
  
              if (csvString.Length &gt; 0)
              {
                  SmtpManager.SendEmailStatic(csvString, scheduledTask.Id, scheduledTask);
                  TaskHistoryFactory.UpdateTaskHistory(this._taskId, (int)ScheduledTaskStatus.Success, "Success");
              }
              else
                  TaskHistoryFactory.UpdateTaskHistory(this._taskId, (int)ScheduledTaskStatus.Unknown, String.Format("No Data for that Taks id {0}", _taskId));
          }
          catch (Exception ex)
          {
              TaskHistoryFactory.UpdateTaskHistory(this._taskId, (int)ScheduledTaskStatus.Failure, "Fail");
              ErorrLoggingFacede.LogError(ex, "TaskRunner", "RunTask");
              throw ex;
          }
          finally
          {
              dt = null;
          }
      }
  }
  </code></pre>
  
  <p>sorry about the formatting...</p>
  
  <p>Also, I tried having an empty constructor and passing the args directly to method, it worked in the test app (with a few errors since it wasn't built for that but actually completed)</p>