Last active
November 19, 2019 13:53
-
-
Save wcoder/44341a4ecafaaae861c4 to your computer and use it in GitHub Desktop.
Static IoC container
This file contains hidden or 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 static class SimpleIoc | |
{ | |
private static readonly ConcurrentDictionary<Type, object> _dependencyMap = new ConcurrentDictionary<Type, object>(); | |
public static void Register<TName>(object instance) | |
{ | |
_dependencyMap.TryAdd(typeof(TName), instance); | |
} | |
public static T Get<T>() | |
{ | |
return (T) Get(typeof(T)); | |
} | |
private static object Get(Type type) | |
{ | |
_dependencyMap.TryGetValue(type, out object inst); | |
return inst; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
New version: