InAttribute 类

定义

指示数据应从调用方封送到被调用方,而不是从被调用方封送回调用方。Indicates that data should be marshaled from the caller to the callee, but not back to the caller.

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

示例

下面的示例演示如何将 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.

InAttribute 是可选的。The InAttribute 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.

不能将 InAttribute 应用到使用C#-样式 out 关键字修改的参数。You cannot apply the InAttribute to a parameter modified with the C#-styled out keyword. 若要避免将 Visual Basic 中的 In 关键字与 InAttribute的减号特性混淆,请在属性周围使用带有方括号的 <[In]> 窗体。To avoid confusing the In keyword in Visual Basic with the InAttribute, minus Attribute, use the <[In]> form with brackets around the attribute.

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.

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

构造函数

InAttribute()

初始化 InAttribute 类的新实例。Initializes a new instance of the InAttribute 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()

返回表示当前对象的字符串。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)

适用于

另请参阅