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)]
public sealed class OutAttribute : 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 Interop 和平台叫用的屬性。The attribute is supported for COM interop and platform invoke only. 在沒有明確設定的情況下,interop 封送處理器會根據參數類型來假設規則、參數是以傳址方式或依值傳遞,以及該類型是以全像是或非直接的方式。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,而以傳值方式傳遞的字串陣列則假設為。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 套用至以傳址方式傳遞的值和參考型別,將 In/Out 行為變更為僅限 Out 行為,這相當於使用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#. 例如,以傳值方式傳遞的陣列(根據預設,封送處理為僅限使用的參數)可以變更為僅限 Out。For example, arrays passed by value, marshaled as In-only parameters by default, can be changed to Out-only. 不過,此行為不一定會在類型包含所有可直接執行的元素或欄位時提供預期的語義,因為 interop 封送處理器會使用釘選。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 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。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)

適用於

另請參閱