refactor: reducing code duplication, CreateInstance takes parameters

This commit is contained in:
Chris Cameron
2018-04-05 13:42:08 -04:00
parent 9d034872cb
commit 6a7f5a74af
2 changed files with 38 additions and 58 deletions

View File

@@ -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] [Test]
public void MatchesConstructorParametersTest() public void MatchesConstructorParametersTest()
{ {
Assert.Throws<ArgumentNullException>(() => ReflectionUtils.MatchesConstructorParameters(null, new object[] { "test", 10 })); Assert.Throws<ArgumentNullException>(() => 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[] {"test", 10}));
Assert.IsTrue(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] {null, 10})); Assert.IsTrue(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] {null, 10}));
@@ -114,6 +103,17 @@ namespace ICD.Common.Utils.Tests
Assert.NotNull(output); 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] [Test]
public void GetCustomAttributesTest() public void GetCustomAttributesTest()
{ {

View File

@@ -1,60 +1,22 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ICD.Common.Properties;
using ICD.Common.Utils.Extensions; using ICD.Common.Utils.Extensions;
using ICD.Common.Utils.IO; using ICD.Common.Utils.IO;
#if SIMPLSHARP #if SIMPLSHARP
using Crestron.SimplSharp.CrestronIO; using Crestron.SimplSharp.CrestronIO;
using Crestron.SimplSharp.Reflection; using Crestron.SimplSharp.Reflection;
using Activator = Crestron.SimplSharp.Reflection.Activator;
#else #else
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using Microsoft.Extensions.DependencyModel; using Microsoft.Extensions.DependencyModel;
using System.Runtime.Loader; using System.Runtime.Loader;
using Activator = System.Activator;
#endif #endif
namespace ICD.Common.Utils namespace ICD.Common.Utils
{ {
public static class ReflectionUtils public static class ReflectionUtils
{ {
/// <summary>
/// Instantiates the given type using the constructor matching the given values.
/// </summary>
/// <param name="type"></param>
/// <param name="values"></param>
/// <returns></returns>
[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);
}
/// <summary> /// <summary>
/// Returns true if the parameters match the constructor parameters. /// Returns true if the parameters match the constructor parameters.
/// </summary> /// </summary>
@@ -206,7 +168,7 @@ namespace ICD.Common.Utils
.GetTypeInfo() .GetTypeInfo()
#endif #endif
.IsValueType .IsValueType
? Activator.CreateInstance(type) ? CreateInstance(type)
: null; : null;
} }
@@ -235,29 +197,47 @@ namespace ICD.Common.Utils
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <returns></returns> /// <returns></returns>
public static T CreateInstance<T>() public static T CreateInstance<T>(params object[] parameters)
where T : new()
{ {
return (T)CreateInstance(typeof(T)); if (parameters == null)
throw new ArgumentNullException("parameters");
return (T)CreateInstance(typeof(T), parameters);
} }
/// <summary> /// <summary>
/// Creates an instance of the given type, calling the default constructor. /// Creates an instance of the given type, calling the default constructor.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static object CreateInstance(Type type) public static object CreateInstance(Type type, params object[] parameters)
{ {
if (type == null) if (type == null)
throw new ArgumentNullException("type"); 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 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);
} }
/// <summary> /// <summary>