我创建了一个简单的C#类,以学习如何从PowerShell脚本调用C#类。我将该项目编译为“类库”并复制到我的C:\驱动器上。
在网上搜索后,发现我们需要使用以下命令将该.DLL文件注册到PowerShell中。
PS C:\WINDOWS\system32> [Reflecion.Assembly]::LoadFile("C:\Calculator.dll")
PS C:\WINDOWS\system32> $ml = new-object Calculator()
At line:1 char:29
+ $ml = new-object Calculator()
+ ~
An expression was expected after '('.
+ CategoryInfo : ParserError: (:) [],
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedExpression
在这个命令之后我没有收到任何错误提示。 但是当我尝试像这样访问该类时,我遇到了错误。 我是PowerShell的新手。请问您有什么建议吗?谢谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
public class Calculator
{
#region "Private Data Members"
private int _operandA;
private int _operandB;
#endregion "Private Data Members"
public int OperandA
{
get { return _operandA; }
set { _operandA = value; }
}
public int OperandB
{
get { return _operandB; }
set { _operandB = value; }
}
public void AddNumbers(int a, int b)
{
int c;
c = a + b;
MessageBox.Show("The addition of 2 operands is c");
}
}
}