File tree Expand file tree Collapse file tree 3 files changed +61
-7
lines changed
FluentNHibernate.Testing/Utils Expand file tree Collapse file tree 3 files changed +61
-7
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ public MissingConstructorException(Type type)
10
10
: base ( "'" + type . AssemblyQualifiedName + "' is missing a parameterless constructor." )
11
11
{ }
12
12
13
+ public MissingConstructorException ( Type type , Exception innerException )
14
+ : base ( "'" + type . AssemblyQualifiedName + "' is missing a parameterless constructor." , innerException )
15
+ { }
16
+
13
17
[ Obsolete ( "This API supports obsolete formatter-based serialization and will be removed in a future version" ) ]
14
18
protected MissingConstructorException ( SerializationInfo info , StreamingContext context ) : base ( info , context )
15
19
{ }
Original file line number Diff line number Diff line change @@ -59,12 +59,14 @@ public static T InstantiateUsingParameterlessConstructor<T>(this Type type)
59
59
60
60
public static object InstantiateUsingParameterlessConstructor ( this Type type )
61
61
{
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
+ }
68
70
}
69
71
70
72
public static bool HasInterface ( this Type type , Type interfaceType )
@@ -81,7 +83,7 @@ public static T DeepClone<T>(this T obj)
81
83
#if NETFRAMEWORK
82
84
var formatter = new BinaryFormatter ( ) ;
83
85
#else
84
- var formatter = new BinaryFormatter ( new NetStandardSerialization . SurrogateSelector ( ) , new StreamingContext ( ) ) ;
86
+ var formatter = new BinaryFormatter ( new NetStandardSerialization . SurrogateSelector ( ) , new StreamingContext ( ) ) ;
85
87
#endif
86
88
87
89
formatter . Serialize ( stream , obj ) ;
You can’t perform that action at this time.
0 commit comments