PostSharp6.4/API Reference/Post­Sharp Namespace/Post Class/Post Methods/Post.​Cast(TSource, TTarget) Method

Post.Cast<TSource, TTarget> Method

At post-compile time, casts an instance of a type into another. A post-compile time error is reported if the source type cannot be assigned to the target type.

Namespace:  PostSharp
Assembly:  PostSharp (in PostSharp.dll) Version: 6.4.3.0 (6.4.3.0)
Syntax
C#
public static TTarget Cast<TSource, TTarget>(
	TSource o
)
where TSource : class
where TTarget : class

Parameters

o
Type: TSource
Instance to be casted.

Type Parameters

TSource
Source type.
TTarget
Target type.

Return Value

Type: TTarget
The object o casted as TTarget.
Remarks
The purpose of this method is to make a source code compilable even when an interface will be implemented at post-compile time. PostSharp ensures that TTarget is assignable from TSource. If yes, the call to this method is simply suppressed. If types are not assignable, a build error is issued.
See Also