Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SingletonAttribute.cs
1using System;
2using System.Collections.Generic;
3using Waher.Events;
4
6{
12 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)]
13 public class SingletonAttribute : Attribute
14 {
15 private static readonly Dictionary<SingletonKey, SingletonRecord> instances = new Dictionary<SingletonKey, SingletonRecord>();
16
23 {
24 }
25
26 internal static void Clear()
27 {
28 SingletonRecord[] Objects;
29
30 lock (instances)
31 {
32 Objects = new SingletonRecord[instances.Count];
33 instances.Values.CopyTo(Objects, 0);
34 instances.Clear();
35 }
36
37 foreach (SingletonRecord Rec in Objects)
38 {
39 if (Rec.Instantiated && Rec.Instance is IDisposable Disposable)
40 {
41 try
42 {
43 Disposable.Dispose();
44 }
45 catch (Exception ex)
46 {
47 Log.Exception(ex);
48 }
49 }
50 }
51 }
52
60 public object Instantiate(bool ReturnNullIfFail, Type Type, params object[] Arguments)
61 {
62 SingletonKey Key = new SingletonKey(Type, Arguments);
63
64 lock (instances)
65 {
66 if (instances.TryGetValue(Key, out SingletonRecord Rec))
67 return Rec.Instance;
68 }
69
70 object Object = Types.Create(ReturnNullIfFail, Type, Arguments);
71 if (Object is null)
72 return null;
73
74 lock (instances)
75 {
76 if (instances.TryGetValue(Key, out SingletonRecord Rec))
77 {
78 if (Object is IDisposable Disposable)
79 Disposable.Dispose();
80
81 return Rec.Instance;
82 }
83
84 instances[Key] = new SingletonRecord(Key, true, Object);
85 }
86
87 return Object;
88 }
89
95 public static void Register(object Object, params object[] Arguments)
96 {
97 SingletonKey Key = new SingletonKey(Object.GetType(), Arguments);
98
99 lock (instances)
100 {
101 if (instances.ContainsKey(Key))
102 throw new InvalidOperationException("Singleton already registered.");
103
104 instances[Key] = new SingletonRecord(Key, false, Object);
105 }
106 }
107
114 public static bool Unregister(object Object, params object[] Arguments)
115 {
116 SingletonKey Key = new SingletonKey(Object.GetType(), Arguments);
117
118 lock (instances)
119 {
120 return instances.Remove(Key);
121 }
122 }
123
130 public static bool IsRegistered(Type Type, params object[] Arguments)
131 {
132 SingletonKey Key = new SingletonKey(Type, Arguments);
133
134 lock (instances)
135 {
136 return instances.ContainsKey(Key);
137 }
138 }
139
145 {
146 lock (instances)
147 {
148 SingletonRecord[] Result = new SingletonRecord[instances.Count];
149 instances.Values.CopyTo(Result, 0);
150 return Result;
151 }
152 }
153 }
154}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
Defines a class or struct as singleton. This means that when instantiated, using Types....
SingletonAttribute()
Defines a class or struct as singleton. This means that when instantiated, using Types....
static bool Unregister(object Object, params object[] Arguments)
Unregisters a singleton instance of a type.
static SingletonRecord[] GetInstances()
Gets available singleton instances.
object Instantiate(bool ReturnNullIfFail, Type Type, params object[] Arguments)
Returns an instance of the type Type .
static bool IsRegistered(Type Type, params object[] Arguments)
Checks if a singleton type (with optional associated arguments) is registered.
static void Register(object Object, params object[] Arguments)
Registers a singleton instance of a type.
Represents a type and a set of arguments, for which an object instance is the single instantiation.
Definition: SingletonKey.cs:10
A record of a singleton instance in memory.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static object Create(bool ReturnNullIfFail, Type Type, params object[] Arguments)
Returns an instance of the type Type . Creates an instance of a type. If the constructor requires arg...
Definition: Types.cs:1237