2

我被困住了,假设我有一个方法:

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
}

如何做到这一点?我知道的一种方式

 MethodInfo.GetCurrentMethod().GetParameters()

但是更具体的 out 参数怎么样?

4

3 回答 3

4
MethodInfo.GetCurrentMethod().GetParameters().Where(p => p.IsOut)
于 2014-02-02T02:46:07.307 回答
3

MethodInfo.GetCurrentMethod().GetParameters() 返回一个 ParameterInfo 数组,ParameterInfo 有一个属性 Attributes - 查看它以查找参数是否存在。

于 2014-02-02T02:44:48.363 回答
3
// 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参考

此方法取决于可选的元数据标志。该标志可以由编译器插入,但编译器没有义务这样做。

此方法利用 ParameterAttributes 枚举器的 Out 标志。

于 2014-02-02T02:46:26.590 回答