Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ObjectValue.cs
1using System;
2using System.Reflection;
5using Waher.Events;
6using System.Threading.Tasks;
7
9{
13 public sealed class ObjectValue : Element, IDisposableAsync
14 {
15 private static readonly ObjectValues associatedSet = new ObjectValues();
16
17 private object value;
18
23 public ObjectValue(object Value)
24 {
25 this.value = Value;
26 }
27
31 public object Value
32 {
33 get => this.value;
34 set => this.value = value;
35 }
36
38 public override string ToString()
39 {
40 if (this.value is null)
41 return "null";
42 else
43 return Expression.ToExpressionString(this.value);
44 }
45
49 public override ISet AssociatedSet
50 {
51 get { return associatedSet; }
52 }
53
57 public override object AssociatedObjectValue => this.value;
58
60 public override bool Equals(object obj)
61 {
62 if (!(obj is IElement E))
63 return false;
64
65 object Obj = E.AssociatedObjectValue;
66
67 if ((this.value is null) ^ (Obj is null))
68 return false;
69
70 if (this.value is null)
71 return true;
72
73 return this.value.Equals(Obj);
74 }
75
77 public override int GetHashCode()
78 {
79 if (this.value is null)
80 return 0;
81 else
82 return this.value.GetHashCode();
83 }
84
88 public static readonly ObjectValue Null = new ObjectValue(null);
89
96 public override bool TryConvertTo(Type DesiredType, out object Value)
97 {
98 TypeInfo TI = DesiredType.GetTypeInfo();
99
100 if (this.value is null)
101 {
102 Value = null;
103 return !TI.IsValueType || !(Nullable.GetUnderlyingType(DesiredType) is null);
104 }
105 else
106 {
107 if (TI.IsAssignableFrom(this.value.GetType().GetTypeInfo()))
108 {
109 Value = this.value;
110 return true;
111 }
112 else if (TI.IsAssignableFrom(typeof(ObjectValue).GetTypeInfo()))
113 {
114 Value = this;
115 return true;
116 }
117 else
118 return Expression.TryConvert(this.value, DesiredType, true, out Value);
119 }
120 }
121
125 [Obsolete("Use DisposeAsync() instead.")]
126 public void Dispose()
127 {
128 this.DisposeAsync().Wait();
129 }
130
134 public async Task DisposeAsync()
135 {
136 if (this.value is IDisposableAsync DAsync)
137 await DAsync.DisposeAsync();
138 else if (this.value is IDisposable D)
139 D.Dispose();
140 }
141 }
142}
Base class for all types of elements.
Definition: Element.cs:14
Class managing a script expression.
Definition: Expression.cs:41
static bool TryConvert(object Value, Type DesiredType, bool AcceptInformationLoss, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5530
static string ToExpressionString(object Value)
Converts an object to a string, that can be parsed as part of an expression.
Definition: Expression.cs:5052
void Dispose()
IDisposable.Dispose
Definition: ObjectValue.cs:126
ObjectValue(object Value)
Object value.
Definition: ObjectValue.cs:23
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
object Value
Object value.
Definition: ObjectValue.cs:32
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
Definition: ObjectValue.cs:96
override ISet AssociatedSet
Associated Set.
Definition: ObjectValue.cs:50
override bool Equals(object obj)
Compares the element to another. If elements are equal.
Definition: ObjectValue.cs:60
async Task DisposeAsync()
IDisposableAsync.DisposeAsync
Definition: ObjectValue.cs:134
override object AssociatedObjectValue
Associated object value.
Definition: ObjectValue.cs:57
override int GetHashCode()
Calculates a hash code of the element. Hash code.
Definition: ObjectValue.cs:77
Interface for asynchronously disposable objects.
Basic interface for all types of elements.
Definition: IElement.cs:21
Basic interface for all types of sets.
Definition: ISet.cs:10