如何使用本类的新对象在C#DLL中使用PowerShell [英] How to use New-Object of a class present in a C# DLL using PowerShell
问题描述
我在C#中的一类,例如说
I have a class in C# say for example
public class MyComputer : PSObject
{
public string UserName
{
get { return userName; }
set { userName = value; }
}
private string userName;
public string DeviceName
{
get { return deviceName; }
set { deviceName = value; }
}
public string deviceName;
}
这是从PSObject的。
我加载使用进口模块具有在PowerShell中这个代码的DLL。
然后我试图创建在PowerShell中的我的电脑类的新对象。
which is derived from PSObject. I am loading the DLL having this code in powershell using import-module. Then I tried to create a new object of the MyComputer class in PowerShell.
PS C:> $ MyCompObj =新对象我的电脑
PS C:> $MyCompObj = New-Object MyComputer
但它抛出一个错误,说让加载包含这种类型的肯定组装。
注:我能够成功调用的cmdlet这是目前在DLL
but it throws an error saying make sure assembly containing this type is loaded. Note: I am able to call Cmdlets successfully which is present in the DLL.
我不知道这是继续创造一个正确的方式新对象。
请纠正我做这项工作。
I am not sure is this the right way to go ahead on creating a new object. Please correct to me make this work.
推荐答案
首先,确保组件使用加载
First, make sure the assembly is loaded using
[System.Reflection.Assembly]::LoadFrom("C:\path-to\my\assembly.dll")
接下来,使用完全限定类名
Next, use the fully qualified class name
$MyCompObj = New-Object My.Assembly.MyComputer
这篇关于如何使用本类的新对象在C#DLL中使用PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!