dimanche 24 avril 2016

Enforce getter setter access at the interface level

I want to enforce access on getter or setter for a property at the interface level so that the same be followed in the class that implements it. I want to do something like below:

public interface IExample
{
    string Name
    {
        get;
        internal set;
    }
}

public class Example : IExample
{
    private string _name = String.Empty;
    string Name
    {
        get
        {
            return _name;
        }
        internal set
        {
            _name = value;
        }
    }
}

But unfortunately from what I know this is not allowed in C#. I think that is because interface are meant to only expose what that is with a public access(I haven't the slightest idea!).

What I need here is a way to implement this using any other coding pattern (preferably using interface) which will help me to enforce specific access on getter or setter of a property in all of its implemented classes.

I googled this and tried to go through MSDN docs for this but had no luck!

Aucun commentaire:

Enregistrer un commentaire