Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
GD0302:泛型类型参数必须用“[MustBeVariant]”属性注解
规则 ID |
GD0302 |
类别 |
用法 |
修复是破坏性的还是非破坏性的 |
破坏性的 |
默认启用 |
是 |
原因
当泛型类型参数期望 Variant 兼容类型时,为其指定泛型类型但指定的泛型类型未用 [MustBeVariant]
属性注解。
规则说明
当泛型类型参数使用 [MustBeVariant]
属性进行注解时,泛型类型必须是与 Variant 兼容的类型。当使用的类型也是泛型类型时,该泛型类型也必须使用 [MustBeVariant]
属性进行注解。例如,泛型 Godot.Collections.Array<T>
类型仅支持可以被转换为 Variant 类型的项,如果被正确注解,则可以指定泛型类型。
public void Method1<T>()
{
// T is not valid here because it may not a Variant-compatible type.
var invalidArray = new Godot.Collections.Array<T>();
}
public void Method2<[MustBeVariant] T>()
{
// T is guaranteed to be a Variant-compatible type because it's annotated
// with the [MustBeVariant] attribute, so it can be used here.
var validArray = new Godot.Collections.Array<T>();
}
如何解决违规情况
要修复违反该规则的情况,请将 [MustBeVariant]
属性添加到泛型类型,该泛型类型被用作必须与 Variant 兼容的泛型类型参数。
何时抑制警告
不要抑制该规则的警告。包含用 [MustBeVariant]
属性注解的泛型类型参数的 API 通常有此要求,因为值将被传递给引擎,如果该类型无法被编组,则会导致运行时错误。