asiftasleem / nbuilder

Automatically exported from code.google.com/p/nbuilder
0 stars 0 forks source link

AreConstructedUsing with Child Class Constructor Generates Parent Type #40

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Class model...
public class Parent {}
public class Child : Parent {}

2. Statement in question
var children = Builder<Parent>.CreateListOfSize(3)
                                .WhereAll()
                                  .AreConstructedUsing(() => new Child())
                                .Build();

What is the expected output? What do you see instead?
I expect a list of Parent type containing Child type instances.  Instead 
there is a list of Parent type containing Parent type instances.

What version of the product are you using? On what operating system?
2.1.9.0

Please provide any additional information below.

Original issue reported on code.google.com by russell....@googlemail.com on 16 Feb 2010 at 12:02

GoogleCodeExporter commented 8 years ago
Hi Russell,

From what I can ascertain from the code, the AreConstructedUsing method is 
intended to be used to construct a class with an alternate constructor for the 
class being constructed using Builder<T>. As such, it is not intended to be 
used in the way you have attempted.

I believe the reason you are currently unable to do what you are wanting is not 
due to NBuilder but rather the version of C# you are using. It turns out C# 4.0 
has better support for covariance and contravariance: 
http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravarian
ce-faq.aspx

During my investigation of this issue, I tested my thought that this was a 
covariance/contravariance type isue by testing NBuilder in a .NET 3.5 and a 
.NET 4 project.

This is the code I used:

List<Parent> children = Builder<Child>
                .CreateListOfSize(3)                                 
                .Build()
                .ToList<Parent>();

This code only compiles in .NET 4. The result is a list of Parent type 
containing Child type instances.

In light of the evidence, I will presently be closing this defect. If you feel 
that this is being closed in error, please let me know and we can discuss this 
further.

All the best upgrading to C# 4.

Regards,
Joshua Roth

Original comment by joshuajr...@gmail.com on 10 Sep 2010 at 12:14