[英]How do I know that a parameter uses ref or params modifier?
盡管ParameterDefinition
不包含IsRef
或IsParams
,但很容易從其他兩個屬性中確定這兩個屬性。
當參數包含ref
修飾符時, ParameterDefinition.ParameterType.IsByReference
值為true
。 否則,即使實際參數是引用類型,它也為false
。
至於params
, CustomAttributes
集合包含一個與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.