diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-06-17 16:35:43 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-06-17 16:35:43 -0400 |
| commit | e677a57bf1cedc55214b0e457778311b8f1ea5ac (patch) | |
| tree | 9c0b045279901f5dd4a866f46ce2d378a6d41d68 /MediaBrowser.Controller/Reflection/TypeMapper.cs | |
| parent | 95f471e8c3ab466488cc4c2fba1b15e14e00ee3c (diff) | |
switch to flat file storage
Diffstat (limited to 'MediaBrowser.Controller/Reflection/TypeMapper.cs')
| -rw-r--r-- | MediaBrowser.Controller/Reflection/TypeMapper.cs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/MediaBrowser.Controller/Reflection/TypeMapper.cs b/MediaBrowser.Controller/Reflection/TypeMapper.cs new file mode 100644 index 000000000..d968a3b42 --- /dev/null +++ b/MediaBrowser.Controller/Reflection/TypeMapper.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Concurrent; +using System.Linq; + +namespace MediaBrowser.Controller.Reflection +{ + /// <summary> + /// Class TypeMapper + /// </summary> + public class TypeMapper + { + /// <summary> + /// This holds all the types in the running assemblies so that we can de-serialize properly when we don't have strong types + /// </summary> + private readonly ConcurrentDictionary<string, Type> _typeMap = new ConcurrentDictionary<string, Type>(); + + /// <summary> + /// Gets the type. + /// </summary> + /// <param name="typeName">Name of the type.</param> + /// <returns>Type.</returns> + /// <exception cref="System.ArgumentNullException"></exception> + public Type GetType(string typeName) + { + if (string.IsNullOrEmpty(typeName)) + { + throw new ArgumentNullException(); + } + + return _typeMap.GetOrAdd(typeName, LookupType); + } + + /// <summary> + /// Lookups the type. + /// </summary> + /// <param name="typeName">Name of the type.</param> + /// <returns>Type.</returns> + private Type LookupType(string typeName) + { + return AppDomain + .CurrentDomain + .GetAssemblies() + .Select(a => a.GetType(typeName, false)) + .FirstOrDefault(t => t != null); + } + } +} |
