0

我正在使用 sharpsnmp-pro 来解析 MIB 文件。

从示例中,我有以下代码:

var registry = new SimpleObjectRegistry();
var collector = new ErrorRegistry();
registry.Tree.Collector = collector;
registry.Import(Parser.Compile(GetLocation("SNMPv2-SMI.txt"), collector));
registry.Import(Parser.Compile(GetLocation("SNMPv2-CONF.txt"), collector));
registry.Import(Parser.Compile(GetLocation("SNMPv2-TC.txt"), collector));
registry.Import(Parser.Compile(GetLocation("SNMPv2-MIB.txt"), collector));
registry.Import(Parser.Compile(GetLocation("SNMPv2-TM.txt"), collector));
registry.Refresh();

// get an item with a DESCRIPTION; this works fine...
Definition item = registry.Tree.Find("SNMPv2-MIB", "sysDescr");
IEntity entity = item.DisplayEntity;
Console.WriteLine(entity.DescriptionFormatted());


// now find an item without a DESCRIPTION; this raises null reference exception
item = registry.Tree.Find("SNMPv2-MIB", "snmpTrap");
entity = item.DisplayEntity;
Console.WriteLine(entity.DescriptionFormatted());

第二次调用引发空引用异常

   Object reference not set to an instance of an object.
   at Lextm.SharpSnmpPro.Mib.ObjectIdentifierMacro.쬯()
   at Lextm.SharpSnmpPro.Mib.EntityExtensions.DescriptionFormatted(IEntity entity)
   at snmptranslate.Program.Main(String[] args) 

在调用描述以避免空引用之前,您如何确定相关实体是否具有描述(最好不要在 try..catch 中捕获)?

例如,使用 linq 拉出一个新实体以导出到 excel 电子表格中,我希望 DescriptionFormatted() 只会返回空字符串,但它失败了。

var content = entities.Select(y => new { Module = y.Module.Name, y.Name, OID = y.GetObjectIdentifier(), Description = y.DescriptionFormatted() })

在此处输入图像描述

4

0 回答 0