ActivatorExt.cs 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. namespace Quadarax.Foundation.Core.Reflection.Extensions
  3. {
  4. public static class ActivatorExt
  5. {
  6. #region *** Private Fields ***
  7. #endregion
  8. #region *** Public Properties ***
  9. #endregion
  10. #region *** Constructors ***
  11. #endregion
  12. #region *** Public operations & overrides ***
  13. public static object CreateInstance(string sClassOrInterfaceQualifiedName, bool bLoadAssemblyIfNeeded, params object[] aConstructorParams)
  14. {
  15. var oType = Type.GetType(sClassOrInterfaceQualifiedName, false) ?? TypeUtils.GetRemoteType(sClassOrInterfaceQualifiedName);
  16. if (oType.IsInterface)
  17. {
  18. throw new ArgumentException($"Input class name '{sClassOrInterfaceQualifiedName}' cannot be an interface.",nameof(sClassOrInterfaceQualifiedName));
  19. }
  20. return Activator.CreateInstance(oType, aConstructorParams);
  21. }
  22. public static T CreateInstance<T>(string sClassOrInterfaceQualifiedName, bool bLoadAssemblyIfNeeded, params object[] aConstructorParams)
  23. {
  24. return (T) CreateInstance(sClassOrInterfaceQualifiedName, bLoadAssemblyIfNeeded, aConstructorParams);
  25. }
  26. #endregion
  27. #region *** Private operations & overrides ***
  28. #endregion
  29. }
  30. }