Created
July 24, 2018 00:40
-
-
Save mjs3339/601173700a0b9bb0c4ee22524c3573c8 to your computer and use it in GitHub Desktop.
C# - ProgressReport Class to use with IProgress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProgressReport | |
{ | |
public readonly Dictionary<string, object> GValues = new Dictionary<string, object>(); | |
public List<string> ErrorLog = new List<string>(); | |
public void Set(string name, object value) | |
{ | |
if(!GValues.ContainsKey(name)) | |
GValues.Add(name, value); | |
else GValues[name] = value; | |
} | |
public bool GetBool(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToBoolean(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return false; | |
} | |
public char GetChar(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToChar(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return' '; | |
} | |
public sbyte GetSByte(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToSByte(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public byte GetByte(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToByte(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public short GetShort(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToInt16(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public ushort GetUShort(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToUInt16(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public int GetInt(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToInt32(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public uint GetUInt(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToUInt32(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public long GetLong(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToInt64(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public ulong GetULong(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToUInt64(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public float GetFloat(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToSingle(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public double GetDouble(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToDouble(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public decimal GetDecimal(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToDecimal(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return 0; | |
} | |
public string GetString(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToString(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return" "; | |
} | |
public DateTime GetDateTime(string name) | |
{ | |
if(GValues.TryGetValue(name, out var ov)) | |
try | |
{ | |
return Convert.ToDateTime(ov); | |
} | |
catch(Exception ex) | |
{ | |
if(ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString()); | |
} | |
return default; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment