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;
5
7{
11 public sealed class ObjectValue : Element, IDisposable
12 {
13 private static readonly ObjectValues associatedSet = new ObjectValues();
14
15 private object value;
16
21 public ObjectValue(object Value)
22 {
23 this.value = Value;
24 }
25
29 public object Value
30 {
31 get => this.value;
32 set => this.value = value;
33 }
34
36 public override string ToString()
37 {
38 if (this.value is null)
39 return "null";
40 else
41 return Expression.ToString(this.value);
42 }
43
47 public override ISet AssociatedSet
48 {
49 get { return associatedSet; }
50 }
51
55 public override object AssociatedObjectValue => this.value;
56
58 public override bool Equals(object obj)
59 {
60 if (!(obj is IElement E))
61 return false;
62
63 object Obj = E.AssociatedObjectValue;
64
65 if ((this.value is null) ^ (Obj is null))
66 return false;
67
68 if (this.value is null)
69 return true;
70
71 return this.value.Equals(Obj);
72 }
73
75 public override int GetHashCode()
76 {
77 if (this.value is null)
78 return 0;
79 else
80 return this.value.GetHashCode();
81 }
82
86 public static readonly ObjectValue Null = new ObjectValue(null);
87
94 public override bool TryConvertTo(Type DesiredType, out object Value)
95 {
96 TypeInfo TI = DesiredType.GetTypeInfo();
97
98 if (this.value is null)
99 {
100 Value = null;
101 return !TI.IsValueType || !(Nullable.GetUnderlyingType(DesiredType) is null);
102 }
103 else
104 {
105 if (TI.IsAssignableFrom(this.value.GetType().GetTypeInfo()))
106 {
107 Value = this.value;
108 return true;
109 }
110 else if (TI.IsAssignableFrom(typeof(ObjectValue).GetTypeInfo()))
111 {
112 Value = this;
113 return true;
114 }
115 else
116 return Expression.TryConvert(this.value, DesiredType, out Value);
117 }
118 }
119
123 public void Dispose()
124 {
125 if (this.value is IDisposable D)
126 D.Dispose();
127 }
128 }
129}
Base class for all types of elements.
Definition: Element.cs:13
Class managing a script expression.
Definition: Expression.cs:39
static bool TryConvert(object Value, Type DesiredType, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5268
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
void Dispose()
IDisposable.Dispose
Definition: ObjectValue.cs:123
ObjectValue(object Value)
Object value.
Definition: ObjectValue.cs:21
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
object Value
Object value.
Definition: ObjectValue.cs:30
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
Definition: ObjectValue.cs:94
override ISet AssociatedSet
Associated Set.
Definition: ObjectValue.cs:48
override bool Equals(object obj)
Compares the element to another. If elements are equal.
Definition: ObjectValue.cs:58
override object AssociatedObjectValue
Associated object value.
Definition: ObjectValue.cs:55
override int GetHashCode()
Calculates a hash code of the element. Hash code.
Definition: ObjectValue.cs:75
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for all types of sets.
Definition: ISet.cs:10