Krishanu Dey
Krishanu Dey

Reputation: 6406

Calling Protected constructors of a class in c#

Possible Duplicate:
How to call protected constructor in c#?

I searched for a solution of my problem.
Actually I don't know if it is possible or not. Please help me.

namespace namespace1
{
    namespace namespace1a
    {
        public class classa
        {
            protected classa(string i) //protected constructor
            {
                //Do something
            }
            public classa() //public constructor
            {
                //Do something
            }

        }
    }

    namespace namespace1b
    {
        public class classb
        {
            classa i = new classa(); // calls public constructor of classa

            classa j = new classa("hi"); //Invalid. How to call the protected constructor of classa
        }
    }
}  

I want to call the protected constructor of "classa" from "classb", How to do that? Please help me.

Upvotes: 2

Views: 4840

Answers (5)

Austin Salonen
Austin Salonen

Reputation: 50225

A safe way to do this would be to add a proxy class that inherits from classb with a public constructor taking a string. The protected contract is well-defined.

class bproxy : classb
{
     public bproxy(string x) : base(x) {...}
}

An alternative is to use reflection. Quite honestly I don't recommend doing this because when an author sets a constructor as protected, it's intended to only be used by itself and derived classes. You are also tightly coupled to a "contract" that is not explicitly defined.

That being said, this should work for you though it will break as soon as the author changes the non-guaranteed part of the contract (USE AT YOUR OWN RISK):

var constructor = typeof(classa).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
var b = constructor.First().Invoke(new object[] {"hi"});

Upvotes: 1

tia
tia

Reputation: 9698

If

  • classa and classb are in the same assembly
  • You are authoring both class
  • You are trying to protect classa's constructor.

then you can declare the constructor as protected internal instead.

Upvotes: 3

mbcrute
mbcrute

Reputation: 1127

You can use reflection to get a reference to the constructor you want to invoke from the type's metadata:

var ctor = typeof(classa).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(string) }, null);

Once you have a reference to the constructor you can invoke it, passing in the arguments you want to give the constructor as an array of objects:

var instance = (classa)ctor.Invoke(new object[] { "Chicken butt" });

That being said, the constructor is probably protected for a reason and using reflection to circumvent encapsulation of a type's members is generally not a good idea.

Upvotes: 2

YavgenyP
YavgenyP

Reputation: 2123

Your question is a duplicate of this one.
except for deriving from classa, reflection is a usefull option (by quering the constructors of the tpye, and then running the suitable one, as explained in one of the answers in the link provided)

Upvotes: 0

MAV
MAV

Reputation: 7457

It is not possible unless classb derives from classa. Protected makes the constructor visible only to the class itself and every class that derives from classa.

Take a look here: Protected C#

Upvotes: 0

Related Questions