[AttributeUsage(AttributeTargets.Parameter)] |
This attribute only applies in COM interop and platform invoke scenarios.
The InAttribute and OutAttribute are optional. In the absence of explicit settings, the common language runtime assumes that all arguments passed by reference are passed in / out and that all other non reference parameters are in. The only exception is the StringBuilder class, which is always assumed to be in / out. The InAttribute and OutAttribute are particularly useful when applied to arrays and formatted, non blittable types. Since these types require copying during marshaling, you can use InAttribute and OutAttribute to reduce unnecessary copies.
For more information on the effect of OutAttribute on marshaling behavior, see the conceptual topic at MSDN: directionalattributes.
using System; using System.Runtime.InteropServices; namespace MyModule { // If you do not have a type library for an interface // you can redeclare it using ComImportAttribute. // This is how the interface would look in an idl file. //[ //object, //uuid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26"), //dual, helpstring("IMyStorage Interface"), //pointer_default(unique) //] //interface IMyStorage : IDispatch //{ // [id(1)] // HRESULT GetItem([in] BSTR bstrName, [out, retval] IDispatch ** ppItem); // [id(2)] // HRESULT GetItems([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT)* pItems); // [id(3)] // HRESULT GetItemDescriptions([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT) ** ppItems); // [id(4), propget] // HRESULT get_IsEmpty([out, retval] BOOL * pfEmpty); //}; // This is the managed declaration. [ComImport] [Guid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26")] public interface IMyStorage { [DispId(1)] [return : MarshalAs( UnmanagedType.Interface )] Object GetItem( [In, MarshalAs( UnmanagedType.BStr )] String bstrName ); [DispId(2)] void GetItems( [In, MarshalAs( UnmanagedType.BStr )] String bstrLocation, [Out, MarshalAs( UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT )] out Object[] Items ); [DispId(3)] void GetItemDescriptions( [In] String bstrLocation, [In, Out, MarshalAs( UnmanagedType.SafeArray )] ref Object[] varDescriptions ); bool IsEmpty { [DispId(4)] [return : MarshalAs( UnmanagedType.VariantBool )] get; } } }
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. Initializes a new instance of the OutAttribute class. |
TypeId (inherited from System.Attribute) |
Read-only See base class member description: System.Attribute.TypeId When implemented in a derived class, gets a unique identifier for this Attribute. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Attribute) |
See base class member description: System.Attribute.GetHashCode Returns the hash code for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsDefaultAttribute (inherited from System.Attribute) |
See base class member description: System.Attribute.IsDefaultAttribute When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class. |
Match (inherited from System.Attribute) |
See base class member description: System.Attribute.Match When overridden in a derived class, returns a value indicating whether this instance equals a specified object. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public OutAttribute(); |
public virtual object TypeId {get;}
|
~OutAttribute(); |
public override int GetHashCode(); |
public Type GetType(); |
public virtual bool IsDefaultAttribute(); |
The implementation of this method in a derived class compares the value of this instance to a standard, default value obtained by some means, then returns a Boolean value that indicates whether the value of this instance is equal to the standard. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.
obj
protected object MemberwiseClone(); |
public virtual string ToString(); |