Skip to content

Commit 9675ab5

Browse files
authored
Replace Constructor Info Invoke with CreateInstance (#646)
+semver:patch
1 parent 0eafff9 commit 9675ab5

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using NUnit.Framework;
2+
using FluentNHibernate.Utils;
3+
4+
namespace FluentNHibernate.Testing.Utils;
5+
6+
[TestFixture]
7+
public class ExtensionsTests
8+
{
9+
class PublicConstructor
10+
{
11+
public PublicConstructor() { }
12+
}
13+
14+
class PrivateConstructor
15+
{
16+
private PrivateConstructor() { }
17+
}
18+
19+
class ConstructorWithArguments
20+
{
21+
private ConstructorWithArguments(int number) { }
22+
}
23+
24+
[Test]
25+
public void CanInitialiseClass()
26+
{
27+
var type = typeof(PublicConstructor);
28+
var result = type.InstantiateUsingParameterlessConstructor();
29+
30+
Assert.That(result, Is.InstanceOf<PublicConstructor>());
31+
}
32+
33+
[Test]
34+
public void CanInitialiseClassWithPrivateConstructor()
35+
{
36+
var type = typeof(PrivateConstructor);
37+
var result = type.InstantiateUsingParameterlessConstructor();
38+
39+
Assert.That(result, Is.InstanceOf<PrivateConstructor>());
40+
}
41+
42+
[Test]
43+
public void ClassWithoutParameterlessConstructorThrowsException()
44+
{
45+
var type = typeof(ConstructorWithArguments);
46+
Assert.Throws<MissingConstructorException>(() => type.InstantiateUsingParameterlessConstructor());
47+
}
48+
}

src/FluentNHibernate/MissingConstructorException.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public MissingConstructorException(Type type)
1010
: base("'" + type.AssemblyQualifiedName + "' is missing a parameterless constructor.")
1111
{ }
1212

13+
public MissingConstructorException(Type type, Exception innerException)
14+
: base("'" + type.AssemblyQualifiedName + "' is missing a parameterless constructor.", innerException)
15+
{ }
16+
1317
[Obsolete("This API supports obsolete formatter-based serialization and will be removed in a future version")]
1418
protected MissingConstructorException(SerializationInfo info, StreamingContext context) : base(info, context)
1519
{ }

src/FluentNHibernate/Utils/Extensions.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ public static T InstantiateUsingParameterlessConstructor<T>(this Type type)
5959

6060
public static object InstantiateUsingParameterlessConstructor(this Type type)
6161
{
62-
var constructor = ReflectHelper.GetDefaultConstructor(type);
63-
64-
if (constructor is null)
65-
throw new MissingConstructorException(type);
66-
67-
return constructor.Invoke(null);
62+
try
63+
{
64+
return Activator.CreateInstance(type, true);
65+
}
66+
catch (MissingMethodException ex)
67+
{
68+
throw new MissingConstructorException(type, ex);
69+
}
6870
}
6971

7072
public static bool HasInterface(this Type type, Type interfaceType)
@@ -81,7 +83,7 @@ public static T DeepClone<T>(this T obj)
8183
#if NETFRAMEWORK
8284
var formatter = new BinaryFormatter();
8385
#else
84-
var formatter = new BinaryFormatter(new NetStandardSerialization.SurrogateSelector(), new StreamingContext());
86+
var formatter = new BinaryFormatter(new NetStandardSerialization.SurrogateSelector(), new StreamingContext());
8587
#endif
8688

8789
formatter.Serialize(stream, obj);

0 commit comments

Comments
 (0)