ReSharper 中可能存在 null 赋值。我不明白那怎么可能是空的?

发布于 2024-12-27 06:36:21 字数 378 浏览 2 评论 0原文

我收到以下警告:

可能对标有“值不能”的实体进行“空”赋值 null' 属性

我的代码:

if (verifier.GetType().GetInterface(typeof(IAsyncVerifier).FullName, true) == null)
                {
                    continue;
                }

typeof(IAsyncVerifier).FullName 部分给出警告。那怎么可以为空呢?你会如何解决它?或者也许有更好的方法来确定对象是否实现特定接口?

I get following warning:

Possible 'null' assignment to an entity marked with 'Value cannot be
null' attribute

My code:

if (verifier.GetType().GetInterface(typeof(IAsyncVerifier).FullName, true) == null)
                {
                    continue;
                }

Warning given on typeof(IAsyncVerifier).FullName part. How that can be null? How would you fix it? Or maybe there is better way to figure if object implements specific interface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2025-01-03 06:36:21

或者也许有更好的方法来确定对象是否实现特定接口?

使用 is 运算符来检查对象的类型是否派生自特定接口或类

if (verifier is IAsyncVerifier)

请参阅这篇 MSDN 文章 Type.FullName 它表示该属性不会返回任何内容。

...如果当前实例表示泛型类型参数,则什么也不做,
基于类型参数的数组类型、指针类型或 byref 类型,
或不是泛型类型定义但包含的泛型类型
未解析的类型参数。

如果您确定在访问 FullName 时这在任何情况下都不会为空,您可以使用 //resharper disable 注释禁用此警告

Or maybe there is better way to figure if object implements specific interface?

Use is operator for to check if an object's type is derived from a particular interface or class

if (verifier is IAsyncVerifier)

Look at this MSDN article Type.FullName It says that property will return nothing.

... Nothing if the current instance represents a generic type parameter,
an array type, pointer type, or byref type based on a type parameter,
or a generic type that is not a generic type definition but contains
unresolved type parameters.

If you are sure that when you access FullName this will not be null in any case you can disable this warning using //resharper disable comment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文