Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/FluentNHibernate.Testing/Utils/ExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using NUnit.Framework;
using FluentNHibernate.Utils;

namespace FluentNHibernate.Testing.Utils;

[TestFixture]
public class ExtensionsTests
{
class PublicConstructor
{
public PublicConstructor() { }
}

class PrivateConstructor
{
private PrivateConstructor() { }
}

class ConstructorWithArguments
{
private ConstructorWithArguments(int number) { }
}

[Test]
public void CanInitialiseClass()
{
var type = typeof(PublicConstructor);
var result = type.InstantiateUsingParameterlessConstructor();

Assert.That(result, Is.InstanceOf<PublicConstructor>());
}

[Test]
public void CanInitialiseClassWithPrivateConstructor()
{
var type = typeof(PrivateConstructor);
var result = type.InstantiateUsingParameterlessConstructor();

Assert.That(result, Is.InstanceOf<PrivateConstructor>());
}

[Test]
public void ClassWithoutParameterlessConstructorThrowsException()
{
var type = typeof(ConstructorWithArguments);
Assert.Throws<MissingConstructorException>(() => type.InstantiateUsingParameterlessConstructor());
}
}
4 changes: 4 additions & 0 deletions src/FluentNHibernate/MissingConstructorException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public MissingConstructorException(Type type)
: base("'" + type.AssemblyQualifiedName + "' is missing a parameterless constructor.")
{ }

public MissingConstructorException(Type type, Exception innerException)
: base("'" + type.AssemblyQualifiedName + "' is missing a parameterless constructor.", innerException)
{ }

[Obsolete("This API supports obsolete formatter-based serialization and will be removed in a future version")]
protected MissingConstructorException(SerializationInfo info, StreamingContext context) : base(info, context)
{ }
Expand Down
16 changes: 9 additions & 7 deletions src/FluentNHibernate/Utils/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ public static T InstantiateUsingParameterlessConstructor<T>(this Type type)

public static object InstantiateUsingParameterlessConstructor(this Type type)
{
var constructor = ReflectHelper.GetDefaultConstructor(type);

if (constructor is null)
throw new MissingConstructorException(type);

return constructor.Invoke(null);
try
{
return Activator.CreateInstance(type, true);
}
catch (MissingMethodException ex)
{
throw new MissingConstructorException(type, ex);
}
}

public static bool HasInterface(this Type type, Type interfaceType)
Expand All @@ -81,7 +83,7 @@ public static T DeepClone<T>(this T obj)
#if NETFRAMEWORK
var formatter = new BinaryFormatter();
#else
var formatter = new BinaryFormatter(new NetStandardSerialization.SurrogateSelector(), new StreamingContext());
var formatter = new BinaryFormatter(new NetStandardSerialization.SurrogateSelector(), new StreamingContext());
#endif

formatter.Serialize(stream, obj);
Expand Down