سؤال

I am stuck on, Suppose I am having a method:

public void InsertEmployee(Employee _employee, out Guid _employeeId)
{
    //My code
    //Current execution is here. 
    //And here I need a list of 'out' parameters of 'InsertEmployee' Method
}

How to achieve this? one way i know

 MethodInfo.GetCurrentMethod().GetParameters()

But how about more specific to out parameter only?

هل كانت مفيدة؟

المحلول 2

// boilerplate (paste into LINQPad)
void Main()
{
     int bar;
     MethodWithParameters(1, out bar);
     Console.WriteLine( bar );
}

void MethodWithParameters( int foo, out int bar ){

bar = 123;
var parameters = MethodInfo.GetCurrentMethod().GetParameters();

foreach( var p in parameters )
{
    if( p.IsOut ) // the important part
    {
        Console.WriteLine( p.Name + " is an out parameter." );
    }
  }
}

IsOut Reference

This method depends on an optional metadata flag. This flag can be inserted by compilers, but the compilers are not obligated to do so.

This method utilizes the Out flag of the ParameterAttributes enumerator.

نصائح أخرى

MethodInfo.GetCurrentMethod().GetParameters().Where(p => p.IsOut)

MethodInfo.GetCurrentMethod().GetParameters() return an array of ParameterInfo, ParameterInfo has a property Attributes - look into that to find if the parameter is out.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top