diff --git a/src/CsvHelper.Tests/Issues/Issue920.cs b/src/CsvHelper.Tests/Issues/Issue920.cs new file mode 100644 index 000000000..38e308835 --- /dev/null +++ b/src/CsvHelper.Tests/Issues/Issue920.cs @@ -0,0 +1,54 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace CsvHelper.Tests.Issues +{ + [TestClass] + public class Issue920 + { + [TestMethod] + public void Test1() + { + using( var stream = new MemoryStream() ) + using( var writer = new StreamWriter( stream ) ) + using( var reader = new StreamReader( stream ) ) + using( var csv = new CsvReader( reader ) ) + { + writer.WriteLine( "A,B" ); + writer.WriteLine( "1,one" ); + writer.WriteLine( "2,two" ); + writer.Flush(); + stream.Position = 0; + + csv.Configuration.GetConstructor = type => + type.GetConstructors( BindingFlags.NonPublic | BindingFlags.Instance ) + .OrderBy( c => c.GetParameters().Length ) + .First(); + csv.Configuration.IncludePrivateMembers = true; + var records = csv.GetRecords().ToList(); + + Assert.AreEqual( 2, records.Count ); + } + } + + private class Sample + { + public int A { get; private set; } + public string B { get; private set; } + + private Sample() { } + + public Sample( int a, string b ) + { + A = a; + B = b; + } + } + } +} diff --git a/src/CsvHelper.Tests/Mappings/MapConstructorTests.cs b/src/CsvHelper.Tests/Mappings/MapConstructorTests.cs index b3bc56aec..6bc501b57 100644 --- a/src/CsvHelper.Tests/Mappings/MapConstructorTests.cs +++ b/src/CsvHelper.Tests/Mappings/MapConstructorTests.cs @@ -43,7 +43,7 @@ private class Test private sealed class TestMap : ClassMap { - private TestMap() + private TestMap( string test ) { Map( m => m.Id ); Map( m => m.Name ); diff --git a/src/CsvHelper.Tests/Reflection/CreateInstanceTests.cs b/src/CsvHelper.Tests/Reflection/CreateInstanceTests.cs index b13e82f9d..43a6f147f 100644 --- a/src/CsvHelper.Tests/Reflection/CreateInstanceTests.cs +++ b/src/CsvHelper.Tests/Reflection/CreateInstanceTests.cs @@ -22,6 +22,14 @@ public void CreateInstanceTests() Assert.AreEqual( "name", test.Name ); } + [TestMethod] + public void PrivateConstructorTest() + { + var c = ReflectionHelper.CreateInstance(); + + Assert.IsNotNull( c ); + } + private class Test { public string Name @@ -29,5 +37,10 @@ public string Name get { return "name"; } } } + + private class PrivateConstructor + { + private PrivateConstructor() { } + } } } diff --git a/src/CsvHelper/CsvHelper.csproj b/src/CsvHelper/CsvHelper.csproj index 704cd87a1..b89ba049b 100644 --- a/src/CsvHelper/CsvHelper.csproj +++ b/src/CsvHelper/CsvHelper.csproj @@ -8,7 +8,7 @@ Josh Close - 7.0.0 + 7.0.1 7.0.0.0 7.0.0.0 netstandard2.0;net45 diff --git a/src/CsvHelper/ReflectionHelper.cs b/src/CsvHelper/ReflectionHelper.cs index 1d86a0225..de64a2612 100644 --- a/src/CsvHelper/ReflectionHelper.cs +++ b/src/CsvHelper/ReflectionHelper.cs @@ -187,7 +187,7 @@ private static Delegate CreateInstanceDelegate( Type type, params object[] args { var argumentTypes = args.Select( a => a.GetType() ).ToArray(); var argumentExpressions = argumentTypes.Select( ( t, i ) => Expression.Parameter( t, "var" + i ) ).ToArray(); - var constructorInfo = type.GetConstructor( argumentTypes ); + var constructorInfo = type.GetConstructor( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, argumentTypes, null ); if( constructorInfo == null ) { throw new InvalidOperationException( "No public parameterless constructor found." );