OutAttribute 类

定义

指示数据应从被调用方封送回调用方。Indicates that data should be marshaled from callee back to caller.

public ref class OutAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class OutAttribute : Attribute
type OutAttribute = class
    inherit Attribute
Public NotInheritable Class OutAttribute
Inherits Attribute
继承
OutAttribute
属性

示例

下面的示例演示如何将 InAttributeOutAttribute 应用于将数组作为参数传递的平台调用原型。The following example shows how to apply the InAttribute and OutAttribute to a platform invoke prototype that passes an array as a parameter. 使用方向特性组合后,调用方可以查看被调用方所做的更改。The combination of directional attributes allows the caller to see the changes made by the callee.

using namespace System;
using namespace System::Runtime::InteropServices;

// Declare a class member for each structure element.

[StructLayout(LayoutKind::Sequential, CharSet = CharSet::Unicode)]
public ref class OpenFileName
{
public:
    int structSize;
    String^ filter;
    String^ file;
    // ...
};

private ref class NativeMethods
{
public:

    // Declare a managed prototype for the unmanaged function.

    [DllImport("Comdlg32.dll", CharSet = CharSet::Unicode)]
    static bool GetOpenFileName([In, Out]OpenFileName^ ofn);
};

void main() {}
using System.Runtime.InteropServices;
using System;

// Declare a class member for each structure element.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class OpenFileName
{
    public int structSize = 0;
    public string filter = null;
    public string file = null;
    // ...
}

internal static class NativeMethods
{
    // Declare a managed prototype for the unmanaged function.
    [DllImport("Comdlg32.dll", CharSet = CharSet.Unicode)]
    internal static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
}

public class MainMethod
{
    static void Main()
    { }
}
Imports System.Runtime.InteropServices

' Declare a class member for each structure element.
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
Public Class OpenFileName

    Public structSize As Integer = 0
    Public filter As String = Nothing
    Public file As String = Nothing
    ' ...

End Class

Friend Class NativeMethods
    ' Declare managed prototype for the unmanaged function.
    Friend Declare Unicode Function GetOpenFileName Lib "Comdlg32.dll" (
       <[In](), Out()> ByVal ofn As OpenFileName) As Boolean
End Class

Public Class App
    Public Shared Sub Main()

    End Sub
End Class

注解

可以将此特性应用于参数。You can apply this attribute to parameters.

OutAttribute 是可选的。The OutAttribute is optional. 只有 COM 互操作和平台调用支持特性。The attribute is supported for COM interop and platform invoke only. 在没有显式设置的情况下,互操作封送拆收器会根据参数类型来使用规则,无论参数是通过引用传递还是按值传递,以及类型是可直接复制还是非直接复制。In the absence of explicit settings, the interop marshaler assumes rules based on the parameter type, whether the parameter is passed by reference or by value, and whether the type is blittable or non-blittable. 例如,StringBuilder 类始终被认为是 In/Out,而由值传递的字符串数组假定为 In。For example, the StringBuilder class is always assumed to be In/Out and an array of strings passed by value is assumed to be In.

仅限 Out 行为绝不是参数的默认封送处理行为。Out-only behavior is never a default marshaling behavior for parameters. 可以将 OutAttribute 应用到值,通过引用传递的引用类型更改为仅限出行为,这等效于在中C#使用 out 关键字。You can apply the OutAttribute to value and reference types passed by reference to change In/Out behavior to Out-only behavior, which is equivalent to using the out keyword in C#. 例如,默认情况下,按值传递的数组(作为仅限每个参数进行封送处理)可以改为仅供使用。For example, arrays passed by value, marshaled as In-only parameters by default, can be changed to Out-only. 但是,当类型包含所有可直接复制到一起的元素或字段时,该行为不会始终提供预期语义,因为互操作封送拆收器使用固定。However, the behavior does not always provide expected semantics when the types include all-blittable elements or fields because the interop marshaler uses pinning. 如果你不介意将数据传递给被调用方,则仅限 Out 封送处理可为非直接复制到本机的类型提供更好的性能。If you do not care about passing data into the callee, Out-only marshaling can provide better performance for non-blittable types.

InAttributeOutAttribute 应用到数组和格式的、非直接复制的类型时,将特别有用。Combining the InAttribute and OutAttribute is particularly useful when applied to arrays and formatted, non-blittable types. 调用方仅在应用这两个属性时才会看到被调用方对这些类型的更改。Callers see the changes a callee makes to these types only when you apply both attributes. 由于这些类型需要在封送处理期间复制,因此可以使用 InAttributeOutAttribute 来减少不必要的副本。Since these types require copying during marshaling, you can use InAttribute and OutAttribute to reduce unnecessary copies.

有关 OutAttribute 封送处理行为的影响的详细信息,请参阅方向属性For more information on the effect of OutAttribute on marshaling behavior, see Directional Attributes.

构造函数

OutAttribute()

初始化 OutAttribute 类的新实例。Initializes a new instance of the OutAttribute class.

属性

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于

另请参阅