Redigera

Dela via


UnmanagedCallersOnlyAttribute Class

Definition

Any method marked with UnmanagedCallersOnlyAttribute can be directly called from native code. The function token can be loaded to a local variable using the address-of operator in C# and passed as a callback to a native method.

public ref class UnmanagedCallersOnlyAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
public sealed class UnmanagedCallersOnlyAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
type UnmanagedCallersOnlyAttribute = class
    inherit Attribute
Public NotInheritable Class UnmanagedCallersOnlyAttribute
Inherits Attribute
Inheritance
UnmanagedCallersOnlyAttribute
Attributes

Examples

The following example demonstrates passing a callback marked UnmanagedCallersOnlyAttribute to a native function.

[DllImport("NativeLibrary")]
internal static extern unsafe void NativeFunctionWithCallback(delegate* unmanaged[Cdecl]<int, int> callback);

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static int DoubleInt(int i) => i * 2;

public static unsafe void PassCallbackToNativeFunction()
{
    NativeFunctionWithCallback(&DoubleInt);
}

Remarks

Methods marked with this attribute have the following restrictions:

  • Must be marked static.
  • Must not be called from managed code.
  • Must only have blittable arguments.
  • Must not have generic type parameters or be contained within a generic class.

Constructors

UnmanagedCallersOnlyAttribute()

Initializes a new UnmanagedCallersOnlyAttribute instance.

Fields

CallConvs

Optional. If omitted, the runtime will use the default platform calling convention.

EntryPoint

Optional. If omitted, no named export is emitted during compilation.

Properties

TypeId

When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

Methods

Equals(Object)

Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode()

Returns the hash code for this instance.

(Inherited from Attribute)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute()

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object)

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to