【问题标题】:what does [In , Out ] indicate in C# [duplicate][In , Out ] 在 C# 中表示什么 [重复]
【发布时间】:2015-12-03 06:09:11
【问题描述】:

我正在浏览我找到的StreamReader 的源代码 -

public override int Read([In, Out] char[] buffer, int index, int count)
{

}

有人能解释一下第一个参数的[In , Out] 是什么意思吗?

【问题讨论】:

    标签: c# streamreader


    【解决方案1】:

    它们是具有[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]的属性(参数属性)

    属性的目标是属性应用到的实体。例如,属性可以应用于类、特定方法或整个程序集。默认情况下,属性应用于它之前的元素。但您也可以显式识别,例如,属性是应用于方法,还是应用于其参数,或应用于其返回值。

    -引用自here

    InAttributeOutAttribute 的定义如下:

    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
    [System.Runtime.InteropServices.ComVisible(true)]
    public sealed class InAttribute : Attribute
    {
        internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
        {
            return parameter.IsIn ? new InAttribute() : null;
        }
        internal static bool IsDefined(RuntimeParameterInfo parameter)
        {
            return parameter.IsIn;
        }
    
        public InAttribute()
        {
        }
    }
    
    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
    [System.Runtime.InteropServices.ComVisible(true)]
    public sealed class OutAttribute : Attribute
    {
        internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
        {
            return parameter.IsOut ? new OutAttribute() : null;
        }
        internal static bool IsDefined(RuntimeParameterInfo parameter)
        {
            return parameter.IsOut;
        }
    
        public OutAttribute()
        {
        }
    }
    

    查看referencesource.microsoft.com 上的here,了解有关这些属性类的更多详细信息。

    【讨论】:

    • Here 也是来自 msdn 的文章,用于详细研究。
    • 您应该将该文章添加到您的答案中,并可能提供其中一些详细信息,以防这些链接在以后无法正常工作,就像它们有时那样。
    猜你喜欢
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2021-08-03
    • 2023-03-12
    • 1970-01-01
    • 2016-02-22
    • 2021-02-21
    相关资源
    最近更新 更多