C#:如何对动态对象执行空检查

发布于 2024-11-29 06:30:11 字数 329 浏览 2 评论 0原文

如何对动态对象执行空检查

伪代码:

public void Main() {
    dynamic dynamicObject = 33;
    if(true) { // Arbitrary logic
        dynamicObject = null;
    }
    Method(dynamicObject);
}

public void Method(dynamic param) {
    // TODO: check if the content of 'param' is equal to null
}

How do I perform a null-check on a dynamic object?

Pseudo code:

public void Main() {
    dynamic dynamicObject = 33;
    if(true) { // Arbitrary logic
        dynamicObject = null;
    }
    Method(dynamicObject);
}

public void Method(dynamic param) {
    // TODO: check if the content of 'param' is equal to null
}

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

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

发布评论

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

评论(4

一场信仰旅途 2024-12-06 06:30:11

您是否担心动态对象可能具有自定义相等运算符,该运算符会改变 null 的解释方式?如果是这样,只需使用Object.ReferenceEquals

if (Object.ReferenceEquals(null, param)) {
  .......
}

Are you worried about the possibility the dynamic object will have a custom equality operator that will change the way the null is interpreted? If so just use Object.ReferenceEquals

if (Object.ReferenceEquals(null, param)) {
  .......
}
忆沫 2024-12-06 06:30:11

您始终可以将参数设置为对象类型,这就是编译器正在做的事情。当您键入动态参数时,它仅意味着在该方法内仅对参数的所有使用使用动态调用,但在外部它只是类型对象的签名。 DynamicObject 的更强大用法是重载您正在调用的方法,因此,如果您保持示例相同并且只有两个重载,它将根据运行时类型调用这两个方法之一,并且您始终可以添加更多以获取更多类型。

public void Main() {
    dynamic dynamicObject = 33;
    if(true) { // Arbitrary logic
        dynamicObject = null;
    }
    Method(dynamicObject);
}
public void Method(int param) {
  //don't have to check check null
  //only called if dynamicObject is an int
}
public void Method(object param) {
// will be called if dynamicObject is not an int or null
}

You can always just make the param of type object, that's what the compiler is doing. When you type a parameter dynamic it just means within that method only it is using dynamic invoke for all uses of param, but outside it's just a signature of type object. A more powerful usage of your dynamicObject would be to have overloads of the method you are calling, so if you keep your example the same and just have two overloads it would call one of the two methods based on the runtime type, and you can always add more for more types.

public void Main() {
    dynamic dynamicObject = 33;
    if(true) { // Arbitrary logic
        dynamicObject = null;
    }
    Method(dynamicObject);
}
public void Method(int param) {
  //don't have to check check null
  //only called if dynamicObject is an int
}
public void Method(object param) {
// will be called if dynamicObject is not an int or null
}
可爱咩 2024-12-06 06:30:11

您可以使用简单性:

var s = data.servicePhoneNumber is null ? "" : data.servicePhoneNumber.Value;

You can use simplicity:

var s = data.servicePhoneNumber is null ? "" : data.servicePhoneNumber.Value;
不如归去 2024-12-06 06:30:11

快速的方法可能是:

if (_owner is null)
{

}

Fast way might be:

if (_owner is null)
{

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