gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:57:09 24 4
gpt4 key购买 nike

我有以下代码:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com