开发者

.NET Reflection of all method parameters

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开发者_StackOverflow中文版 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


Try this:

var parameters = MethodBase.GetCurrentMethod().GetParameters();
foreach (ParameterInfo parameter in parameters)
{
    Console.WriteLine(parameter.Name);
}


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.


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);
        }          
    }    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜