简体   繁体   English

将第二个参数传递给函数

[英]Pass Second Parameter Into Function

I am new to c# and have inherited code that's not well commented and the original developer is no longer around.我是 c# 新手,继承了没有很好注释的代码,原始开发人员不再存在。

I need to pass a second parameter in to the function below:我需要将第二个参数传递给下面的函数:

Code Ref=1代码参考=1

public class SnmpTrapListener : IDisposable
{
 ....
 private KeyValuePair<string, object> MapVariableValues(Variable variable)
        {
            ....
            return new KeyValuePair(...);
        }    
...
}

The only places the above function is called/referenced is in the same class and one other class.上述函数被调用/引用的唯一地方是在同一个类和另一个类中。

The reference from the same class is...来自同一个类的引用是...

Code Ref=2代码参考=2

var observable = trapV1MessageHandler.ToObservableGenericTrapMessage(_Logger, MapVariableValues)
            .Merge(trapV2MessageHandler.ToObservableGenericTrapMessage(_Logger, MapVariableValues));

The call from the other class is...来自其他班级的电话是……

Code Ref=3代码参考=3

public static IObservable<GenericTrapMessage> ToObservableGenericTrapMessage(this TrapV1MessageHandler handler, Logger Logger, Func<Variable, KeyValuePair<string, object>> variableMapping)
    {
        return Observable.FromEventPattern<TrapV1MessageReceivedEventArgs>(
          h => handler.MessageReceived += h,
          h => handler.MessageReceived -= h)
          .Select(e => 
          {
            return new GenericTrapMessage
            {
                Timestamp = e.EventArgs.TrapV1Message.TimeStamp,
                Sender = e.EventArgs.Sender.ToString() + ":" + System.Environment.MachineName.ToString(),
                Type = e.EventArgs.TrapV1Message.Enterprise.ToString(),
                Variables = e.EventArgs.TrapV1Message.Variables().Select(variableMapping).ToList()
            }
         });
     ...
    }

I want to add a second string 'OID' parameter as shown below... Code Ref=4我想添加第二个字符串 'OID' 参数,如下所示......代码参考 = 4

private KeyValuePair<string, object> MapVariableValues(Variable variable, String OID)
            {
                ....
                return new KeyValuePair(...);
            }    
    ...
    }

The part I need help on is how to change the code shown in "Code Ref=2" and "Code Ref=3" above to handle the second string parameter?我需要帮助的部分是如何更改上面“Code Ref=2”和“Code Ref=3”中显示的代码以处理第二个字符串参数?

My thoughts are I just need to change one line under "Code Ref=3" passing in the new String parameter but I don't know how to do this - the line I think I need to change:我的想法是我只需要更改传递新字符串参数的“Code Ref = 3”下的一行,但我不知道如何执行此操作 - 我认为我需要更改的行:

Variables = e.EventArgs.TrapV1Message.Variables().Select(variableMapping).ToList()

You can just add a second parameter to your function as usual:您可以像往常一样向函数添加第二个参数:

private KeyValuePair<string, object> MapVariableValues(Variable variable, String OID)
{
}

Then, you need to change the parameter of the function in code sample 3 in order to accept functions with 2 input parameters, like this:然后,您需要更改代码示例 3 中函数的参数,以便接受具有 2 个输入参数的函数,如下所示:

public static IObservable<GenericTrapMessage> ToObservableGenericTrapMessage(this TrapV1MessageHandler handler, Logger Logger, Func<Variable, String, KeyValuePair<string, object>> variableMapping)
{
}

For the selection you can use a slightly different syntax, so that you can provide a additional parameter:对于选择,您可以使用稍微不同的语法,以便您可以提供附加参数:

Variables = e.EventArgs.TrapV1Message.Variables().Select(v => variableMapping(v, "yourStringParameter").ToList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM