ConstructorCache.cs 928 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace OpenSEMI.Core.Container
  7. {
  8. internal class ConstructorCache
  9. {
  10. private static ConcurrentDictionary<Type, ConstructorInfo> _constructorCache = new ConcurrentDictionary<Type, ConstructorInfo>();
  11. public static ConstructorInfo GetConstructor(Type type)
  12. {
  13. if (!_constructorCache.TryGetValue(type, out ConstructorInfo value))
  14. {
  15. ConstructorInfo constructorInfo2 = _constructorCache[type] = DiscoverConstructor(type.GetTypeInfo());
  16. value = constructorInfo2;
  17. return value;
  18. }
  19. return value;
  20. }
  21. private static ConstructorInfo DiscoverConstructor(TypeInfo typeInfo)
  22. {
  23. IEnumerable<ConstructorInfo> declaredConstructors = typeInfo.DeclaredConstructors;
  24. if (declaredConstructors.Any())
  25. {
  26. return declaredConstructors.First();
  27. }
  28. return null;
  29. }
  30. }
  31. }