Question

Consider the following:

class ControllerFactoryBase<P> : where P : PledgeReadOnly
{
    public void Foo()
    {
         PledgeRepositoryReadOnly<P> repos = PledgeRepository();
         new AdminController(repos); // compilation error here
    }
}

public interface PledgeRepositoryReadOnly<out P> where P : PledgeReadOnly
{
    IEnumerable<P> GetPledgesToBeneficiary();

}

public class AdminController
{
    public AdminController(PledgeRepositoryReadOnly<PledgeReadOnly> pledgeProvider)
    { ... }
}

I'm getting a compilation error on the instantiation of AdminController with message:

cannot convert from 'PledgeRepositoryReadOnly<P>' to 'PledgeRepositoryReadOnly<PledgeReadOnly>'

and I'm not sure why. I believe this substitution is safe. Can you help me understand why it is not?

edit more digestible presentation: http://csharppad.com/gist/9283391

No correct solution

OTHER TIPS

with some step,finaly,i get what's wrong
first

public interface PledgeReadOnly
{

}

this is a interface,and

where P : PledgeReadOnly

it mean p must be a PledgeReadOnly but

public struct x : PledgeReadOnly
{
}

public class y : PledgeReadOnly
{
}

both class and struct and implement from a interface not sure P is a class or a struct you can change your PledgeReadOnly from interface to class or you can change all

where P : PledgeReadOnly

to

where P : class,PledgeReadOnly

it make sure P is a class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top