I am hoping someone can help with this, I searched and can't seem to find an answer or even the exact same problem.
I am loading an assembly from file (even though I have v2 Powershell add-type has some strange behaviour in this instance).
[System.Reflection.Assembly]::LoadFrom("$env:userprogile\path\to\my\dll\mydll.dll")
$taskId = 1
$ts = New-Object mydll.myclass -ArgumentList @(,$taskId)
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
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
The unit test and test app for this works just fine when an integer of 1 is passed to it.
$taskId -is [int]
gives true, I tried passing it these ways:
-ArgumentList 1
-ArgumentList (1)
-ArgumentList @(1)
and I am sure there were some other more ludicrous ones
Funnily enough all give the same error...
Constructor signature:
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 > 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;
}
}
}
sorry about the formatting...
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)