views:

218

answers:

3

Is it possible to get the parameter name (where I have parmName below)? Or perhaps in the MSIL code there are only relative positions, no absolute parm names?

I have an unusualy case using HIP within Microsoft Host Integration Server. When fields are NULL and the error goes back to CICS (on the mainframe), the error is "A CALL TO VERIFYINVOKEPARAMS FAILED". I have hard-coded a solution, but was trying to make a general solution that would work for any HIP subroutine.

Thanks,

Neal Walters

    // check for null values in any of the parameters 
    MethodBase method = MethodBase.GetCurrentMethod();
    //string key = method.Name + "(";
    for (int i = 0; i < method.GetParameters().Length; i++)
    {
        if (method.GetParameters().GetValue(i).GetType() == typeof(String))
        {
            if (method.GetParameters().GetValue(i) == null)
            {
                string parmName = " Parm #" + i; 
                msg = "Value of return variable " + parmName + " is null (should be blanks)";
                System.Diagnostics.EventLog.WriteEntry("LOGGER", msg, 
                    System.Diagnostics.EventLogEntryType.Error);

            }
        }
    }

Extra info: I'm calling a BizTalk Orch published as a WCF web service. When it gets errors, some fields are not serialzied back to the above program. This is how the values got to be NULL in the first place. But the CICS/application that is calling my HIS/HIP program doesn't like nulls.

+1  A: 

Try this:

var parameters = MethodBase.GetCurrentMethod().GetParameters();
foreach (ParameterInfo parameter in parameters)
{
    Console.WriteLine(parameter.Name);
}
Rubens Farias
Works great - I guess the way to do it by subscript would be this: MethodBase.GetCurrentMethod().GetParameters()[0].Name
NealWalters
Getting the value is the next trick - I'll post in a new question.
NealWalters
A: 
public struct Argument
{
    public String Name;
    public String Value; 
}

public void Method(Argument[] arguments)
{
    for (int i = 0; i < arguments.Length; i++)
    {
        var v = arguments[i].Value;
        if (v == null)
        {
           var message = "Param " + arguments[i].Name + " cannot be null.";
           EventLog.WriteEntry("LOGGER", message, EventLogEntryType.Error);
        }          
    }    
}
ChaosPandion
But I have 26 (or potentially more parms).
NealWalters
The parms are being passed by ref, and I'm editing them before they go out, not when they come in.
NealWalters
A: 

Hi,

I think this line doesn't do what you are thinking it will do.


if (method.GetParameters().GetValue(i) == null)

GetValue will not get the value of the parameter passed to the method. GetValue(i) is a method on the Array class which will simply return the value of the i'th index into the array, which is a ParameterInfo. The ParameterInfo class does not have any information about the value the method was called with. I doubt it will ever return null.

John Buchanan
Yea - that's also what I saw. Seems like there should be some way to get to the value though... I was surprised the index wasn't the other way around "GetParameter[i].getValue()".
NealWalters
@NealWalters have you looked into aspect-oriented? I made a prototype a while back in C#, using ContextBoundObject to inspect the values of a method call at runtime. I don't know of any other way.
John Buchanan
Exactly I bet he is looking for something similar to the JavaScript arguments array.
ChaosPandion
Thanks, I'll ask that as another question.
NealWalters