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

标签 c# .net powershell extension-methods

我有以下代码:

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;
        }
    }
}

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

$test = 1.ListPowersOf(2)

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

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

我尝试了以下方法:

[BaseDllNamespace]::ListPowersOf(1,2)

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

如何让它工作

最佳答案

试试这个:

[IntEx]::ListPowersOf(1,2)

[IntEx] | gm -Static -Type Method

列出可用的静态方法。

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

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

关于c# - 如何在 Powershell 中使用扩展方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25915450/

相关文章:

c# - 一种解析带有 'Flags' 的 .NET 枚举字符串或 int 值的方法

c# - 如何将 C# 委托(delegate)函数传递给托管 C++ .Dll?

c# - 导出到 Word 在本地有效,但在 IIS6 上无效

powershell - 如何以加速方式获取 PowerShell 类型加速器列表?

c# - 我如何(以及为什么)避免在这些异步方法上返回 void?

c# - 如何在 UWP 中使用 ListView 捕获点击?

c# - 如何禁用 EF 代码优先中链接表的级联删除?

powershell - Azure AD 模块和 MS Online 模块有什么区别

powershell - 在 Powershell 中使用 FtpWebRequest 使用证书访问 Filezilla 服务器

C# 泛型——为什么 lambda 有效,而简单的方法却无效?