如何使用PowerShell引用.NET程序集 [英] How to reference .NET assemblies using PowerShell

查看:184
本文介绍了如何使用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?

推荐答案

看看博客文章

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:\temp\MyMathLib.dll")
> [MyMathLib.Methods]::Sum(10, 2)

> $mathInstance = new-object MyMathLib.Methods
> $mathInstance.Product(10, 2)

这篇关于如何使用PowerShell引用.NET程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆