Column.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Reflection;
  8. using System.Runtime.Serialization;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. namespace InABox.Core
  12. {
  13. public interface IColumn
  14. {
  15. string Property { get; }
  16. Expression Expression { get; }
  17. Type Type { get; }
  18. }
  19. public static class Column
  20. {
  21. public static IColumn Create(Type concrete, string property)
  22. {
  23. var type = typeof(Column<>).MakeGenericType(concrete);
  24. var result = Activator.CreateInstance(type, property) as IColumn;
  25. return result!;
  26. }
  27. }
  28. public class Column<T> : SerializableExpression<T>, IColumn
  29. {
  30. public Type Type
  31. {
  32. get
  33. {
  34. if (Expression == null)
  35. throw new Exception(string.Format("Expression [{0}] may not be null!", Property));
  36. if (Expression is IndexExpression)
  37. return DatabaseSchema.Property(typeof(T), Property).PropertyType;
  38. return Expression.Type;
  39. }
  40. }
  41. public bool IsEqualTo(string name) =>
  42. !string.IsNullOrWhiteSpace(name) && string.Equals(Property, name);
  43. public bool IsEqualTo(Column<T> column) => string.Equals(Property, column.Property);
  44. public bool IsParentOf(string name) =>
  45. !string.IsNullOrWhiteSpace(name) && name.StartsWith(Property + ".");
  46. public Column()
  47. {
  48. }
  49. public Column(IProperty property)
  50. {
  51. Property = property.Name;
  52. Expression = property.Expression();
  53. }
  54. public Column(Expression<Func<T, object?>> expression) : base(expression)
  55. {
  56. //String[] parts = expression.ToString().Split(new String[] { "=>" }, StringSplitOptions.RemoveEmptyEntries);
  57. //string property = String.Join(".", parts.Last().Split('.').Skip(1));
  58. //property = property.Replace("Convert(", "").Replace("(","").Replace(")", "");
  59. Property = CoreUtils.GetFullPropertyName(expression, ".");
  60. }
  61. public Column(string property)
  62. {
  63. Property = property;
  64. var iprop = DatabaseSchema.Property(typeof(T), property);
  65. if (iprop != null)
  66. Expression = iprop.Expression();
  67. else
  68. Expression = CoreUtils.CreateMemberExpression(typeof(T), property);
  69. }
  70. public Column<TNew> Cast<TNew>()
  71. where TNew: T
  72. {
  73. return new Column<TNew>(Property);
  74. }
  75. public bool TryCast<TNew>([NotNullWhen(true)] out Column<TNew>? newColumn)
  76. {
  77. if(DatabaseSchema.Property(typeof(TNew), Property) is IProperty property)
  78. {
  79. newColumn = new Column<TNew>(property);
  80. return true;
  81. }
  82. else
  83. {
  84. newColumn = null;
  85. return false;
  86. }
  87. }
  88. public string Property { get; private set; }
  89. public override void Deserialize(SerializationInfo info, StreamingContext context)
  90. {
  91. }
  92. public override void Serialize(SerializationInfo info, StreamingContext context)
  93. {
  94. }
  95. public static explicit operator Column<T>(Column<Entity> v)
  96. {
  97. var result = new Column<T>();
  98. var exp = CoreUtils.ExpressionToString(typeof(T), v.Expression, true);
  99. result.Expression = CoreUtils.StringToExpression(exp);
  100. result.Property = v.Property;
  101. return result;
  102. }
  103. public override string ToString()
  104. {
  105. var name = Expression.ToString().Replace("x => ", "").Replace("x.", "");
  106. if (Expression.NodeType == System.Linq.Expressions.ExpressionType.Index)
  107. {
  108. var chars = name.SkipWhile(x => !x.Equals('[')).TakeWhile(x => !x.Equals(']'));
  109. name = string.Join("", chars).Replace("[", "").Replace("]", "").Replace("\"", "");
  110. }
  111. return name;
  112. //return Property.ToString();
  113. }
  114. }
  115. public interface IColumns : ISerializeBinary
  116. {
  117. int Count { get; }
  118. bool Any();
  119. IEnumerable<IColumn> GetColumns();
  120. IEnumerable<string> ColumnNames();
  121. Dictionary<String, Type> AsDictionary();
  122. IColumns Add(string column);
  123. IColumns Add(IColumn column);
  124. IColumns Add<T>(Expression<Func<T, object?>> column);
  125. IColumns DefaultColumns(params ColumnType[] types);
  126. }
  127. public enum ColumnType
  128. {
  129. ExcludeVisible,
  130. /// <summary>
  131. /// Do not include <see cref="Entity.ID"/> in the columns.
  132. /// </summary>
  133. ExcludeID,
  134. IncludeOptional,
  135. IncludeForeignKeys,
  136. /// <summary>
  137. /// Include all columns that are accessible through entity links present in the root class.
  138. /// </summary>
  139. IncludeLinked,
  140. IncludeAggregates,
  141. IncludeFormulae,
  142. /// <summary>
  143. /// Include any columns that are a <see cref="CustomProperty"/>.
  144. /// </summary>
  145. IncludeUserProperties,
  146. /// <summary>
  147. /// Include all columns that are accessible through entity links, even nested ones.
  148. /// </summary>
  149. IncludeNestedLinks,
  150. IncludeEditable,
  151. /// <summary>
  152. /// Add all columns that are data actually on the entity, and not on entity links or calculated fields.
  153. /// </summary>
  154. DataColumns,
  155. /// <summary>
  156. /// Add all columns found.
  157. /// </summary>
  158. All
  159. }
  160. public static class Columns
  161. {
  162. public static IColumns Create<T>(Type concrete)
  163. {
  164. if (!typeof(T).IsAssignableFrom(concrete))
  165. throw new Exception($"Columns: {concrete.EntityName()} does not implement {typeof(T).EntityName()}");
  166. var type = typeof(Columns<>).MakeGenericType(concrete);
  167. var result = Activator.CreateInstance(type);
  168. return (result as IColumns)!;
  169. }
  170. public static IColumns Create(Type concrete)
  171. {
  172. var type = typeof(Columns<>).MakeGenericType(concrete);
  173. var result = Activator.CreateInstance(type) as IColumns;
  174. return result!;
  175. }
  176. public static IColumns Create(Type concrete, IEnumerable<string> columns)
  177. {
  178. var type = typeof(Columns<>).MakeGenericType(concrete);
  179. var result = (IColumns)Activator.CreateInstance(type);
  180. foreach (var column in columns)
  181. result.Add(column);
  182. return result;
  183. }
  184. public static IColumns Create(Type concrete, params string[] columns)
  185. {
  186. var type = typeof(Columns<>).MakeGenericType(concrete);
  187. var result = (IColumns)Activator.CreateInstance(type);
  188. foreach (var column in columns)
  189. result.Add(column);
  190. return result;
  191. }
  192. }
  193. public class Columns<T> : IColumns, IEnumerable<Column<T>>
  194. {
  195. private readonly List<Column<T>> columns;
  196. public Columns()
  197. {
  198. columns = new List<Column<T>>();
  199. }
  200. public Columns(IEnumerable<CoreColumn> columns) : this()
  201. {
  202. foreach (var column in columns)
  203. Add(column.ColumnName);
  204. }
  205. /// <summary>
  206. /// Create a new <see cref="Columns{T}"/>, using <paramref name="columns"/> as the internal list.
  207. /// </summary>
  208. /// <remarks>
  209. /// <paramref name="columns"/> is passed by reference and is stored as the internal list of the <see cref="Columns{T}"/>;
  210. /// hence if one wishes <paramref name="columns"/> to not be modified, one should pass a copy of the list.
  211. /// </remarks>
  212. /// <param name="columns"></param>
  213. public Columns(List<Column<T>> columns)
  214. {
  215. this.columns = columns;
  216. }
  217. public Columns<TNew> Cast<TNew>()
  218. where TNew : T
  219. {
  220. var cols = new Columns<TNew>();
  221. foreach(var column in columns)
  222. {
  223. cols.Add(column.Cast<TNew>());
  224. }
  225. return cols;
  226. }
  227. /// <summary>
  228. /// Cast the columns to <typeparamref name="TNew"/>, keeping the columns that are found in both <typeparamref name="T"/> and <typeparamref name="TNew"/>.
  229. /// </summary>
  230. /// <typeparam name="TNew"></typeparam>
  231. /// <returns></returns>
  232. public Columns<TNew> CastIntersection<TNew>()
  233. {
  234. var cols = new Columns<TNew>();
  235. foreach(var column in columns)
  236. {
  237. if (column.TryCast<TNew>(out var newColumn))
  238. {
  239. cols.Add(newColumn);
  240. }
  241. }
  242. return cols;
  243. }
  244. public override string ToString()
  245. {
  246. return String.Join("; ", columns.Select(x => x.Property));
  247. }
  248. public int IndexOf(String columnname)
  249. {
  250. return ColumnNames().ToList().IndexOf(columnname);
  251. }
  252. public int IndexOf(Expression<Func<T, object>> expression)
  253. {
  254. return ColumnNames().ToList().IndexOf(CoreUtils.GetFullPropertyName(expression,"."));
  255. }
  256. public Columns(params Expression<Func<T, object?>>[] expressions) : this()
  257. {
  258. foreach (var expression in expressions)
  259. columns.Add(new Column<T>(expression));
  260. }
  261. public Columns(IEnumerable<string> properties) : this()
  262. {
  263. foreach (var property in properties)
  264. columns.Add(new Column<T>(property));
  265. }
  266. public Column<T>[] Items
  267. {
  268. get { return columns != null ? columns.ToArray() : new Column<T>[] { }; }
  269. set
  270. {
  271. columns.Clear();
  272. columns.AddRange(value);
  273. }
  274. }
  275. public int Count => columns.Count;
  276. public IEnumerable<IColumn> GetColumns() => columns;
  277. public bool Any() => columns.Any();
  278. public bool Contains(string column) => Items.Any(x => x.IsEqualTo(column));
  279. public bool Contains(Column<T> column) => Items.Any(x => x.IsEqualTo(column));
  280. public IColumns Add(string column)
  281. {
  282. if(CoreUtils.TryGetProperty(typeof(T), column, out var propertyInfo))
  283. {
  284. if (!propertyInfo.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)) &&
  285. !propertyInfo.PropertyType.GetInterfaces().Any(x => x == typeof(IEntityLink)))
  286. {
  287. var exists = columns.Any(x => x.Expression.ToString().Replace("x.", "").Equals(column));
  288. if (!exists)
  289. columns.Add(new Column<T>(column));
  290. }
  291. }
  292. else
  293. {
  294. var prop = DatabaseSchema.Property(typeof(T), column);
  295. if (prop != null)
  296. {
  297. var exists = columns.Any(x => x.Expression.Equals(prop.Expression()));
  298. if (!exists)
  299. columns.Add(new Column<T>(column));
  300. }
  301. }
  302. return this;
  303. }
  304. public Columns<T> AddSubColumns<TSub>(Expression<Func<T, TSub>> super, Columns<TSub>? sub)
  305. {
  306. sub ??= CoreUtils.GetColumns(sub);
  307. var prefix = CoreUtils.GetFullPropertyName(super, ".") + ".";
  308. foreach(var column in sub.ColumnNames())
  309. {
  310. columns.Add(new Column<T>(prefix + column));
  311. }
  312. return this;
  313. }
  314. public IColumns Add<TEntity>(Expression<Func<TEntity, object?>> expression)
  315. {
  316. return Add(CoreUtils.GetFullPropertyName(expression, "."));
  317. }
  318. public Columns<T> Add(Column<T> column)
  319. {
  320. if(!columns.Any(x => x.Property.Equals(column.Property)))
  321. {
  322. columns.Add(column);
  323. }
  324. return this;
  325. }
  326. public IColumns Add(IColumn column)
  327. {
  328. if (column is Column<T> col)
  329. return Add(col);
  330. return this;
  331. }
  332. public IEnumerable<string> ColumnNames()
  333. {
  334. return Items.Select(c => c.Property);
  335. //List<String> result = new List<string>();
  336. //foreach (var col in Items)
  337. // result.Add(col.Property);
  338. //return result;
  339. }
  340. public Dictionary<String, Type> AsDictionary()
  341. {
  342. Dictionary< String, Type> result = new Dictionary< String, Type>();
  343. foreach (var column in Items)
  344. result[column.Property] = column.Type;
  345. return result;
  346. }
  347. public IColumns DefaultColumns(params ColumnType[] types)
  348. {
  349. return Default(types);
  350. }
  351. public Columns<T> Add(IEnumerable<Column<T>> columns)
  352. {
  353. this.columns.AddRange(columns);
  354. return this;
  355. }
  356. public Columns<T> Add(params string[] columnnames)
  357. {
  358. foreach (var name in columnnames)
  359. Add(name);
  360. return this;
  361. }
  362. public Columns<T> Add(IEnumerable<string> columnnames)
  363. {
  364. foreach (var name in columnnames)
  365. Add(name);
  366. return this;
  367. }
  368. public Columns<T> Add(Expression<Func<T, object?>> expression)
  369. {
  370. try
  371. {
  372. var property = CoreUtils.GetFullPropertyName(expression, ".");
  373. var exists = columns.Any(x => x.Expression.ToString().Replace("x.", "").Equals(property));
  374. if (!exists)
  375. columns.Add(new Column<T>(expression));
  376. }
  377. catch (Exception e)
  378. {
  379. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  380. }
  381. return this;
  382. }
  383. public Columns<T> Add<TType>(Expression<Func<T, TType>> expression)
  384. {
  385. try
  386. {
  387. var property = CoreUtils.GetFullPropertyName(expression, ".");
  388. var exists = columns.Any(x => x.Expression.ToString().Replace("x.", "").Equals(property));
  389. if (!exists)
  390. {
  391. columns.Add(new Column<T>(property));
  392. }
  393. }
  394. catch (Exception e)
  395. {
  396. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  397. }
  398. return this;
  399. }
  400. public Columns<T> Remove(string column)
  401. {
  402. var col = new Column<T>(column);
  403. columns.RemoveAll(x => x.ToString() == col.ToString());
  404. return this;
  405. }
  406. public static explicit operator Columns<T>(Columns<Entity> vs)
  407. {
  408. var result = new Columns<T>();
  409. var items = vs.Items.Cast<Column<T>>().ToArray();
  410. result.Items = items;
  411. //List<Column<T>> cols = new List<Column<T>>();
  412. //foreach (var v in vs.Items)
  413. // cols.Add((Column<T>)v);
  414. //result.Items = cols.ToArray();
  415. return result;
  416. }
  417. public Columns<T> Default(params ColumnType[] types)
  418. {
  419. columns.Clear();
  420. var props = DatabaseSchema.Properties(typeof(T))
  421. .Where(x => x.Setter() != null)
  422. .OrderBy(x => x.PropertySequence()).ToList();
  423. if (types.Contains(ColumnType.All))
  424. {
  425. foreach (var prop in props)
  426. columns.Add(new Column<T>(prop.Name));
  427. return this;
  428. }
  429. else if (types.Contains(ColumnType.DataColumns))
  430. {
  431. foreach(var prop in props)
  432. {
  433. if (prop.IsCalculated)
  434. {
  435. continue;
  436. }
  437. if (prop.HasParentEntityLink())
  438. {
  439. if(prop.Parent?.HasParentEntityLink() == true || !prop.Name.EndsWith(".ID"))
  440. {
  441. continue;
  442. }
  443. }
  444. columns.Add(new Column<T>(prop.Name));
  445. }
  446. if(types.Length == 1)
  447. {
  448. return this;
  449. }
  450. }
  451. if (typeof(T).IsSubclassOf(typeof(Entity)) && !types.Contains(ColumnType.ExcludeID))
  452. columns.Add(new Column<T>("ID"));
  453. for (int iCol = 0; iCol < props.Count; iCol++)
  454. {
  455. var prop = props[iCol];
  456. var bOK = true;
  457. var bIsForeignKey = false;
  458. var bNullEditor = prop.Editor is NullEditor;
  459. if (prop is CustomProperty)
  460. {
  461. if (!types.Any(x => x.Equals(ColumnType.IncludeUserProperties)))
  462. bOK = false;
  463. else
  464. columns.Add(new Column<T>(prop.Name));
  465. }
  466. if (bOK)
  467. if (prop.Name.Contains(".") && !(prop is CustomProperty))
  468. {
  469. var ancestors = prop.Name.Split('.');
  470. var anclevel = 2;
  471. for (var i = 1; i < ancestors.Length; i++)
  472. {
  473. var ancestor = string.Join(".", ancestors.Take(i));
  474. var ancprop = CoreUtils.GetProperty(typeof(T), ancestor);
  475. bNullEditor = bNullEditor || ancprop.GetCustomAttribute<NullEditor>() != null;
  476. if (ancprop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)))
  477. anclevel++;
  478. else if (ancprop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
  479. {
  480. if (types.Contains(ColumnType.IncludeLinked) || types.Contains(ColumnType.IncludeForeignKeys))
  481. {
  482. if (types.Contains(ColumnType.IncludeNestedLinks) || ancestors.Length <= anclevel)
  483. {
  484. if (prop.Name.EndsWith(".ID") && types.Contains(ColumnType.IncludeForeignKeys))
  485. {
  486. bIsForeignKey = true;
  487. break;
  488. }
  489. if (!types.Contains(ColumnType.IncludeLinked))
  490. {
  491. bOK = false;
  492. break;
  493. }
  494. }
  495. else
  496. {
  497. bOK = false;
  498. break;
  499. }
  500. }
  501. else
  502. {
  503. bOK = false;
  504. break;
  505. }
  506. }
  507. }
  508. }
  509. if (bOK)
  510. {
  511. var visible = prop.Editor != null
  512. ? bNullEditor
  513. ? Visible.Hidden
  514. : prop.Editor.Visible
  515. : Visible.Optional;
  516. var editable = prop.Editor != null
  517. ? bNullEditor
  518. ? Editable.Hidden
  519. : prop.Editor.Editable
  520. : Editable.Enabled;
  521. bOK = (types.Any(x => x.Equals(ColumnType.IncludeForeignKeys)) && bIsForeignKey) ||
  522. (!types.Any(x => x.Equals(ColumnType.ExcludeVisible)) && visible.Equals(Visible.Default)) ||
  523. (types.Any(x => x.Equals(ColumnType.IncludeOptional)) && visible.Equals(Visible.Optional)) ||
  524. (types.Any(x => x.Equals(ColumnType.IncludeEditable)) && editable.ColumnVisible());
  525. }
  526. var property = bOK ? DatabaseSchema.Property(typeof(T), prop.Name) : null;
  527. if (property is StandardProperty)
  528. {
  529. if (bOK && !types.Any(x => x.Equals(ColumnType.IncludeAggregates)))
  530. bOK = CoreUtils.GetProperty(typeof(T), prop.Name).GetCustomAttribute<AggregateAttribute>() == null;
  531. if (bOK && !types.Any(x => x.Equals(ColumnType.IncludeFormulae)))
  532. bOK = CoreUtils.GetProperty(typeof(T), prop.Name).GetCustomAttribute<FormulaAttribute>() == null;
  533. }
  534. if (bOK && !columns.Any(x => string.Equals(x.Property?.ToUpper(), prop.Name?.ToUpper())))
  535. {
  536. if (prop.Editor is LookupEditor le)
  537. {
  538. if (le.OtherColumns != null)
  539. {
  540. var prefix = String.Join(".",prop.Name.Split('.').Reverse().Skip(1).Reverse());
  541. foreach (var col in le.OtherColumns)
  542. {
  543. String newcol = prefix + "." + col.Key;
  544. if (!columns.Any(x => String.Equals(newcol, x.Property)))
  545. columns.Add(new Column<T>(newcol));
  546. }
  547. }
  548. }
  549. if (!columns.Any(x => String.Equals(prop.Name, x.Property)))
  550. columns.Add(new Column<T>(prop.Name));
  551. }
  552. }
  553. return this;
  554. }
  555. #region Binary Serialization
  556. public void SerializeBinary(CoreBinaryWriter writer)
  557. {
  558. writer.Write(columns.Count);
  559. foreach(var column in columns)
  560. {
  561. writer.Write(column.Property);
  562. }
  563. }
  564. public void DeserializeBinary(CoreBinaryReader reader)
  565. {
  566. columns.Clear();
  567. var nColumns = reader.ReadInt32();
  568. for(int i = 0; i < nColumns; ++i)
  569. {
  570. var property = reader.ReadString();
  571. columns.Add(new Column<T>(property));
  572. }
  573. }
  574. #endregion
  575. public IEnumerator<Column<T>> GetEnumerator()
  576. {
  577. return columns.GetEnumerator();
  578. }
  579. IEnumerator IEnumerable.GetEnumerator()
  580. {
  581. return columns.GetEnumerator();
  582. }
  583. }
  584. public static class ColumnsExtensions
  585. {
  586. public static Columns<T> ToColumns<T>(this IEnumerable<Column<T>> columns)
  587. {
  588. return new Columns<T>(columns.ToList());
  589. }
  590. }
  591. public static class ColumnSerialization
  592. {
  593. /// <summary>
  594. /// Inverse of <see cref="Write{T}(CoreBinaryWriter, Columns{T}?)"/>.
  595. /// </summary>
  596. /// <param name="reader"></param>
  597. /// <returns></returns>
  598. public static Columns<T>? ReadColumns<T>(this CoreBinaryReader reader)
  599. {
  600. if (reader.ReadBoolean())
  601. {
  602. var columns = new Columns<T>();
  603. columns.DeserializeBinary(reader);
  604. return columns;
  605. }
  606. return null;
  607. }
  608. /// <summary>
  609. /// Inverse of <see cref="ReadColumns{T}(CoreBinaryReader)"/>.
  610. /// </summary>
  611. /// <param name="filter"></param>
  612. /// <param name="writer"></param>
  613. public static void Write<T>(this CoreBinaryWriter writer, Columns<T>? columns)
  614. {
  615. if (columns is null)
  616. {
  617. writer.Write(false);
  618. }
  619. else
  620. {
  621. writer.Write(true);
  622. columns.SerializeBinary(writer);
  623. }
  624. }
  625. }
  626. public class ColumnJsonConverter : JsonConverter
  627. {
  628. public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
  629. {
  630. if(value is null)
  631. {
  632. writer.WriteNull();
  633. return;
  634. }
  635. var property = (CoreUtils.GetPropertyValue(value, "Expression") as Expression)
  636. ?? throw new Exception("'Column.Expression' may not be null");
  637. var prop = CoreUtils.ExpressionToString(value.GetType().GenericTypeArguments[0], property, true);
  638. var name = CoreUtils.GetPropertyValue(value, "Property") as string;
  639. writer.WriteStartObject();
  640. writer.WritePropertyName("$type");
  641. writer.WriteValue(value.GetType().FullName);
  642. writer.WritePropertyName("Expression");
  643. writer.WriteValue(prop);
  644. writer.WritePropertyName("Property");
  645. writer.WriteValue(name);
  646. writer.WriteEndObject();
  647. }
  648. public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
  649. {
  650. if (reader.TokenType == JsonToken.Null)
  651. return null;
  652. var data = new Dictionary<string, object>();
  653. while (reader.TokenType != JsonToken.EndObject && reader.Read())
  654. if (reader.Value != null)
  655. {
  656. var key = reader.Value.ToString();
  657. reader.Read();
  658. if (String.Equals(key, "$type"))
  659. objectType = Type.GetType(reader.Value.ToString()) ?? objectType;
  660. else
  661. data[key] = reader.Value;
  662. }
  663. var prop = data["Property"].ToString();
  664. var result = Activator.CreateInstance(objectType, prop);
  665. return result;
  666. }
  667. public override bool CanConvert(Type objectType)
  668. {
  669. if (objectType.IsConstructedGenericType)
  670. {
  671. var ot = objectType.GetGenericTypeDefinition();
  672. var tt = typeof(Column<>);
  673. if (ot == tt)
  674. return true;
  675. }
  676. return false;
  677. }
  678. }
  679. }