C# 这个操作符是什么&引用;

C# 这个操作符是什么&引用;,c#,dll,syntax,nullreferenceexception,C#,Dll,Syntax,Nullreferenceexception,我正在进行升级项目,最近开始遇到DLL升级版本的问题。我反编译了原始dll,发现以下if语句: if (fieldConfiguration == null && Context.ContentDatabase != null) { Item obj = Context.ContentDatabase.SelectSingleItem( string.Format("//*[@@templateid='{0}' and @@key='{1}']",

我正在进行升级项目,最近开始遇到DLL升级版本的问题。我反编译了原始dll,发现以下if语句:

if (fieldConfiguration == null && Context.ContentDatabase != null)
{
    Item obj = Context.ContentDatabase.SelectSingleItem(
        string.Format("//*[@@templateid='{0}' and @@key='{1}']", 
            (object) TemplateIDs.TemplateField, (object) fieldName));
}
然后我反编译了DLL的升级版本,声明如下:

if (fieldConfiguration == null && (Context.ContentDatabase ?? Context.Database) != null)
{
    Item obj = Context.ContentDatabase.SelectSingleItem(
        string.Format("//*[@@templateid='{0}' and @@key='{1}']", 
            (object) TemplateIDs.TemplateField, (object) fieldName));
}
我能够通过使用dotPeek反编译DLL并使用dotPeek符号服务器功能逐步完成代码。我可以看到,使用升级DLL时代码失败,因为Context.ContentDatabase为null。我不明白的是,双三元运算符是如何计算的。有人能帮我澄清一下那里发生了什么事吗?似乎此程序集的创建者希望对Context.ContentDatabase进行空检查,但可能犯了错误。谢谢你的帮助

(Context.ContentDatabase??Context.Database)
表达式如果Context.ContentDatabase不为空,则最终结果为
Context.ContentDatabase
,否则为
Context.Database
。null coalesce操作符是简化
null
检查的一个步骤


医生:

嗯,是的,这看起来像是个错误。代码正在查看
Context.ContentDatabase
Context.Database
是否为
null
,然后继续使用前者,即使它是
null

代码应该如下所示:

var database = Context.ContentDatabase ?? Context.Database;

if (fieldConfiguration == null && database != null)
{
    Item obj = database.SelectSingleItem(
        string.Format("//*[@@templateid='{0}' and @@key='{1}']", 
            (object) TemplateIDs.TemplateField, (object) fieldName));
}
其中,它使用null合并运算符将数据库存储在单独的变量中,然后对该变量进行操作(如果它不是
null


因此,您应该与提供此库的团队联系,并向他们提交一个bug。

假设Context.ContentDatabase和Context.Database是相同的类型。下面的代码应该可以工作

var contentDatabase = Context.ContentDatabase ?? Context.Database;
if (fieldConfiguration == null && contentDatabase != null)
{
Item obj = contentDatabase.SelectSingleItem(
    string.Format("//*[@@templateid='{0}' and @@key='{1}']", 
        (object) TemplateIDs.TemplateField, (object) fieldName));
}

你把二进制零合并运算符(
)称为“三元”吗?是的,不知道这就是所谓的hahaSo“双三元”意思是“两个问号”?一元表示某事物中有一个,二元表示某事物中的两个,三元表示三个,四元应该很明显。是的,那个双问号操作符:Context.ContentDatabase??数据库“三元运算符”在C#之类的C语言中是
a吗?b:c
——条件运算符,有三个操作数,因此得名。谢谢!根据我的调试,我认为这是正在进行的,但在发出警告之前,我需要第二个意见。看起来像是一个有经验的人,也是一个新的操作员!这是在一个第三方dll中,所以我假设他对它没有管理权。尽管在反编译+修改后编译程序集是可能的,但我以前也做过。但如果你不得不发布软件或侵犯任何权利,那可能是一条死胡同。在这种情况下,我没有访问源代码的权限,但当我发出红旗时,我肯定可以发送这些。谢谢