如何在Powershell中使用扩展方法?

时间:2022-09-23 08:04:55

I have the following code:

我有以下代码:

using System
public static class IntEx
{
    /// <summary>
    /// Yields a power of the given number
    /// </summary>
    /// <param name="number">The base number</param>
    /// <param name="powerOf">the power to be applied on te base number</param>
    /// <returns>Powers applied to  the base number</returns>
    public static IEnumerable<int> ListPowersOf(this int number, int powerOf)
    {
        for (var i = number; ; i <<= powerOf)
        {
            yield return i;
        }
    }
}

I've loaded the dll in Powershell(Windows 8). I try to use it the following way:

我在Powershell中加载了dll(Windows 8)。我尝试以下方式使用它:

$test = 1.ListPowersOf(2)

Should return @(1, 2, 4, 8, 16...)

应该返回@(1,2,4,8,16 ...)

Instead it says there is no such method.

相反,它说没有这样的方法。

I tried the following:

我尝试了以下方法:

[BaseDllNamespace]::ListPowersOf(1,2)

Still nothing. I have no namespace in the IntEx class.

依然没有。我在IntEx类中没有命名空间。

How do I make it work

我如何使它工作

1 个解决方案

#1


10  

Try this:

尝试这个:

[IntEx]::ListPowersOf(1,2)

or

要么

[IntEx] | gm -Static -Type Method

to list available static methods.

列出可用的静态方法。

You can also use reflection to obtain list of exported types to see if yours is available:

您还可以使用反射来获取导出类型的列表,以查看您的类型是否可用:

[Reflection.Assembly]::LoadFile('C:path\to.dll')|select -ExpandProperty ExportedTypes

#1


10  

Try this:

尝试这个:

[IntEx]::ListPowersOf(1,2)

or

要么

[IntEx] | gm -Static -Type Method

to list available static methods.

列出可用的静态方法。

You can also use reflection to obtain list of exported types to see if yours is available:

您还可以使用反射来获取导出类型的列表,以查看您的类型是否可用:

[Reflection.Assembly]::LoadFile('C:path\to.dll')|select -ExpandProperty ExportedTypes