【问题标题】:SNMP#Net KeyValuePair error?SNMP#Net KeyValuePair 错误?
【发布时间】:2015-07-27 18:24:45
【问题描述】:

我正在使用这里的简单示例http://www.snmpsharpnet.com/?page_id=102

并具有以下代码:

using SnmpSharpNet;
using System;
using System.Collections.Generic;

namespace SNMP
{
    class Program
    {
        static void Main(string[] args)
        {
            string host = "10.65.10.17";
            string community = "public";
            SimpleSnmp snmp = new SimpleSnmp(host, community);

            if (!snmp.Valid)
            {
                Console.WriteLine("SNMP agent host name/ip address is invalid.");
                return;
            }
            Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver1,
                                                      new string[] { ".1.3.6.1.4.1.11.2.3.9.4.2.1.1.16.1.1.1.26.0" });
            if (result == null)
            {
                Console.WriteLine("No results received.");
                return;
            }

            foreach (KeyValuePair kvp in result)
            {
                Console.WriteLine("{0}: {1} {2}", kvp.Key.ToString(),
                                      SnmpConstants.GetTypeName(kvp.Value.Type),
                                     kvp.Value.ToString());
            }
        }
    }
}   

当我尝试构建时,我得到了

Using the generic type 'System.Collections.Generic.KeyValuePair<TKey,TValue>' requires 2 type arguments 

我在某处缺少参考吗?或者该示例是否需要针对 .net 4.5 和 VS2013 进行调整?

谢谢

【问题讨论】:

    标签: c# snmp


    【解决方案1】:

    您可以提供类型参数:

    foreach (KeyValuePair<Oid,AsnType> kvp in result)
    {
       Console.WriteLine("{0}: {1} {2}", kvp.Key.ToString(),
                    SnmpConstants.GetTypeName(kvp.Value.Type),
                    kvp.Value.ToString());
    }
    

    或者让编译器进行类型推断:

    foreach (var kvp in result)
    {
       Console.WriteLine("{0}: {1} {2}", kvp.Key.ToString(),
                    SnmpConstants.GetTypeName(kvp.Value.Type),
                    kvp.Value.ToString());
    }
    

    【讨论】:

    • 酷总是一个简单的答案,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    相关资源
    最近更新 更多