Datei: NDO.Mapping/NDO.Mapping/Serialization/SerializationExtensions.cs

Last Commit (2c6a6fe)
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace NDO.Mapping.Serialization
6 {
7 ····/// <summary>
8 ····/// Extension class to convert attribute instances to the correct type.
9 ····/// </summary>
10 ····public static class SerializationExtensions
11 ····{
12 ········/// <summary>
13 ········/// Serializes an object which should be an NDORelationAttribute
14 ········/// </summary>
15 ········/// <param name="attr"></param>
16 ········/// <returns></returns>
17 ········/// <remarks>
18 ········/// We use a serialization mechanism which is independent from the runtime.
19 ········/// System.Text.Json imports dependencies to certain runtime implementations.
20 ········/// </remarks>
21 ········public static string Serialize( this object attr )
22 ········{
23 ············if (attr.GetType().Name != "NDORelationAttribute")
24 ················throw new ArgumentException( "Wrong parameter type", "attr" );
25 ············Type t = attr.GetType();
26 ············Type rt = (Type)t.GetProperty("RelationType").GetValue(attr);
27 ············string rts = null;
28 ············if (rt != null)
29 ················rts = rt.FullName;
30 ············string ri = t.GetProperty("Info").GetValue(attr).ToString();
31 ············string rn = (string)t.GetProperty("RelationName").GetValue(attr);
32 return $@"{ rts??"#null"} , { ri} , { rn??"#null"} ";
33 ········}
34
35 ········/// <summary>
36 ········/// Converts an attribute object to the correct type
37 ········/// </summary>
38 ········/// <param name="attr"></param>
39 ········/// <returns></returns>
40 ········/// <remarks>
41 ········/// We use a serialization mechanism which is independent from the runtime.
42 ········/// System.Text.Json imports dependencies to certain runtime implementations.
43 ········/// </remarks>
44 ········public static NDORelationAttribute ConvertToNdoRelation( this object attr )
45 ········{
46 ············var strAttr = Serialize(attr);
47 ············var arr = strAttr.Split(',');
48 ············var rt = arr[0];
49 ············if (rt == "#null")
50 ················rt = null;
51 ············Type relationType = null;
52 ············if (rt != null)
53 ················relationType = Type.GetType( rt );
54 ············Enum.TryParse<RelationInfo>(arr[1], out var ri);
55 var rn = arr[1];
56 ············if (rn == "#null")
57 ················rn = null;
58 ············return new NDORelationAttribute( relationType, ri, rn );
59 ········}
60 ····}
61 }
62
New Commit (6940b24)
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace NDO.Mapping.Serialization
6 {
7 ····/// <summary>
8 ····/// Extension class to convert attribute instances to the correct type.
9 ····/// </summary>
10 ····public static class SerializationExtensions
11 ····{
12 ········/// <summary>
13 ········/// Serializes an object which should be an NDORelationAttribute
14 ········/// </summary>
15 ········/// <param name="attr"></param>
16 ········/// <returns></returns>
17 ········/// <remarks>
18 ········/// We use a serialization mechanism which is independent from the runtime.
19 ········/// System.Text.Json imports dependencies to certain runtime implementations.
20 ········/// </remarks>
21 ········public static string Serialize( this object attr )
22 ········{
23 ············if (attr.GetType().Name != "NDORelationAttribute")
24 ················throw new ArgumentException( "Wrong parameter type", "attr" );
25 ············Type t = attr.GetType();
26 ············Type rt = (Type)t.GetProperty("RelationType").GetValue(attr);
27 ············string rts = null;
28 ············if (rt != null)
29 ················rts = rt.FullName;
30 ············string ri = t.GetProperty("Info").GetValue(attr).ToString();
31 ············string rn = (string)t.GetProperty("RelationName").GetValue(attr);
32 return $@"{ rts??"#null"} , { ri} , { rn??"#null"} ";
33 ········}
34
35 ········/// <summary>
36 ········/// Converts an attribute object to the correct type
37 ········/// </summary>
38 ········/// <param name="attr"></param>
39 ········/// <returns></returns>
40 ········/// <remarks>
41 ········/// We use a serialization mechanism which is independent from the runtime.
42 ········/// System.Text.Json imports dependencies to certain runtime implementations.
43 ········/// </remarks>
44 ········public static NDORelationAttribute ConvertToNdoRelation( this object attr )
45 ········{
46 ············var strAttr = Serialize(attr);
47 ············var arr = strAttr.Split(',');
48 ············var rt = arr[0];
49 ············if (rt == "#null")
50 ················rt = null;
51 ············Type relationType = null;
52 ············if (rt != null)
53 ················relationType = Type.GetType( rt );
54 ············Enum.TryParse<RelationInfo>(arr[1], out var ri);
55 var rn = arr[2];
56 ············if (rn == "#null")
57 ················rn = null;
58 ············return new NDORelationAttribute( relationType, ri, rn );
59 ········}
60 ····}
61 }
62