我正在從 stackoverflow 上的用戶那里改編這個 snmp 程式。我在 Visual Studio 2022 中創建了一個新專案,添加了所有依賴項。
當我編譯時,我收到 6 條 IDE0090 訊息,但我看不出問題出在哪里。我已經設定LangVersion為 10。
https://i.stack.imgur.com/BF5P1.jpg
任何人都可以看到問題是什么?
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using SnmpSharpNet;
namespace Exemple2
{
class Program
{
static void Main(string[] args)
{
/* Get an SNMP Object
*/
SimpleSnmp snmpVerb = new SimpleSnmp("192.168.1.121", 161, "public");
if (!snmpVerb.Valid)
{
Console.WriteLine("Seems that IP or comunauty is not cool");
return;
}
/* Sample of simple Get usage on ifSpeed on interface 10
* TODO : for sure you have to detect the right interface
*/
Oid oidifSpeed = new Oid(".1.3.6.1.2.1.2.2.1.5.10");
/* Getting ifSpeed
*/
Dictionary<Oid, AsnType> snmpDataS = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifSpeed.ToString() });
if (snmpDataS != null)
Console.WriteLine("Interface speed \"{0}\" : {1}", oidifSpeed.ToString(), snmpDataS[oidifSpeed].ToString());
else
Console.WriteLine("Not Glop!");
/* Sample of simple Get usage on ifInOctets on interface 10
* TODO : for sure you have to detect the right interface
*/
Oid oidifInOctets = new Oid(".1.3.6.1.2.1.2.2.1.10.10");
Dictionary<Oid, AsnType> snmpData1;
/* Getting it for the first time
*/
snmpData1 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
if (snmpData1 != null)
Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData1[oidifInOctets].ToString());
else
Console.WriteLine("Not Glop!");
int missed = 0;
while (true)
{
if (missed == 0)
{
/* When you detect a non refesh data, keep the last one
*/
snmpData1 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
if (snmpData1 != null)
Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData1[oidifInOctets].ToString());
else
Console.WriteLine("Not Glop!");
}
/* Some Wait (less aproximative)
*/
int duration = 5;
System.Threading.Thread.Sleep(duration * 1000); // duration seconds
/* Getting it for the Second time
*/
Dictionary<Oid, AsnType> snmpData2 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
if (snmpData2 != null)
Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData2[oidifInOctets].ToString());
else
Console.WriteLine("Not Glop!");
Counter32 I1 = new Counter32();
I1.Set(snmpData1[oidifInOctets]);
Counter32 I2 = new Counter32();
I2.Set(snmpData2[oidifInOctets]);
Counter32 speed = new Counter32();
speed.Set(snmpDataS[oidifSpeed]);
if (I2.Value == I1.Value)
{
missed = 1;
continue;
}
decimal bandWithUsage = (((decimal)(I2.Value - I1.Value) * 8) * 100) / (speed * duration * (1 missed));
Console.WriteLine("BandWith usage : {0}%", bandWithUsage);
missed = 0;
}
}
}
}
uj5u.com熱心網友回復:
編譯器建議,因為您明確指定欄位型別而不是 using var snmpVerb = ...,所以您不需要建構式名稱并使用以下縮短的語法:
SimpleSnmp snmpVerb = new ("192.168.1.121", 161, "public");
如果您右鍵單擊對SimnpleSnmp建構式的呼叫并選擇 Quick Actions and Refactorings,它將為您提供一些建議,這就是其中之一。
uj5u.com熱心網友回復:
正如IDE0090 檔案所示,您可以使用 C# 9目標型別的新運算式并簡化實體創建。例如來自:
SimpleSnmp snmpVerb = new SimpleSnmp("192.168.1.121", 161, "public");
Oid oidifSpeed = new Oid(".1.3.6.1.2.1.2.2.1.5.10");
到:
SimpleSnmp snmpVerb = new("192.168.1.121", 161, "public");
Oid oidifSpeed = new(".1.3.6.1.2.1.2.2.1.5.10");
或者只是使用var:
var snmpVerb = new SimpleSnmp("192.168.1.121", 161, "public");
var oidifSpeed = new Oid(".1.3.6.1.2.1.2.2.1.5.10");
請注意,這些不是錯誤,而是資訊性訊息,它們不會影響程式的正確性/編譯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480212.html
