MemberInfo
DataTypeRequest<NetField> is a request for field type of FieldInfo or PropertyInfo. (Avalanche.DataType.Net.dll)
// Create service
IService service = Services.Create(NetTypeHandlers.Instance);
// Create request
MemberInfo fi = typeof(MyRecord).GetProperty(nameof(MyRecord.Name))!;
DataTypeRequest<NetField> request = new(fi);
// Request record
IFieldType datatype = service.GetRequired<DataTypeRequest<NetField>, IFieldType>(request);
// Print datatype
WriteLine(datatype.PrintTree(format: DataTypePrintTreeExtensions.PrintFormat.DefaultLong));
public class MyRecord
{
[Description("This fields contains name of the record"), DisplayName("name")]
[IgnoreDataMember]
public string? Name { get; set; }
}
public class ExampleAttribute : Attribute { }
IFieldType { Name = "Name", Order = 0, Unassignable = True, Referable = False, Description = "This fields contains name of the record", Annotations = [[System.Runtime.Serialization.IgnoreDataMemberAttribute, ]] } └── Value = IStringType { Name = "string", Encoding = "Unicode (UTF-8)", Unassignable = True, Referable = True, Description = "Unicode UTF-8", Annotations = [], MinLength = 0, MaxLength = 2147483647 } └── Element = IIntegerType { Name = "byte", Base = 2, SignificandPrecision = 8, SignCapability = False, Unassignable = False, Referable = False, Annotations = [], DefaultValue = 0, MinValue = 0, MaxValue = 255 }
Annotations
Following attributes are regarded and applied or adapted:
- [DataMember] System.Runtime.Serialization.DataMemberAttribute for Order, IsRequired and Name.
- [IgnoreDataMember] System.Runtime.Serialization.IgnoreDataMemberAttribute.
- [RequiredMember] System.Runtime.CompilerServices.RequiredMemberAttribute
- [Required] System.ComponentModel.DataAnnotations.RequiredAttribute
- [Nullable] System.Runtime.CompilerServices.NullableAttribute
- [Description] System.ComponentModel.DescriptionAttribute
- [DefaultValue] System.ComponentModel.DefaultValueAttribute
- [Key] System.ComponentModel.DataAnnotations.KeyAttribute
All other attributes are accessible in datatype.Annotations property.
// Print annotations
foreach(var annotation in datatype.Annotations)
WriteLine($"{annotation.Key} = \"{annotation.Value}\""); // "name"
System.Runtime.Serialization.IgnoreDataMemberAttribute = ""
Full Example
Full example
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using Avalanche.DataType;
using Avalanche.Identity;
using Avalanche.Service;
using static System.Console;
public class field
{
public static void Run()
{
{
// <04>
// Create service
IService service = Services.Create(NetTypeHandlers.Instance);
// Create request
MemberInfo fi = typeof(MyRecord).GetProperty(nameof(MyRecord.Name))!;
DataTypeRequest<NetField> request = new(fi);
// Request record
IFieldType datatype = service.GetRequired<DataTypeRequest<NetField>, IFieldType>(request);
// Print datatype
WriteLine(datatype.PrintTree(format: DataTypePrintTreeExtensions.PrintFormat.DefaultLong));
// </04>
// <05>
// Print annotations
foreach(var annotation in datatype.Annotations)
WriteLine($"{annotation.Key} = \"{annotation.Value}\""); // "name"
// </05>
}
}
// <99>
public class MyRecord
{
[Description("This fields contains name of the record"), DisplayName("name")]
[IgnoreDataMember]
public string? Name { get; set; }
}
public class ExampleAttribute : Attribute { }
// </99>
}