-
Notifications
You must be signed in to change notification settings - Fork 180
/
TypeCastObject.cs
58 lines (51 loc) · 2.31 KB
/
TypeCastObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// -----------------------------------------------------------------------
// <copyright file="TypeCastObject.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------
namespace Exiled.API.Features.Core
{
/// <summary>
/// The interface which allows defined objects to be cast to each other.
/// </summary>
/// <typeparam name="T">The type of the object to cast.</typeparam>
public abstract class TypeCastObject<T>
where T : class
{
/// <summary>
/// Initializes a new instance of the <see cref="TypeCastObject{T1}"/> class.
/// </summary>
protected TypeCastObject()
{
}
/// <summary>
/// Unsafely casts the current <typeparamref name="T"/> instance to the specified <typeparamref name="TObject"/> type.
/// </summary>
/// <typeparam name="TObject">The type to which to cast the <typeparamref name="T"/> instance.</typeparam>
/// <returns>The cast <typeparamref name="T"/> instance.</returns>
public TObject Cast<TObject>()
where TObject : class, T => this as T as TObject;
/// <summary>
/// Safely casts the current <typeparamref name="TObject"/> instance to the specified <typeparamref name="TObject"/> type.
/// </summary>
/// <typeparam name="TObject">The type to which to cast the <typeparamref name="TObject"/> instance.</typeparam>
/// <param name="param">The cast object.</param>
/// <returns><see langword="true"/> if the <typeparamref name="TObject"/> instance was successfully cast; otherwise, <see langword="false"/>.</returns>
public bool Cast<TObject>(out TObject param)
where TObject : class, T
{
param = default;
if (this as TObject is not TObject cast)
return false;
param = cast;
return true;
}
/// <inheritdoc cref="Cast{T}()"/>
public TObject As<TObject>()
where TObject : class, T => Cast<TObject>();
/// <inheritdoc cref="Cast{T}(out T)"/>
public bool Is<TObject>(out TObject param)
where TObject : class, T => Cast(out param);
}
}