How to call protected constructor?
public class Foo{
public Foo(a lot of arguments){}
protected Foo(){}
}
var foo=???
This obviously fails
You can only call that from a subclass, basically. Your FooMock
class will already be calling the protected constructor, because it's equivalent to:
public class FooMock : Foo
{
public FooMock() : base() // Call the protected base constructor
{
}
}
However, your assertion will fail because the type of object referred to be foo
is FooMock
, not Foo
.
An assertion of the form foo is Foo
will pass though.
You can't construct an instance of just Foo
by calling the protected constructor directly. The point of it being protected instead of public is to ensure that it's only called by subclasses (or within the text of Foo
itself).
It's possible that you could call it with reflection within a full trust context, but I'd urge you not to do so.