Fauna v10 .NET/C# Driver 1.0.0
 
Loading...
Searching...
No Matches
FieldInfo.cs
Go to the documentation of this file.
1using System.Reflection;
3
4namespace Fauna.Mapping;
5
9public sealed class FieldInfo
10{
14 public string Name { get; }
18 public PropertyInfo Property { get; }
19
23 public FieldType FieldType { get; }
24
28 public Type Type { get; }
32 public bool IsNullable { get; }
33
34 private MappingContext _ctx;
35 private ISerializer? _serializer;
36
37 internal FieldInfo(MappingContext ctx, BaseFieldAttribute attr, PropertyInfo prop)
38 {
39 var nullCtx = new NullabilityInfoContext();
40 var nullInfo = nullCtx.Create(prop);
41
42 Name = attr.Name ?? FieldName.Canonical(prop.Name);
43 FieldType = attr.Type;
44 Property = prop;
45 Type = prop.PropertyType;
46 IsNullable = nullInfo.WriteState is NullabilityState.Nullable;
47 _ctx = ctx;
48 }
49
50 internal ISerializer Serializer
51 {
52 get
53 {
54 lock (_ctx)
55 {
56 if (_serializer is null)
57 {
58 _serializer = Serialization.Serializer.Generate(_ctx, Type);
59 if (IsNullable && (!_serializer.GetType().IsGenericType ||
60 (_serializer.GetType().IsGenericType &&
61 _serializer.GetType().GetGenericTypeDefinition() !=
62 typeof(NullableStructSerializer<>))))
63 {
64 var serType = typeof(NullableSerializer<>).MakeGenericType(new[] { Type });
65 var ser = Activator.CreateInstance(serType, new[] { _serializer });
66 _serializer = (ISerializer)ser!;
67 }
68 }
69
70 return _serializer;
71 }
72 }
73 }
74}
An abstract type for attributing user-defined document classes.
Definition Attributes.cs:30
readonly FieldType Type
The type of the field.
Definition Attributes.cs:38
A class that encapsulates the field mapping, serialization, and deserialization of a particular field...
Definition FieldInfo.cs:10
bool IsNullable
Whether the field is nullable.
Definition FieldInfo.cs:32
Type Type
The Type that the field should deserialize into.
Definition FieldInfo.cs:28
PropertyInfo Property
The property info of an associated class.
Definition FieldInfo.cs:18
string Name
The name of the field.
Definition FieldInfo.cs:14
A class representing the mapping context to be used during serialization and deserialization.
A generic interface that defines serialize and deserialize behavior for a specific type,...
FieldType
An enum for the field type, used with concrete implementations of BaseFieldAttribute.
Definition FieldType.cs:7
@ FieldName
The token type is a Fauna property name.