簡體   English   中英

我怎么知道參數使用ref或params修飾符?

[英]How do I know that a parameter uses ref or params modifier?

在Mono.Cecil中, out參數的ParameterDefinition的屬性IsOut設置為true

refparams呢? 如何通過ParameterDefinition確定那些修飾符之一用於方法參數?

盡管ParameterDefinition不包含IsRefIsParams ,但很容易從其他兩個屬性中確定這兩個屬性。

當參數包含ref修飾符時, ParameterDefinition.ParameterType.IsByReference值為true 否則,即使實際參數是引用類型,它也為false

至於paramsCustomAttributes集合包含一個與System.ParamArrayAttribute相對應的元素。

以下代碼說明了如何確定四種狀態:

using System;
using System.Linq;
using Mono.Cecil;

...

if (definition.IsOut)
{
    // There is an `out` modifier.
}
else if (definition.ParameterType.IsByReference)
{
    // There is a `ref` modifier.
}
else if (definition.CustomAttributes.Any(attribute => 
    attribute.AttributeType.FullName == typeof(ParamArrayAttribute).FullName))
{
    // There is a `params` modifier.
}
else
{
    // There are no modifiers.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM