Skip to content

Commit

Permalink
Allow private constructors to be used to instantiate new class instan…
Browse files Browse the repository at this point in the history
…ces.
  • Loading branch information
Josh Close committed Feb 23, 2018
1 parent 05292ef commit 5971782
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
54 changes: 54 additions & 0 deletions src/CsvHelper.Tests/Issues/Issue920.cs
Original file line number Diff line number Diff line change
@@ -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<Sample>().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;
}
}
}
}
2 changes: 1 addition & 1 deletion src/CsvHelper.Tests/Mappings/MapConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private class Test

private sealed class TestMap : ClassMap<Test>
{
private TestMap()
private TestMap( string test )
{
Map( m => m.Id );
Map( m => m.Name );
Expand Down
13 changes: 13 additions & 0 deletions src/CsvHelper.Tests/Reflection/CreateInstanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ public void CreateInstanceTests()
Assert.AreEqual( "name", test.Name );
}

[TestMethod]
public void PrivateConstructorTest()
{
var c = ReflectionHelper.CreateInstance<PrivateConstructor>();

Assert.IsNotNull( c );
}

private class Test
{
public string Name
{
get { return "name"; }
}
}

private class PrivateConstructor
{
private PrivateConstructor() { }
}
}
}
2 changes: 1 addition & 1 deletion src/CsvHelper/CsvHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Authors>Josh Close</Authors>

<!-- Build -->
<Version>7.0.0</Version>
<Version>7.0.1</Version>
<FileVersion>7.0.0.0</FileVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
Expand Down
2 changes: 1 addition & 1 deletion src/CsvHelper/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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." );
Expand Down

0 comments on commit 5971782

Please sign in to comment.