Show / Hide Table of Contents

TryReadObject Method

TryReadObject<T>(out T)

Try to read the data at the storage location specified by the defined data tag for the PivDataObject T, storing and returning this data in a new object of type T.

C#
public bool TryReadObject<T>(out T pivDataObject) where T : PivDataObject, new()

Parameters

Type Name Description
T pivDataObject

An output argument, this method will create a new object containing the data from the storage location, as long as it is formatted as expected.

Returns

bool

A boolean, true if the storage location contains data formatted as expected, or no data, and false if the storage location contains data formatted in an unexpected way.

Type Parameters

Name Description
T

The type of PivDataObject to create, set, and return.

Remarks

For example,

bool isValid = pivSession.TryReadObject<PinProtectedData>(out PinProtectedData pinProtect);
if (isValid)
{
    // perform operations
}
pinProtect.Dispose();

or alternatively,

bool isValid = pivSession.ReadObject(out PinProtectedData pinProtect);
using (pinProtect)
{
    if (isValid)
    {
        // perform operations
    }
}

Note that the PivDataObject is Disposable so make sure you use the using keyword or call Dispose directly.

See also the user's manual entry on PIV data objects.

The method will look in the storage location specified by the defined data tag of the pivDataObject.

It is possible that there is no data on the YubiKey in the specified storage location. If so, this method will build a new, empty object as the pivDataObject and return false.

If there is data and it is formatted as expected, the method will build a new object set with the data and return true.

If there is data stored under the specified data tag, but it is not formatted as expected, this method will build a new, empty object as the pivDataObject and return false.

TryReadObject<T>(int, out T)

Try to read the data at the storage location specified by the defined data tag for the PivDataObject T, storing and returning this data in a new object of type T.

C#
public bool TryReadObject<T>(int dataTag, out T pivDataObject) where T : PivDataObject, new()

Parameters

Type Name Description
int dataTag

The alternate data tag, the tag of the storage location to look.

T pivDataObject

An output argument, this method will create a new object containing the data from the storage location, as long as it is formatted as expected.

Returns

bool

A boolean, true if the storage location contains data formatted as expected, or no data, and false if the storage location contains data formatted in an unexpected way.

Type Parameters

Name Description
T

The type of PivDataObject to create, set, and return.

Exceptions

Type Condition
ArgumentException

The dataTag in not allowed to be an alternate tag.

Remarks

For example,

bool isValid = pivSession.TryReadObject<KeyHistory>(out KeyHistory keyHistory);
if (isValid)
{
    // perform operations
}
keyHistory.Dispose();

or alternatively,

bool isValid = pivSession.ReadObject<KeyHistory>(out KeyHistory keyHistory);
using (keyHistory)
{
    if (isValid)
    {
        // perform operations
    }
}

Note that the PivDataObject is Disposable so make sure you use the using keyword or call Dispose directly.

See also the user's manual entry on PIV data objects.

The method will look in the storage location specified by the dataTag argument.

It is possible that there is no data on the YubiKey in the specified storage location. If so, this method will build a new, empty object as the pivDataObject and return false.

If there is data and it is formatted as expected, the method will build a new object set with the data and return true.

If there is data stored under the specified data tag, and it is not formatted as expected, this method will build a new, empty object as the pivDataObject and return false.

In this article
Back to top Generated by DocFX