Connectivity Software User's Guide and Reference
TryGetRequiredService<TService> Method



OpcLabs.BaseLib Assembly > OpcLabs.BaseLib.Extensions Namespace > IServiceProviderExtension2 Class : TryGetRequiredService<TService> Method
The type of service object to get.
The service provider.

The value of this parameter cannot be null (Nothing in Visual Basic).

A service object of type TService.

The value of this parameter can be null (Nothing in Visual Basic).

Tries to get the required service object of the specified type, casting it to the same type.
Syntax
'Declaration
 
<ExtensionAttribute()>
<JetBrains.Annotations.CanBeNullAttribute()>
Public Shared Function TryGetRequiredService(Of TService)( _
   ByVal serviceProvider As IServiceProvider, _
   ByRef service As TService _
) As Exception
'Usage
 
Dim serviceProvider As IServiceProvider
Dim service As TService
Dim value As Exception
 
value = IServiceProviderExtension2.TryGetRequiredService(Of TService)(serviceProvider, service)
[Extension()]
[JetBrains.Annotations.CanBeNull()]
public static Exception TryGetRequiredService<TService>( 
   IServiceProvider serviceProvider,
   out TService service
)
[Extension()]
[JetBrains.Annotations.CanBeNull()]
public:
static Exception^ TryGetRequiredServicegeneric<typename TService>
( 
   IServiceProvider^ serviceProvider,
   [Out] TService^ service
) 

Parameters

serviceProvider
The service provider.

The value of this parameter cannot be null (Nothing in Visual Basic).

service
A service object of type TService.

The value of this parameter can be null (Nothing in Visual Basic).

Type Parameters

TService
The type of service object to get.

Return Value

Returns null when the service object has been successfully retrieved. If there is no service of the requested type, returns a non-null exception.

This method can return null (Nothing in Visual Basic).

Remarks

This is an extension method (info: C#, VB.NET). In languages that have support for extensions methods (such as C# and VB.NET), you can use the extension method as if it were a regular method on the object that is its first parameter. In other languages (such as with Python.NET), you will call the extension as a static method, and pass it the object on which it acts as its first parameter.

Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also