diff --git a/ICD.Common.Utils.Tests/ReflectionUtilsTest.cs b/ICD.Common.Utils.Tests/ReflectionUtilsTest.cs index 21adc45..1ba8e18 100644 --- a/ICD.Common.Utils.Tests/ReflectionUtilsTest.cs +++ b/ICD.Common.Utils.Tests/ReflectionUtilsTest.cs @@ -34,23 +34,12 @@ namespace ICD.Common.Utils.Tests } } - [TestCase("test", 10)] - [TestCase(null, 0)] - public void InstantiateTest(string param1, int param2) - { - TestClass result = ReflectionUtils.Instantiate(typeof(TestClass), param1, param2) as TestClass; - - Assert.NotNull(result); - Assert.AreEqual(param1, result.Param1); - Assert.AreEqual(param2, result.Param2); - } - [Test] public void MatchesConstructorParametersTest() { Assert.Throws(() => ReflectionUtils.MatchesConstructorParameters(null, new object[] { "test", 10 })); - ConstructorInfo constructor = typeof(TestClass).GetConstructor(new Type[] {typeof(string), typeof(int)}); + ConstructorInfo constructor = typeof(TestClass).GetConstructor(new[] {typeof(string), typeof(int)}); Assert.IsTrue(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] {"test", 10})); Assert.IsTrue(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] {null, 10})); @@ -114,6 +103,17 @@ namespace ICD.Common.Utils.Tests Assert.NotNull(output); } + [TestCase("test", 10)] + [TestCase(null, 0)] + public void CreateInstanceTest(string param1, int param2) + { + TestClass result = ReflectionUtils.CreateInstance(typeof(TestClass), param1, param2) as TestClass; + + Assert.NotNull(result); + Assert.AreEqual(param1, result.Param1); + Assert.AreEqual(param2, result.Param2); + } + [Test] public void GetCustomAttributesTest() { diff --git a/ICD.Common.Utils/ReflectionUtils.cs b/ICD.Common.Utils/ReflectionUtils.cs index 2a4733d..e42b2c5 100644 --- a/ICD.Common.Utils/ReflectionUtils.cs +++ b/ICD.Common.Utils/ReflectionUtils.cs @@ -1,60 +1,22 @@ using System; using System.Collections.Generic; using System.Linq; -using ICD.Common.Properties; using ICD.Common.Utils.Extensions; using ICD.Common.Utils.IO; #if SIMPLSHARP using Crestron.SimplSharp.CrestronIO; using Crestron.SimplSharp.Reflection; -using Activator = Crestron.SimplSharp.Reflection.Activator; #else using System.IO; using System.Reflection; using Microsoft.Extensions.DependencyModel; using System.Runtime.Loader; -using Activator = System.Activator; #endif namespace ICD.Common.Utils { public static class ReflectionUtils { - /// - /// Instantiates the given type using the constructor matching the given values. - /// - /// - /// - /// - [PublicAPI] - public static object Instantiate(Type type, params object[] values) - { - if (type == null) - throw new ArgumentNullException("type"); - - ConstructorInfo constructor = -#if SIMPLSHARP - ((CType)type) -#else - type -#endif - .GetConstructors() - .FirstOrDefault(c => MatchesConstructorParameters(c, values)); - - try - { - if (constructor != null) - return constructor.Invoke(values); - } - catch (TypeLoadException e) - { - throw new TypeLoadException(e.GetBaseException().Message); - } - - string message = string.Format("Unable to find constructor for {0}", type.Name); - throw new InvalidOperationException(message); - } - /// /// Returns true if the parameters match the constructor parameters. /// @@ -206,7 +168,7 @@ namespace ICD.Common.Utils .GetTypeInfo() #endif .IsValueType - ? Activator.CreateInstance(type) + ? CreateInstance(type) : null; } @@ -235,29 +197,47 @@ namespace ICD.Common.Utils /// /// /// - public static T CreateInstance() - where T : new() + public static T CreateInstance(params object[] parameters) { - return (T)CreateInstance(typeof(T)); + if (parameters == null) + throw new ArgumentNullException("parameters"); + + return (T)CreateInstance(typeof(T), parameters); } /// /// Creates an instance of the given type, calling the default constructor. /// /// - public static object CreateInstance(Type type) + public static object CreateInstance(Type type, params object[] parameters) { if (type == null) throw new ArgumentNullException("type"); + if (parameters == null) + throw new ArgumentNullException("parameters"); + + ConstructorInfo constructor = +#if SIMPLSHARP + type.GetCType() +#else + type +#endif + .GetConstructors() + .FirstOrDefault(c => MatchesConstructorParameters(c, parameters)); + try { - return Activator.CreateInstance(type); + if (constructor != null) + return constructor.Invoke(parameters); } - catch (TargetInvocationException e) + catch (TypeLoadException e) { - throw e.GetBaseException(); + throw new TypeLoadException(e.GetBaseException().Message); } + + string message = string.Format("Unable to find constructor for {0}", type.Name); + throw new InvalidOperationException(message); } ///