gpt4 book ai didi

c# - 具有多个自定义属性的 PowerShell 方法

转载 作者:行者123 更新时间:2023-11-30 12:22:54 25 4
gpt4 key购买 nike

我无法在 PowerShell 5.0 类方法上使用多个自定义属性(装饰器)。在 C# 中,我可以执行以下操作:

public class SomeAttribute : Attribute {
public string Text { get; set; }
}

public class OtherAttribute : Attribute {
public string Text { get; set; }
}

public class MyClass {
[SomeAttribute(Text = "sometext")]
[OtherAttribute(Text = "othertext")]
public void MyMethod() {
// ...
}
}

然后在别的地方调用

object[] customAttributes = typeof(MyClass).GetMethod("MyMethod").GetCustomAttributes(false);

它毫无问题地为我提供了与该方法关联的所有自定义属性的数组。

在 PowerShell 5.0 中,我尝试类比地使用:

class SomeAttribute : Attribute {
[string]$Text
}

class OtherAttribute : Attribute {
[string]$Text
}

class MyClass {
[SomeAttribute(Text="sometext")]
[OtherAttribute(Text="othertext")]
MyMethod() {
# ...
}
}

这似乎是正确的语法,因为 PowerShell 乐于接受它。但是通过

列出属性
[MyClass].GetMethod("MyMethod").GetCustomAttributes($false)

返回以下错误:

Exception calling "GetCustomAttributes" with "1" argument(s): "Could not load type 'OtherAttribute' from assembly '⧹powershell, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'."
At line:1 char:1
+ [MyClass].GetMethod("MyMethod").GetCustomAttributes($false)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TypeLoadException

[MyClass].GetMethod("MyMethod").CustomAttributes

简单地返回$null

但是,当我只使用一个自定义属性时,一切都按预期工作,并且使用上面的代码片段正确返回了该属性。
如何为 PowerShell 5.0 类方法正确定义多个自定义属性?

更新 - 仅使用一个自定义属性的行为示例

让我们只假设原始问题中的第一个自定义属性。

class SomeAttribute : Attribute {
[string]$Text
}

class MyClass {
[SomeAttribute(Text="sometext")]
MyMethod() {
# ...
}
}

然后

[MyClass].GetMethod("MyMethod").GetCustomAttributes($false)

给出以下输出

Text     TypeId
---- ------
sometext SomeAttribute

[MyClass].GetMethod("MyMethod").CustomAttributes

给出以下输出

AttributeType Constructor  ConstructorArguments NamedArguments
------------- ----------- -------------------- --------------
SomeAttribute Void .ctor() {} {Text = "sometext"}

这让我相信一个属性确实按预期工作。

最佳答案

似乎 PowerShell(.NET,恕我直言)在引用两个具有相同全名的不同程序集时遇到了麻烦(告诉我,这在 .NET 中是不可能的)。

PS> class A {}
PS> class B {}
PS> [A].Assembly -eq [B].Assembly
False
PS> [A].Assembly.FullName -eq [B].Assembly.FullName
True
PS> class CA { [A] $A }; class CB { [B] $B }
PS> [CA]::new().A #work fine
PS> [CB]::new().B #error

解决方案是在同一个命令中定义类 AB,这会将它们放在同一个生成的程序集中:

PS> class A {}; class B {}
PS> [A].Assembly -eq [B].Assembly
True
PS> class CA { [A] $A }; class CB { [B] $B }
PS> [CA]::new().A #work fine
PS> [CB]::new().B #work fine

所以,这对我来说很好:

PS> class SomeAttribute : Attribute {
>>> [string]$Text
>>> }
>>> class OtherAttribute : Attribute {
>>> [string]$Text
>>> }
PS> class MyClass {
>>> [SomeAttribute(Text="sometext")]
>>> [OtherAttribute(Text="othertext")]
>>> MyMethod() {
>>> # ...
>>> }
>>> }
PS> [MyClass].GetMethod("MyMethod").GetCustomAttributes($false)

Text TypeId
---- ------
sometext SomeAttribute
othertext OtherAttribute

其他解决方案是不在交互式 session 中而是在脚本文件中定义类。在这种情况下,混淆后的文件名将成为生成的程序集名称的一部分,因此即使属性定义在不同的脚本文件中,您也不会遇到这个问题。

关于c# - 具有多个自定义属性的 PowerShell 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39026482/

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