如何使用 PowerShell 引用 .NET 程序集 [英] How to reference .NET assemblies using PowerShell
本文介绍了如何使用 PowerShell 引用 .NET 程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一名 C# .NET 开发人员/架构师,并且了解它使用对象(.NET 对象)而不仅仅是流/文本.
I am a C# .NET developer/architect and understand that it uses objects (.NET objects) and not just streams/text.
我希望能够使用 PowerShell 来调用我的 .NET(C# 库)程序集上的方法.
I would like to be able to use PowerShell to call methods on my .NET (C# library) assembies.
如何在 PowerShell 中引用程序集并使用该程序集?
How do I reference an assembly in PowerShell and use the assembly?
推荐答案
看看博文 从 PowerShell 加载自定义 DLL:
Take a look at the blog post Load a Custom DLL from PowerShell:
以一个简单的数学库为例.它有一个静态 Sum 方法和一个实例 Product 方法:
Take, for example, a simple math library. It has a static Sum method, and an instance Product method:
namespace MyMathLib
{
public class Methods
{
public Methods()
{
}
public static int Sum(int a, int b)
{
return a + b;
}
public int Product(int a, int b)
{
return a * b;
}
}
}
在 PowerShell 中编译并运行:
Compile and run in PowerShell:
> [Reflection.Assembly]::LoadFile("c: empMyMathLib.dll")
> [MyMathLib.Methods]::Sum(10, 2)
> $mathInstance = new-object MyMathLib.Methods
> $mathInstance.Product(10, 2)
这篇关于如何使用 PowerShell 引用 .NET 程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文