繁体   English   中英

在 Powershell 中,如何使用字节数组作为我的 C# 通用 class 之一的密钥类型?

[英]In Powershell, how do I make use of byte array as key type to one of my C# Generic class please?

我在无法修改的外部 DLL 中有 C# class。 库中的两个类定义如下,我需要在 Powershell 脚本中使用它们。

public class MyCsharpClass<TKey, TValue> : ISomeBaseClass<TKey, TValue>
{
    public MyCsharpClass(string path, ISomeBaseTranslator<TKey, TValue> translator)
    {
        ...
    }
}
...
...
public class MyByteTranslatorCSharpClass: ISomeBaseTranslator<byte[], byte[]>
{
    ...
}

我想在 Powershell 脚本中创建 MyCsharpClass 的实例,如下所示:

$MyType = [MyCsharpClass[byte[],byte[]]]::new($Path, [MyByteTranslatorCSharpClass]::new())

但是上面的行失败并出现以下错误。 我可以在 PS ISE 命令 window 中输入类型名称并得到相同的错误。

PS C:\> [MyCsharpClass[byte[],byte[]]]
At line:1 char:30
+ [MyCsharpClass[byte[],byte[]]]
+                                                                     ~
Unexpected token ']' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

当我只对 TKey 使用字节类型时,虽然向我显示了 class 的类型信息,但下面的行工作正常:

PS C:\> [MyCsharpClass[byte,byte[]]]

IsPublic IsSerial Name                                     BaseType                                                                                                                                         
-------- -------- ----                                     --------                                                                                                                                         
True     False    MyCsharpClass                           System.Object 

似乎我的 byte[] 键类型的第一个右括号 ']' 混淆了 Powershell 类型定义正在关闭,因此当它在表达式末尾观察到 Unexpected token ']' 时会引发错误。

我还尝试将 byte[] 类型替换为 System.Collections.Generic.List 类型,该类型现在可以很好地声明类型。

PS C:\> MyCsharpClass[System.Collections.Generic.List[System.Byte],System.Collections.Generic.List[System.Byte]]]

IsPublic IsSerial Name                                     BaseType                                                                                                                                         
-------- -------- ----                                     --------                                                                                                                                         
True     False    MyCsharpClass                           System.Object 

但是,这次构造函数调用失败,同时将MyByteTranslatorCSharpClass的实例作为构造函数中的第二个参数传递(在下一行的末尾)。

$MyType = [MyCsharpClass[System.Collections.Generic.List[System.Byte],System.Collections.Generic.List[System.Byte]]]::new($Path, [MyByteTranslatorCSharpClass]::new())

这给出了“System.Management.Automation.MethodException:无法转换参数“translator”的异常,值为:“MyByteTranslatorCSharpClass”,对于“.ctor”,输入“ISomeBaseTranslator[System.Collections.Generic.List 1[System.Byte],System.Collections.Generic.List 1[System.Byte]]"

请让我知道在这种情况下,如何在 Powershell 中为 TKey 和 TValue 通用 arguments 创建具有byte[]类型的 MyCsharpClass 实例。 请注意,我既不能更改 DLL 方面,也没有它的来源。

这似乎是一个类型名称解析错误。

幸运的是,您可以通过使用额外的一对方括号 ( [] ) “限定”类型参数文字来轻松解决此问题:

[MyCsharpClass[[byte[]],[byte[]]]]::new(...)

...或通过直接使用反射构造具体类型:

$myType = [MyCsharpClass`2].MakeGenericType(@([byte[]],[byte[]]))

$myType::new(...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM