Fauna v10 .NET/C# Driver 0.2.0-beta
 
Loading...
Searching...
No Matches
FieldInfo.cs
Go to the documentation of this file.
1using System.Reflection;
4
5namespace Fauna.Mapping;
6
10public sealed class FieldInfo
11{
15 public string Name { get; }
19 public PropertyInfo Property { get; }
23 public Type Type { get; }
27 public bool IsNullable { get; }
28
29 private MappingContext _ctx;
30 private ISerializer? _serializer;
31
32 internal FieldInfo(MappingContext ctx, FieldAttribute attr, PropertyInfo prop)
33 {
34 var nullCtx = new NullabilityInfoContext();
35 var nullInfo = nullCtx.Create(prop);
36
37 Name = attr.Name ?? FieldName.Canonical(prop.Name);
38 Property = prop;
39 Type = prop.PropertyType;
40 IsNullable = nullInfo.WriteState is NullabilityState.Nullable;
41 _ctx = ctx;
42 }
43
44 internal ISerializer Serializer
45 {
46 get
47 {
48 lock (_ctx)
49 {
50 if (_serializer is null)
51 {
52 _serializer = Serialization.Serializer.Generate(_ctx, Type);
53 if (IsNullable && (!_serializer.GetType().IsGenericType ||
54 (_serializer.GetType().IsGenericType &&
55 _serializer.GetType().GetGenericTypeDefinition() !=
56 typeof(NullableStructSerializer<>))))
57 {
58 var serType = typeof(NullableSerializer<>).MakeGenericType(new[] { Type });
59 var ser = Activator.CreateInstance(serType, new[] { _serializer });
60 _serializer = (ISerializer)ser!;
61 }
62 }
63
64 return _serializer;
65 }
66 }
67 }
68}
Attribute used to specify properties of a field in a Fauna object.
Definition Attributes.cs:17
A class that encapsulates the field mapping, serialization, and deserialization of a particular field...
Definition FieldInfo.cs:11
bool IsNullable
Whether the field is nullable.
Definition FieldInfo.cs:27
Type Type
The Type that the field should deserialize into.
Definition FieldInfo.cs:23
PropertyInfo Property
The property info of an associated class.
Definition FieldInfo.cs:19
string Name
The name of the field.
Definition FieldInfo.cs:15
A class representing the mapping context to be used during serialization and deserialization.