PostSharp 4.3 / / Post­Sharp Documentation / API Reference / Post­Sharp Namespace / Post Class / Post Methods / Post.​Cast(Source­Type, Target­Type) Method
Post.Cast<SourceType, TargetType> 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: (
public static TargetType Cast<SourceType, TargetType>(
	SourceType o
where SourceType : class
where TargetType : class


Type: SourceType
Instance to be casted.

Type Parameters

Source type.
Target type.

Return Value

Type: TargetType
The object o casted as TargetType.
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 TargetType is assignable from SourceType. If yes, the call to this method is simply suppressed. If types are not assignable, a build error is issued.
See Also