[In , Out ] 在 C# 中表示什么
what does [In , Out ] indicate in C#
我在查看 StreamReader
的源代码时发现 -
public override int Read([In, Out] char[] buffer, int index, int count)
{
}
有人能解释一下第一个参数的 [In , Out]
是什么意思吗?
它们是具有[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
的属性(参数属性)
The target of an attribute is the entity to which the attribute applies. For example, an attribute may apply to a class, a particular method, or an entire assembly. By default, an attribute applies to the element that it precedes. But you can also explicitly identify, for example, whether an attribute is applied to a method, or to its parameter, or to its return value.
-引用自here
InAttribute
和 OutAttribute
定义如下:
[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,了解有关这些属性的更多详细信息 类。
我在查看 StreamReader
的源代码时发现 -
public override int Read([In, Out] char[] buffer, int index, int count)
{
}
有人能解释一下第一个参数的 [In , Out]
是什么意思吗?
它们是具有[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
The target of an attribute is the entity to which the attribute applies. For example, an attribute may apply to a class, a particular method, or an entire assembly. By default, an attribute applies to the element that it precedes. But you can also explicitly identify, for example, whether an attribute is applied to a method, or to its parameter, or to its return value.
-引用自here
InAttribute
和 OutAttribute
定义如下:
[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,了解有关这些属性的更多详细信息 类。