2

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 unusual 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.

NOTE: The follow-up question on how to get the values is here: C# Getting value of parms using reflection

NealWalters
  • 17,197
  • 42
  • 141
  • 251
  • 2
    Sorry to armchair architect, but I see what you're trying to do there, and I'm not sure it falls under the realm of best practices... but I could be wrong. it's probably better just to do != null checks and throw ArgumentNullExceptions, then handle logging in your exception handler. At least, that's the standard I've seen. *shrug* – Ben Lesh Dec 07 '09 at 19:52
  • In this case it may be better to ignore the nulls. What good will throwing the exception do? He would have to change the calling code which could conceivably be a lot. – ChaosPandion Dec 07 '09 at 19:57
  • Due to the nature of HIS/HIP, I have 26 parms, each passed by ref, representing a COBOL structure. That's a lot of if statements, and we would have to do it again in any future HIP program. HIP translated the .NET fields to their corresponding COBOL formats (PIC S9(4) COMP etc...) – NealWalters Dec 07 '09 at 20:23
  • Also - the calling program - believe it or not - is mainframe CICS via HIP. It cannot handle exceptions. It is actually getting an exception (mentioned in my post) that indicates that I'm passing it a NULL field. – NealWalters Dec 07 '09 at 20:28

3 Answers3

3

Try this:

var parameters = MethodBase.GetCurrentMethod().GetParameters();
foreach (ParameterInfo parameter in parameters)
{
    Console.WriteLine(parameter.Name);
}
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
1

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
  • 5,054
  • 2
  • 19
  • 17
  • 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 Dec 07 '09 at 20:36
  • @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 Dec 07 '09 at 20:58
  • Exactly I bet he is looking for something similar to the JavaScript arguments array. – ChaosPandion Dec 07 '09 at 21:57
  • Thanks, I'll ask that as another question. – NealWalters Dec 08 '09 at 14:27
0
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
  • 77,506
  • 18
  • 119
  • 157