tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ModelExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Linq.Expressions;
3using System.Reflection;
4
6
8{
12 static class ModelExtensions
13 {
23 public static TProperty Require<TModel, TProperty>(this TModel model, Expression<Func<TModel, TProperty?>> accessor)
24 where TModel : EntityId
25 where TProperty : struct
26 {
27 ArgumentNullException.ThrowIfNull(model);
28 ArgumentNullException.ThrowIfNull(accessor);
29
30 var memberSelectorExpression = (MemberExpression)accessor.Body;
31 var property = (PropertyInfo)memberSelectorExpression.Member;
32
33 var nullableValue = (TProperty?)property.GetValue(model);
34 if (!nullableValue.HasValue)
35 throw new InvalidOperationException($"Expected {model.GetType().Name}.{property.Name} to be set here!");
36
37 return nullableValue.Value;
38 }
39 }
40}
Common base of entities with IDs.
Definition EntityId.cs:7
static TProperty Require< TModel, TProperty >(this TModel model, Expression< Func< TModel, TProperty?> > accessor)
Require a given Nullable<T> property of a given model be non-null.