具有多个自定义属性的 PowerShell 方法
PowerShell methods with multiple custom attributes
我无法在 PowerShell 5.0 class 方法上使用多个自定义属性(装饰器)。在 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)
returns 以下错误:
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
只是 returns $null
.
然而,当我只使用一个自定义属性时,一切都按预期工作,并且使用上面的代码片段正确返回了属性。
如何为 PowerShell 5.0 class 方法正确定义多个自定义属性?
更新 - 仅使用一个自定义属性的行为示例
我们假设只有原始问题中的第一个自定义属性。
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"}
这让我相信一个属性确实按预期工作。
我怀疑您是否真的得到了预期的结果,即使只使用一个自定义属性也是如此。原因是方法不支持属性,至少我发现了这一点。一种方法支持可选的 return 类型,在 PowerShell 中, 恰好看起来就像一个属性 。那就是你可以定义一个方法 returning void...
MyMethod() {
$foo = 5
}
或方法 return 处理某些东西(在本例中为 int)...
[int] MyMethod() {
$foo = 5
return $foo
}
(不过,我希望有人能证明我是错的,并证明 PowerShell 类 支持属性!)
似乎 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
解决方案是在同一个命令中定义 类 A
和 B
,这会将它们放在同一个生成的程序集中:
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
其他解决方案是不在交互式会话中而是在脚本文件中定义 类。在这种情况下,混淆后的文件名将成为生成的程序集名称的一部分,因此即使属性定义在不同的脚本文件中,您也不会遇到这个问题。
我无法在 PowerShell 5.0 class 方法上使用多个自定义属性(装饰器)。在 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)
returns 以下错误:
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
只是 returns $null
.
然而,当我只使用一个自定义属性时,一切都按预期工作,并且使用上面的代码片段正确返回了属性。
如何为 PowerShell 5.0 class 方法正确定义多个自定义属性?
更新 - 仅使用一个自定义属性的行为示例
我们假设只有原始问题中的第一个自定义属性。
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"}
这让我相信一个属性确实按预期工作。
我怀疑您是否真的得到了预期的结果,即使只使用一个自定义属性也是如此。原因是方法不支持属性,至少我发现了这一点。一种方法支持可选的 return 类型,在 PowerShell 中, 恰好看起来就像一个属性 。那就是你可以定义一个方法 returning void...
MyMethod() {
$foo = 5
}
或方法 return 处理某些东西(在本例中为 int)...
[int] MyMethod() {
$foo = 5
return $foo
}
(不过,我希望有人能证明我是错的,并证明 PowerShell 类 支持属性!)
似乎 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
解决方案是在同一个命令中定义 类 A
和 B
,这会将它们放在同一个生成的程序集中:
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
其他解决方案是不在交互式会话中而是在脚本文件中定义 类。在这种情况下,混淆后的文件名将成为生成的程序集名称的一部分,因此即使属性定义在不同的脚本文件中,您也不会遇到这个问题。