-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.cs
48 lines (41 loc) · 1.26 KB
/
Tools.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModularSkylines
{
public static class Tools
{
public static Dictionary<int, string> EnumToDictionary<T>()
{
var type = typeof(T);
return Enum.GetValues(type).Cast<int>().ToDictionary(e => e, e => Enum.GetName(type, e));
}
//Dictionary extension
public static TValue GetValueOrDefault<TKey, TValue>
(this IDictionary<TKey, TValue> dictionary, TKey key, TValue def)
{
TValue ret;
// Ignore return value
if(dictionary.TryGetValue(key, out ret))
return ret;
return def;
}
}
public class TypeDictionary
{
private Dictionary<RuntimeTypeHandle, object> internalDict;
public bool TryGetValue<T>(out DataModuleEvent<T> returnValue) where T : DataModule<T>
{
object moduleEvent;
if(internalDict.TryGetValue(typeof(T).TypeHandle,out moduleEvent))
{
returnValue = (DataModuleEvent<T>)moduleEvent;
return true;
}
returnValue = null;
return false;
}
}
}