diff options
| author | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-25 22:43:04 -0500 |
|---|---|---|
| committer | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-25 22:43:04 -0500 |
| commit | 2d06095447b972c8c7239277428e2c67c8b7ca86 (patch) | |
| tree | 14278bd4c0732ee962b73ff4845e5022e157a0a3 /MediaBrowser.Server.Implementations/Reflection/TypeMapper.cs | |
| parent | 364fbb9e0c7586afa296ddd7d739df086f4c3533 (diff) | |
plugin security fixes and other abstractions
Diffstat (limited to 'MediaBrowser.Server.Implementations/Reflection/TypeMapper.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Reflection/TypeMapper.cs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/MediaBrowser.Server.Implementations/Reflection/TypeMapper.cs b/MediaBrowser.Server.Implementations/Reflection/TypeMapper.cs new file mode 100644 index 000000000..5f411a002 --- /dev/null +++ b/MediaBrowser.Server.Implementations/Reflection/TypeMapper.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Concurrent; +using System.Linq; + +namespace MediaBrowser.Server.Implementations.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); + } + } +} |
