BGC Tools
Public Member Functions | Private Attributes
BGC.Utility.EnumSerialization Class Reference
Collaboration diagram for BGC.Utility.EnumSerialization:
Collaboration graph
[legend]

Public Member Functions

bool AddEnumToDic< T > (T Enum)
 Adds an Enum Value to serialization More...
 
StringToEnum< T > (string str)
 Converts a String to an Enum that was added to the serialization More...
 

Private Attributes

List< Dictionary< string, int > > enumStringValuePairs = null
 
List< Dictionary< string, int > > EnumStringValuePairs => enumStringValuePairs ?? (enumStringValuePairs = new List<Dictionary<string, int>>())
 
Dictionary< Type, int > enumTypeIndexPair = null
 
Dictionary< Type, int > EnumTypeIndexPair => enumTypeIndexPair ?? (enumTypeIndexPair = new Dictionary<Type, int>())
 

Detailed Description

Definition at line 7 of file EnumSerialization.cs.

Member Function Documentation

◆ AddEnumToDic< T >()

bool BGC.Utility.EnumSerialization.AddEnumToDic< T > ( Enum)
inline

Adds an Enum Value to serialization

Definition at line 18 of file EnumSerialization.cs.

19  {
20  if (EnumTypeIndexPair.ContainsKey(Enum.GetType()) == false)
21  {
22  EnumTypeIndexPair.Add(Enum.GetType(), EnumStringValuePairs.Count);
23  EnumStringValuePairs.Add(new Dictionary<string, int>());
24  }
25 
26  int index = EnumTypeIndexPair[Enum.GetType()];
27  if (EnumStringValuePairs[index].ContainsKey(Enum.ToString()) == false)
28  {
29  int val;
30  try
31  {
32  val = (int)Convert.ChangeType(Enum, typeof(int));
33  }
34  catch (InvalidCastException)
35  {
36  UnityEngine.Debug.LogError("Enum was not passed");
37  val = default(int);
38  }
39 
40  EnumStringValuePairs[index].Add(Enum.ToString(), val);
41 
42  return true;
43  }
44 
45  return false;
46  }
List< Dictionary< string, int > > EnumStringValuePairs
Dictionary< Type, int > EnumTypeIndexPair

◆ StringToEnum< T >()

T BGC.Utility.EnumSerialization.StringToEnum< T > ( string  str)
inline

Converts a String to an Enum that was added to the serialization

Definition at line 49 of file EnumSerialization.cs.

50  {
51  Assert.IsFalse(string.IsNullOrEmpty(str));
52 
53  int index = EnumTypeIndexPair[typeof(T)];
54  T val;
55  try
56  {
57  val = (T)Enum.ToObject(typeof(T), EnumStringValuePairs[index][str]);
58  }
59  catch (InvalidCastException)
60  {
61  UnityEngine.Debug.LogError("Enum was not passed");
62  val = default(T);
63  }
64 
65  return val;
66  }
List< Dictionary< string, int > > EnumStringValuePairs
Dictionary< Type, int > EnumTypeIndexPair

Field Documentation

◆ enumStringValuePairs

List<Dictionary<string, int> > BGC.Utility.EnumSerialization.enumStringValuePairs = null
private

Definition at line 9 of file EnumSerialization.cs.

◆ EnumStringValuePairs

List<Dictionary<string, int> > BGC.Utility.EnumSerialization.EnumStringValuePairs => enumStringValuePairs ?? (enumStringValuePairs = new List<Dictionary<string, int>>())
private

Definition at line 10 of file EnumSerialization.cs.

◆ enumTypeIndexPair

Dictionary<Type, int> BGC.Utility.EnumSerialization.enumTypeIndexPair = null
private

Definition at line 13 of file EnumSerialization.cs.

◆ EnumTypeIndexPair

Dictionary<Type, int> BGC.Utility.EnumSerialization.EnumTypeIndexPair => enumTypeIndexPair ?? (enumTypeIndexPair = new Dictionary<Type, int>())
private

Definition at line 14 of file EnumSerialization.cs.


The documentation for this class was generated from the following file: