BlockLiteral Struct

Definition

Wraps a ECMA CLI delegate (C# lambdas, anonymous methods or delegates) as an Objective-C block.

public struct BlockLiteral
type BlockLiteral = struct
Inheritance
BlockLiteral

Remarks

This is a low-level class that is automatically used by the Xamarin.iOS bindings when using Objective-C block APIs.

In the C#/ECMA CLI world delegates are automatically turned into blocks that can be consumed by Objective-C blocks-aware APIs.

If you need to P/Invoke a native C method that takes a block parameter, you would need to manually setup the BlockLiteral object and declare a proxy method that is invoked by the block handler and will invoke your managed code.

// Imagine that you want to invoke the following method:
// void SetupHandler (void (^block)(double offset, int count));

// Declare the signature of the method that users would have to provide
public delegate void SetupHandlerCallback (double offset, int count);
// Declare the signature of the method that the block will call
delegate void SetupHandlerCallbackProxy (IntPtr blockLiteral, double offset, int count);
// Static variable that points to our trampoline method
static readonly SetupHandlerCallback static_handler = TrampolineHandler;

// Our trampoline method must be registered for reverse-callback with Mono
// it takes one extra parameter than the signature, which is the pointer
// to the block that was originally passed.
[MonoPInvokeCallback (typeof (SetupHandlerCallbackProxy))]
static void TrampolineHandler (IntPtr block, double offset, int count)
{
    // Find the delegate for the block and call it
    var callback = BlockLiteral.GetTarget<SetupHandlerCallback> (block);
    if (callback != null)
        callback (offset, count);
}

[DllImport ("YourLibrary")]
static extern void SetupHandler (ref BlockLiteral block);

public void SetupHandler (SetupHandlerCallback callback)
{
    if (callback == null)
        throw new ArgumentNullException (nameof (callback));
    BlockLiteral block = new BlockLiteral ();
    block.SetupBlock (static_handler, callback);
    try {
        SetupHandler (ref block);
    } finally {
        block.CleanupBlock ();
    }
}

Properties

Target

Returns the target object for the block.

Methods

CleanupBlock()

Releases the resources associated with this block.

GetDelegateForBlock<T>()

This method supports the Xamarin.iOS runtime and is not intended for use by application developers.

GetTarget<T>(IntPtr)

If this block represents a managed delegate, this method will return that managed delegate.

IsManagedBlock(IntPtr)

This method determines whether a block is wrapping a managed delegate or if it's an Objective-C block.

SetupBlock(Delegate, Delegate)

Sets up a block using a trampoline and a user delegate.

SetupBlockUnsafe(Delegate, Delegate)

Sets up a block using a trampoline and a user delegate.

Applies to