PerspexProperty.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reactive.Subjects;
  6. using System.Reflection;
  7. using Perspex.Utilities;
  8. namespace Perspex
  9. {
  10. /// <summary>
  11. /// A perspex property.
  12. /// </summary>
  13. /// <remarks>
  14. /// This class is analogous to DependencyProperty in WPF.
  15. /// </remarks>
  16. public class PerspexProperty : IEquatable<PerspexProperty>
  17. {
  18. /// <summary>
  19. /// Represents an unset property value.
  20. /// </summary>
  21. public static readonly object UnsetValue = new Unset();
  22. /// <summary>
  23. /// Gets the next ID that will be allocated to a property.
  24. /// </summary>
  25. private static int s_nextId = 1;
  26. /// <summary>
  27. /// The default values for the property, by type.
  28. /// </summary>
  29. private readonly Dictionary<Type, object> _defaultValues = new Dictionary<Type, object>();
  30. /// <summary>
  31. /// Observable fired when this property changes on any <see cref="PerspexObject"/>.
  32. /// </summary>
  33. private readonly Subject<PerspexPropertyChangedEventArgs> _initialized = new Subject<PerspexPropertyChangedEventArgs>();
  34. /// <summary>
  35. /// Observable fired when this property changes on any <see cref="PerspexObject"/>.
  36. /// </summary>
  37. private readonly Subject<PerspexPropertyChangedEventArgs> _changed = new Subject<PerspexPropertyChangedEventArgs>();
  38. /// <summary>
  39. /// The validation functions for the property, by type.
  40. /// </summary>
  41. private readonly Dictionary<Type, Func<PerspexObject, object, object>> _validation =
  42. new Dictionary<Type, Func<PerspexObject, object, object>>();
  43. /// <summary>
  44. /// Gets the ID of the property.
  45. /// </summary>
  46. private int _id;
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="PerspexProperty"/> class.
  49. /// </summary>
  50. /// <param name="name">The name of the property.</param>
  51. /// <param name="valueType">The type of the property's value.</param>
  52. /// <param name="ownerType">The type of the class that registers the property.</param>
  53. /// <param name="defaultValue">The default value of the property.</param>
  54. /// <param name="inherits">Whether the property inherits its value.</param>
  55. /// <param name="defaultBindingMode">The default binding mode for the property.</param>
  56. /// <param name="validate">A validation function.</param>
  57. /// <param name="isAttached">Whether the property is an attached property.</param>
  58. public PerspexProperty(
  59. string name,
  60. Type valueType,
  61. Type ownerType,
  62. object defaultValue,
  63. bool inherits = false,
  64. BindingMode defaultBindingMode = BindingMode.Default,
  65. Func<PerspexObject, object, object> validate = null,
  66. bool isAttached = false)
  67. {
  68. Contract.Requires<ArgumentNullException>(name != null);
  69. Contract.Requires<ArgumentNullException>(valueType != null);
  70. Contract.Requires<ArgumentNullException>(ownerType != null);
  71. if (name.Contains("."))
  72. {
  73. throw new ArgumentException("'name' may not contain periods.");
  74. }
  75. Name = name;
  76. PropertyType = valueType;
  77. OwnerType = ownerType;
  78. _defaultValues.Add(ownerType, defaultValue);
  79. Inherits = inherits;
  80. DefaultBindingMode = defaultBindingMode;
  81. IsAttached = isAttached;
  82. _id = s_nextId++;
  83. if (validate != null)
  84. {
  85. _validation.Add(ownerType, validate);
  86. }
  87. }
  88. /// <summary>
  89. /// Initializes a new instance of the <see cref="PerspexProperty"/> class.
  90. /// </summary>
  91. /// <param name="name">The name of the property.</param>
  92. /// <param name="valueType">The type of the property's value.</param>
  93. /// <param name="ownerType">The type of the class that registers the property.</param>
  94. /// <param name="getter">Gets the current value of the property.</param>
  95. /// <param name="setter">Sets the value of the property.</param>
  96. public PerspexProperty(
  97. string name,
  98. Type valueType,
  99. Type ownerType,
  100. Func<PerspexObject, object> getter,
  101. Action<PerspexObject, object> setter)
  102. {
  103. Contract.Requires<ArgumentNullException>(name != null);
  104. Contract.Requires<ArgumentNullException>(valueType != null);
  105. Contract.Requires<ArgumentNullException>(ownerType != null);
  106. Contract.Requires<ArgumentNullException>(getter != null);
  107. if (name.Contains("."))
  108. {
  109. throw new ArgumentException("'name' may not contain periods.");
  110. }
  111. Name = name;
  112. PropertyType = valueType;
  113. OwnerType = ownerType;
  114. Getter = getter;
  115. Setter = setter;
  116. IsDirect = true;
  117. _id = s_nextId++;
  118. }
  119. /// <summary>
  120. /// Initializes a new instance of the <see cref="PerspexProperty"/> class.
  121. /// </summary>
  122. /// <param name="source">The direct property to copy.</param>
  123. /// <param name="getter">A new getter.</param>
  124. /// <param name="setter">A new setter.</param>
  125. protected PerspexProperty(
  126. PerspexProperty source,
  127. Func<PerspexObject, object> getter,
  128. Action<PerspexObject, object> setter)
  129. {
  130. Contract.Requires<ArgumentNullException>(source != null);
  131. Contract.Requires<ArgumentNullException>(getter != null);
  132. if (!source.IsDirect)
  133. {
  134. throw new InvalidOperationException(
  135. "This method can only be called on direct PerspexProperties.");
  136. }
  137. Name = source.Name;
  138. PropertyType = source.PropertyType;
  139. OwnerType = source.OwnerType;
  140. Getter = getter;
  141. Setter = setter;
  142. IsDirect = true;
  143. _id = source._id;
  144. }
  145. /// <summary>
  146. /// Gets the name of the property.
  147. /// </summary>
  148. /// <value>
  149. /// The name of the property.
  150. /// </value>
  151. public string Name { get; }
  152. /// <summary>
  153. /// Gets the type of the property's value.
  154. /// </summary>
  155. /// <value>
  156. /// The type of the property's value.
  157. /// </value>
  158. public Type PropertyType { get; }
  159. /// <summary>
  160. /// Gets the type of the class that registers the property.
  161. /// </summary>
  162. /// <value>
  163. /// The type of the class that registers the property.
  164. /// </value>
  165. public Type OwnerType { get; }
  166. /// <summary>
  167. /// Gets a value indicating whether the property inherits its value.
  168. /// </summary>
  169. /// <value>
  170. /// A value indicating whether the property inherits its value.
  171. /// </value>
  172. public bool Inherits { get; }
  173. /// <summary>
  174. /// Gets the default binding mode for the property.
  175. /// </summary>
  176. /// <value>
  177. /// The default binding mode for the property.
  178. /// </value>
  179. public BindingMode DefaultBindingMode { get; }
  180. /// <summary>
  181. /// Gets a value indicating whether this is an attached property.
  182. /// </summary>
  183. /// <value>
  184. /// A value indicating whether this is an attached property.
  185. /// </value>
  186. public bool IsAttached { get; }
  187. /// <summary>
  188. /// Gets a value indicating whether this is a direct property.
  189. /// </summary>
  190. public bool IsDirect { get; }
  191. /// <summary>
  192. /// Gets an observable that is fired when this property is initialized on a
  193. /// new <see cref="PerspexObject"/> instance.
  194. /// </summary>
  195. /// <remarks>
  196. /// This observable is fired each time a new <see cref="PerspexObject"/> is constructed
  197. /// for all properties registered on the object's type. The default value of the property
  198. /// for the object is passed in the args' NewValue (OldValue will always be
  199. /// <see cref="UnsetValue"/>.
  200. /// </remarks>
  201. /// <value>
  202. /// An observable that is fired when this property is initialized on a new
  203. /// <see cref="PerspexObject"/> instance.
  204. /// </value>
  205. public IObservable<PerspexPropertyChangedEventArgs> Initialized => _initialized;
  206. /// <summary>
  207. /// Gets an observable that is fired when this property changes on any
  208. /// <see cref="PerspexObject"/> instance.
  209. /// </summary>
  210. /// <value>
  211. /// An observable that is fired when this property changes on any
  212. /// <see cref="PerspexObject"/> instance.
  213. /// </value>
  214. public IObservable<PerspexPropertyChangedEventArgs> Changed => _changed;
  215. /// <summary>
  216. /// Provides access to a property's binding via the <see cref="PerspexObject"/>
  217. /// indexer.
  218. /// </summary>
  219. /// <param name="property">The property.</param>
  220. /// <returns>A <see cref="BindingDescriptor"/> describing the binding.</returns>
  221. public static BindingDescriptor operator !(PerspexProperty property)
  222. {
  223. return new BindingDescriptor
  224. {
  225. Priority = BindingPriority.LocalValue,
  226. Property = property,
  227. };
  228. }
  229. /// <summary>
  230. /// Provides access to a property's template binding via the <see cref="PerspexObject"/>
  231. /// indexer.
  232. /// </summary>
  233. /// <param name="property">The property.</param>
  234. /// <returns>A <see cref="BindingDescriptor"/> describing the binding.</returns>
  235. public static BindingDescriptor operator ~(PerspexProperty property)
  236. {
  237. return new BindingDescriptor
  238. {
  239. Priority = BindingPriority.TemplatedParent,
  240. Property = property,
  241. };
  242. }
  243. /// <summary>
  244. /// Gets the getter function for direct properties.
  245. /// </summary>
  246. internal Func<PerspexObject, object> Getter { get; }
  247. /// <summary>
  248. /// Gets the etter function for direct properties.
  249. /// </summary>
  250. internal Action<PerspexObject, object> Setter { get; }
  251. /// <summary>
  252. /// Tests two <see cref="PerspexProperty"/>s for equality.
  253. /// </summary>
  254. /// <param name="a">The first property.</param>
  255. /// <param name="b">The second property.</param>
  256. /// <returns>True if the properties are equal, otherwise false.</returns>
  257. public static bool operator ==(PerspexProperty a, PerspexProperty b)
  258. {
  259. if (object.ReferenceEquals(a, b))
  260. {
  261. return true;
  262. }
  263. else if (((object)a == null) || ((object)b == null))
  264. {
  265. return false;
  266. }
  267. else
  268. {
  269. return a.Equals(b);
  270. }
  271. }
  272. /// <summary>
  273. /// Tests two <see cref="PerspexProperty"/>s for unequality.
  274. /// </summary>
  275. /// <param name="a">The first property.</param>
  276. /// <param name="b">The second property.</param>
  277. /// <returns>True if the properties are equal, otherwise false.</returns>
  278. public static bool operator !=(PerspexProperty a, PerspexProperty b)
  279. {
  280. return !(a == b);
  281. }
  282. /// <summary>
  283. /// Registers a <see cref="PerspexProperty"/>.
  284. /// </summary>
  285. /// <typeparam name="TOwner">The type of the class that is registering the property.</typeparam>
  286. /// <typeparam name="TValue">The type of the property's value.</typeparam>
  287. /// <param name="name">The name of the property.</param>
  288. /// <param name="defaultValue">The default value of the property.</param>
  289. /// <param name="inherits">Whether the property inherits its value.</param>
  290. /// <param name="defaultBindingMode">The default binding mode for the property.</param>
  291. /// <param name="validate">A validation function.</param>
  292. /// <returns>A <see cref="PerspexProperty{TValue}"/></returns>
  293. public static PerspexProperty<TValue> Register<TOwner, TValue>(
  294. string name,
  295. TValue defaultValue = default(TValue),
  296. bool inherits = false,
  297. BindingMode defaultBindingMode = BindingMode.OneWay,
  298. Func<TOwner, TValue, TValue> validate = null)
  299. where TOwner : PerspexObject
  300. {
  301. Contract.Requires<ArgumentNullException>(name != null);
  302. PerspexProperty<TValue> result = new PerspexProperty<TValue>(
  303. name,
  304. typeof(TOwner),
  305. defaultValue,
  306. inherits,
  307. defaultBindingMode,
  308. Cast(validate),
  309. false);
  310. PerspexObject.Register(typeof(TOwner), result);
  311. return result;
  312. }
  313. /// <summary>
  314. /// Registers a direct <see cref="PerspexProperty"/>.
  315. /// </summary>
  316. /// <typeparam name="TOwner">The type of the class that is registering the property.</typeparam>
  317. /// <typeparam name="TValue">The type of the property's value.</typeparam>
  318. /// <param name="name">The name of the property.</param>
  319. /// <param name="getter">Gets the current value of the property.</param>
  320. /// <param name="setter">Sets the value of the property.</param>
  321. /// <returns>A <see cref="PerspexProperty{TValue}"/></returns>
  322. public static PerspexProperty<TValue> RegisterDirect<TOwner, TValue>(
  323. string name,
  324. Func<TOwner, TValue> getter,
  325. Action<TOwner, TValue> setter = null)
  326. where TOwner : PerspexObject
  327. {
  328. Contract.Requires<ArgumentNullException>(name != null);
  329. PerspexProperty<TValue> result = new PerspexProperty<TValue>(
  330. name,
  331. typeof(TOwner),
  332. Cast(getter),
  333. Cast(setter));
  334. PerspexObject.Register(typeof(TOwner), result);
  335. return result;
  336. }
  337. /// <summary>
  338. /// Registers an attached <see cref="PerspexProperty"/>.
  339. /// </summary>
  340. /// <typeparam name="TOwner">The type of the class that is registering the property.</typeparam>
  341. /// <typeparam name="THost">The type of the class that the property is to be registered on.</typeparam>
  342. /// <typeparam name="TValue">The type of the property's value.</typeparam>
  343. /// <param name="name">The name of the property.</param>
  344. /// <param name="defaultValue">The default value of the property.</param>
  345. /// <param name="inherits">Whether the property inherits its value.</param>
  346. /// <param name="defaultBindingMode">The default binding mode for the property.</param>
  347. /// <param name="validate">A validation function.</param>
  348. /// <returns>A <see cref="PerspexProperty{TValue}"/></returns>
  349. public static PerspexProperty<TValue> RegisterAttached<TOwner, THost, TValue>(
  350. string name,
  351. TValue defaultValue = default(TValue),
  352. bool inherits = false,
  353. BindingMode defaultBindingMode = BindingMode.OneWay,
  354. Func<PerspexObject, TValue, TValue> validate = null)
  355. {
  356. Contract.Requires<ArgumentNullException>(name != null);
  357. PerspexProperty<TValue> result = new PerspexProperty<TValue>(
  358. name,
  359. typeof(TOwner),
  360. defaultValue,
  361. inherits,
  362. defaultBindingMode,
  363. validate,
  364. true);
  365. PerspexObject.Register(typeof(THost), result);
  366. return result;
  367. }
  368. /// <summary>
  369. /// Registers an attached <see cref="PerspexProperty"/>.
  370. /// </summary>
  371. /// <typeparam name="THost">The type of the class that the property is to be registered on.</typeparam>
  372. /// <typeparam name="TValue">The type of the property's value.</typeparam>
  373. /// <param name="name">The name of the property.</param>
  374. /// <param name="ownerType">The type of the class that is registering the property.</param>
  375. /// <param name="defaultValue">The default value of the property.</param>
  376. /// <param name="inherits">Whether the property inherits its value.</param>
  377. /// <param name="defaultBindingMode">The default binding mode for the property.</param>
  378. /// <param name="validate">A validation function.</param>
  379. /// <returns>A <see cref="PerspexProperty{TValue}"/></returns>
  380. public static PerspexProperty<TValue> RegisterAttached<THost, TValue>(
  381. string name,
  382. Type ownerType,
  383. TValue defaultValue = default(TValue),
  384. bool inherits = false,
  385. BindingMode defaultBindingMode = BindingMode.OneWay,
  386. Func<PerspexObject, TValue, TValue> validate = null)
  387. {
  388. Contract.Requires<ArgumentNullException>(name != null);
  389. PerspexProperty<TValue> result = new PerspexProperty<TValue>(
  390. name,
  391. ownerType,
  392. defaultValue,
  393. inherits,
  394. defaultBindingMode,
  395. validate,
  396. true);
  397. PerspexObject.Register(typeof(THost), result);
  398. return result;
  399. }
  400. /// <inheritdoc/>
  401. public override bool Equals(object obj)
  402. {
  403. var p = obj as PerspexProperty;
  404. return p != null ? Equals(p) : false;
  405. }
  406. /// <inheritdoc/>
  407. public bool Equals(PerspexProperty other)
  408. {
  409. return other != null && _id == other._id;
  410. }
  411. /// <inheritdoc/>
  412. public override int GetHashCode()
  413. {
  414. return _id;
  415. }
  416. /// <summary>
  417. /// Returns a binding accessor that can be passed to <see cref="PerspexObject"/>'s []
  418. /// operator to initiate a binding.
  419. /// </summary>
  420. /// <returns>A <see cref="BindingDescriptor"/>.</returns>
  421. /// <remarks>
  422. /// The ! and ~ operators are short forms of this.
  423. /// </remarks>
  424. public BindingDescriptor Bind()
  425. {
  426. return new BindingDescriptor
  427. {
  428. Property = this,
  429. };
  430. }
  431. /// <summary>
  432. /// Gets the default value for the property on the specified type.
  433. /// </summary>
  434. /// <param name="type">The type.</param>
  435. /// <returns>The default value.</returns>
  436. public object GetDefaultValue(Type type)
  437. {
  438. Contract.Requires<ArgumentNullException>(type != null);
  439. while (type != null)
  440. {
  441. object result;
  442. if (_defaultValues.TryGetValue(type, out result))
  443. {
  444. return result;
  445. }
  446. type = type.GetTypeInfo().BaseType;
  447. }
  448. return _defaultValues[OwnerType];
  449. }
  450. /// <summary>
  451. /// Gets the validation function for the property on the specified type.
  452. /// </summary>
  453. /// <param name="type">The type.</param>
  454. /// <returns>
  455. /// The validation function, or null if no validation function registered for this type.
  456. /// </returns>
  457. public Func<PerspexObject, object, object> GetValidationFunc(Type type)
  458. {
  459. Contract.Requires<ArgumentNullException>(type != null);
  460. while (type != null)
  461. {
  462. Func<PerspexObject, object, object> result;
  463. if (_validation.TryGetValue(type, out result))
  464. {
  465. return result;
  466. }
  467. type = type.GetTypeInfo().BaseType;
  468. }
  469. return null;
  470. }
  471. /// <summary>
  472. /// Checks whether the <paramref name="value"/> is valid for the property.
  473. /// </summary>
  474. /// <param name="value">The value.</param>
  475. /// <returns>True if the value is valid, otherwise false.</returns>
  476. public bool IsValidValue(object value)
  477. {
  478. return TypeUtilities.TryCast(PropertyType, value, out value);
  479. }
  480. /// <summary>
  481. /// Overrides the default value for the property on the specified type.
  482. /// </summary>
  483. /// <typeparam name="T">The type.</typeparam>
  484. /// <param name="defaultValue">The default value.</param>
  485. public void OverrideDefaultValue<T>(object defaultValue)
  486. {
  487. OverrideDefaultValue(typeof(T), defaultValue);
  488. }
  489. /// <summary>
  490. /// Overrides the default value for the property on the specified type.
  491. /// </summary>
  492. /// <param name="type">The type.</param>
  493. /// <param name="defaultValue">The default value.</param>
  494. public void OverrideDefaultValue(Type type, object defaultValue)
  495. {
  496. Contract.Requires<ArgumentNullException>(type != null);
  497. if (!TypeUtilities.TryCast(PropertyType, defaultValue, out defaultValue))
  498. {
  499. throw new InvalidOperationException(string.Format(
  500. "Invalid value for Property '{0}': {1} ({2})",
  501. Name,
  502. defaultValue,
  503. defaultValue.GetType().FullName));
  504. }
  505. if (_defaultValues.ContainsKey(type))
  506. {
  507. throw new InvalidOperationException("Default value is already set for this property.");
  508. }
  509. _defaultValues.Add(type, defaultValue);
  510. }
  511. /// <summary>
  512. /// Overrides the validation function for the property on the specified type.
  513. /// </summary>
  514. /// <param name="type">The type.</param>
  515. /// <param name="validation">The validation function.</param>
  516. public void OverrideValidation(Type type, Func<PerspexObject, object, object> validation)
  517. {
  518. Contract.Requires<ArgumentNullException>(type != null);
  519. if (_validation.ContainsKey(type))
  520. {
  521. throw new InvalidOperationException("Validation is already set for this property.");
  522. }
  523. _validation.Add(type, validation);
  524. }
  525. /// <summary>
  526. /// Gets the string representation of the property.
  527. /// </summary>
  528. /// <returns>The property's string representation.</returns>
  529. public override string ToString()
  530. {
  531. return Name;
  532. }
  533. /// <summary>
  534. /// Notifies the <see cref="Initialized"/> observable.
  535. /// </summary>
  536. /// <param name="e">The observable arguments.</param>
  537. internal void NotifyInitialized(PerspexPropertyChangedEventArgs e)
  538. {
  539. _initialized.OnNext(e);
  540. }
  541. /// <summary>
  542. /// Notifies the <see cref="Changed"/> observable.
  543. /// </summary>
  544. /// <param name="e">The observable arguments.</param>
  545. internal void NotifyChanged(PerspexPropertyChangedEventArgs e)
  546. {
  547. _changed.OnNext(e);
  548. }
  549. /// <summary>
  550. /// Casts a getter function accepting a typed owner to one accepting a
  551. /// <see cref="PerspexObject"/>.
  552. /// </summary>
  553. /// <typeparam name="TOwner">The owner type.</typeparam>
  554. /// <typeparam name="TValue">The property value type.</typeparam>
  555. /// <param name="f">The typed function.</param>
  556. /// <returns>The untyped function.</returns>
  557. private static Func<PerspexObject, TValue> Cast<TOwner, TValue>(Func<TOwner, TValue> f)
  558. where TOwner : PerspexObject
  559. {
  560. return (f != null) ? o => f((TOwner)o) : (Func<PerspexObject, TValue >)null;
  561. }
  562. /// <summary>
  563. /// Casts a setter action accepting a typed owner to one accepting a
  564. /// <see cref="PerspexObject"/>.
  565. /// </summary>
  566. /// <typeparam name="TOwner">The owner type.</typeparam>
  567. /// <typeparam name="TValue">The property value type.</typeparam>
  568. /// <param name="f">The typed action.</param>
  569. /// <returns>The untyped action.</returns>
  570. private static Action<PerspexObject, TValue> Cast<TOwner, TValue>(Action<TOwner, TValue> f)
  571. where TOwner : PerspexObject
  572. {
  573. return f != null ? (o, v) => f((TOwner)o, v) : (Action<PerspexObject, TValue>)null;
  574. }
  575. /// <summary>
  576. /// Casts a validation function accepting a typed owner to one accepting a
  577. /// <see cref="PerspexObject"/>.
  578. /// </summary>
  579. /// <typeparam name="TOwner">The owner type.</typeparam>
  580. /// <typeparam name="TValue">The property value type.</typeparam>
  581. /// <param name="f">The typed function.</param>
  582. /// <returns>The untyped function.</returns>
  583. private static Func<PerspexObject, TValue, TValue> Cast<TOwner, TValue>(Func<TOwner, TValue, TValue> f)
  584. where TOwner : PerspexObject
  585. {
  586. return f != null ? (o, v) => f((TOwner)o, v) : (Func<PerspexObject, TValue, TValue>)null;
  587. }
  588. /// <summary>
  589. /// Class representing the <see cref="UnsetValue"/>.
  590. /// </summary>
  591. private class Unset
  592. {
  593. /// <summary>
  594. /// Returns the string representation of the <see cref="UnsetValue"/>.
  595. /// </summary>
  596. /// <returns>The string "(unset)".</returns>
  597. public override string ToString()
  598. {
  599. return "(unset)";
  600. }
  601. }
  602. }
  603. }