-
Notifications
You must be signed in to change notification settings - Fork 0
/
Disposable.cs
270 lines (229 loc) · 6.43 KB
/
Disposable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Reflection;
using LongOffset = System.UInt32;
using Offset = System.UInt16;
namespace CoreObjects.Disposable
{
[Serializable()]
public abstract class Base : IDisposable, ICloneable
{
protected delegate void CloneOccurredHandler(ref object NewObject);
protected event CloneOccurredHandler CloneOccurred;
private ArrayList _errors;
protected const Offset __NullOffset = 0;
protected const LongOffset __NullLongOffset = 0;
#region [ Contructors ]
public Base()
{
if (disposed)
{
GC.ReRegisterForFinalize(true);
}
disposed = false;
_errors = new ArrayList();
// ---------------------------------------------------------------
// Do Not put Initialize(true) here. That causes major problems!
// ---------------------------------------------------------------
}
#endregion
#region [ Destructors ]
protected bool disposed;
public void Dispose()
{
// Execute the code that does the cleanup.
Dispose(true);
// Let the common language runtime know that Finalize doesn't have to be called.
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
// Exit if we've already cleaned up this object.
if (disposed)
return;
if (disposing)
{
// ny General Cleanup goes here
Initialize(false);
_errors?.Clear();
}
// Remember that we've executed this code
disposed = true;
}
~Base()
{
//Execute the code that does the cleanup.
Dispose(false);
}
#endregion
protected abstract void Initialize(bool setObjects);
protected void SetErrorInformation(System.Exception ex)
{
var ErrorHandler = new ErrorHandler(ex);
var CallStack = new StackTrace(); //The call stack
var Frame = CallStack.GetFrame(1); //The frame that called me
var Method = Frame.GetMethod(); //The method that called me
_errors.Clear();
ErrorHandler.SetErrorInformation(Method.DeclaringType.FullName + "." + Method.Name, ex);
_errors.Add(ErrorHandler.Description);
ErrorHandler = null;
Method = null;
Frame = null;
CallStack = null;
CheckErrors();
}
protected void SetErrorInformation(string addlInfo, System.Exception ex)
{
var ErrorHandler = new ErrorHandler();
var CallStack = new StackTrace(); // The call stack
var Frame = CallStack.GetFrame(1); // The frame that called me
var Method = Frame.GetMethod(); // The method that called me
_errors.Clear();
ErrorHandler.SetErrorInformation(Method.DeclaringType.FullName + "." + Method.Name, addlInfo, ex);
_errors.Add(ErrorHandler.Description);
ErrorHandler = null;
Method = null;
Frame = null;
CallStack = null;
CheckErrors();
}
/// <summary>
/// If there is an error int he array, then throws the error. Also spits out the information into the Debug pane.
/// </summary>
protected void CheckErrors()
{
var ErrorMessage = string.Empty;
if (_errors.Count > 0)
{
ErrorMessage = (string)_errors[0];
_errors.Clear();
Debug.WriteLine(ErrorMessage);
throw new Exception(ErrorMessage);
}
}
protected void CheckErrors(ref ErrorHandler ErrorHandler)
{
if (ErrorHandler != null)
{
if (ErrorHandler.Description.Length > 0)
throw new System.Exception(ErrorHandler.Description);
ErrorHandler = null;
}
}
protected void Destroy(ref SqlDataReader objectToSacrifice)
{
if (objectToSacrifice != null)
{
if (!objectToSacrifice.IsClosed)
{
objectToSacrifice.Close();
}
objectToSacrifice = null;
}
}
protected void Destroy(ref Object objectToSacrifice)
{
if (objectToSacrifice != null)
{
((IDisposable)objectToSacrifice).Dispose();
objectToSacrifice = null;
}
}
protected void Destroy(ref Base objectToSacrifice)
{
if (objectToSacrifice != null)
{
objectToSacrifice.Dispose();
objectToSacrifice = null;
}
}
protected bool IsAnonymous()
{
return System.Security.Principal.WindowsIdentity.GetCurrent().IsAnonymous;
}
protected bool IsWeb()
{
return (System.Web.HttpContext.Current != null);
}
protected string GetUserID()
{
try
{
if (System.Web.HttpContext.Current == null)
{
return System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}
else
{
return System.Web.HttpContext.Current.User.Identity.Name;
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
protected string DbSafeString(string input)
{
return "'" + input.Replace("'", "''") + "'";
}
protected string DbSafeString(string input, bool wildCardSearch)
{
return "'%" + input.Replace("'", "''") + "%'";
}
///<summary>
///Creates a shallow copy, value type objects are copied, reference type objects are not!
///</summary>
public virtual object Clone()
{
object Obj = this.MemberwiseClone();
CloneOccurred?.Invoke(ref Obj);
return Obj;
}
// typeName must be a fully formed data type name:
// ex. CMSData.ECP.V2.Location.Item
public object ObjectFactory(string typeName)
{
// Add the assembly name to the back. This is assuming that the first namespace
// entry for this class is, indeed, the assembly name. (Dll name)
// ex. CMSData.ECP.Location.Item, the assembly name would be CMSData
typeName += ", " + typeName.Substring(0, typeName.IndexOf("."));
return Activator.CreateInstance(ResolveType(typeName));
}
// the typestring is formatted thusly: ClassName, AssemblyName
// ex. CMSData.ECP.Location.List, CMSData
private System.Type ResolveType(string typeString)
{
int CommaIndex = 1;
string ClassName = null;
string AssemblyName = null;
Assembly TargetAssembly = null;
try
{
CommaIndex = typeString.LastIndexOf(",");
ClassName = typeString.Substring(0, CommaIndex).Trim();
AssemblyName = typeString.Substring(CommaIndex + 1).Trim();
TargetAssembly = Assembly.Load(AssemblyName);
}
catch
{
try
{ TargetAssembly = Assembly.Load(AssemblyName); }
catch
{ throw new ArgumentException("Can't load assembly " + AssemblyName); }
}
//Get the type
return TargetAssembly.GetType(ClassName, false, true);
}
public static bool IsNumeric(object Object)
{
return Microsoft.VisualBasic.Information.IsNumeric(Object);
}
public static bool IsDate(object Object)
{
return Microsoft.VisualBasic.Information.IsDate(Object);
}
}
}