Open sandboxFocus

Method Cast

Cast<TSource, TTarget>(TSource)

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

Declaration
public static TTarget Cast<TSource, TTarget>(TSource o) where TSource : class where TTarget : class
Parameters
Type Name Description
TSource o

The instance to cast.

Returns
Type Description
TTarget

The object o cast as TTarget.

Type Parameters
Name Description
TSource

The source type.

TTarget

The target type.

Remarks

The purpose of this method is to make source code compilable even when an interface is implemented at post-compile time. PostSharp ensures that TTarget is assignable from TSource. If it is, the call to this method is suppressed. If the types are not assignable, a build error is issued. Use it, for example, to access an interface introduced by an aspect from the source code of the target class.

See Also