Provide access to the mesh element at the interface level

I want to provide access to a getter or setter for a property at the interface level so that the same runs 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 forbidden in C #. I think this is because the interface is only intended to expose what is publicly available (I have no idea!).

I need a way here to implement this using any other encoding pattern (preferably using an interface) that will help me provide specific access to the getter or setter properties in all of its implemented classes.

I googled it and tried to go through the MSDN docs for this, but no luck!

+4
3

internal , , , Example internal .

public interface IExample
{
    string Name
    {
        get;
    }
}

internal interface IExampleInternal
{
    string Name
    {
        set; get;
    }
}

internal class Example : IExample, IExampleInternal
{
    public string Name { get; set; } = string.Empty;
}

- IExampleInternal , - IExample. , , .

+1

? :

// Assembly: A
public interface IExample
{
    string Name { get; }
}

// Assembly: B
using A;

public abstract class Example : IExample
{
    public string Name { get; protected internal set; }
}

public class SpecificExample : Example
{
    public void UpdateName(string name)
    {
        // Can be set because it has protected accessor
        Name = name;
    }
}

class Program
{
    static void Main(string[] args)
    {
        IExample e = new SpecificExample()
        {
            // Can be set because it has internal accessor
            Name = "OutsideAssemblyA"
        };
    }
}

// Assembly: C
using A;

public abstract class Example : IExample
{
    public string Name { get; protected internal set; }
}

public class AnotherSpecificExample : Example
{
    public void UpdateName(string name)
    {
        // Can be set because it has protected accessor
        Name = name;
    }
}

class Program
{
    static void Main(string[] args)
    {
        IExample e = new AnotherSpecificExample()
        {
            // Can be set because it has internal accessor
            Name = "OutsideAssemblyA"
        };
    }
}

, ( -) abstract class Example , , . SpecificExample AnotherSpecificExample.

+1

it's impossible. As you were told, the interfaces are designed to define sharing. What about the following code?

public interface IExample
{
    string Name
    {
        get;
    }
}
0
source

All Articles