开发者

A design problem about interfaces and inheritance in C#.Net

开发者 https://www.devze.com 2023-04-02 00:26 出处:网络
I\'m going through a really hard time finding the answer to the question below, about inheritance and OOP. Can anyone please help?

I'm going through a really hard time finding the answer to the question below, about inheritance and OOP. Can anyone please help?

Here is the question :

Let's assume that I have a class named ServiceManager inside an assembly named KioskFramework, which implements 2 different interfaces named IServiceManager and IServiceProvider.

public interface IServiceManager
{
    string SerialNumber { get; }
    string Description { get; set; }

    int DoFoo(IServiceManager instance, int a, int b);
}

public interface IServiceProvider
{
    void DoSomethingRESTRICTED();
}

class ServiceManager : IServiceManager, IServiceProvider
{
    public void DoSomethingRESTRICTED();

    …  // some other properties and methods...

    public string SerialNumber { get { … } }

    public string Description { get { … } set { … } }

    public int DoFoo(int a, int b)
    {
       …
    }
}

I have another class named MonitoringService inside an assembly named KioskFramework.MonitoringService, which uses certain properties of ServiceManager class (The ones that are defined in the IServiceManager).

class MonitoringService
{
    …  // some other properties and methods...

    public int DoBar(IServiceManager instance, int a, int b)
    {
       // an example to show that I need access to ServiceManager's properties
       return instance.SerialNumber.Length + 
              instance.Description.Length + 
              instance.DooFoo(a, b);
    }
}

All I want to do is, that I want to be able to use that certain properties in MonitoringService, but no other class or assembly (such as ControllingService inside KioskFramework.ControllingService), could access that properties.

class ControllingService
{
    public void DoSomethingElse(IServiceProvider instance)
    {
        // this method should not have access to the IServiceManager's 
        // properties and methods, even if it has an instance of
        // IServiceProvider, or even if it has referenced the assembly 
        // containing IServiceManager interface
    }
}

Is it possible? How? Is there a design pattern for solving this?

Maybe I'm thinking in a wron开发者_开发百科g manner or way, but my goal is to restrict certain members of a class to only be seen/used in certain assemblies not all of them.

edit : After Mark Cidade's answer, I edited this post, to say that I don't want to expose other internal members and classes of "KioskFramework" assembly to "KioskFramework.MonitoringService" assembly.

Thanks in advance.


You can mark the interface as internal and apply an InternalsVisibleTo attribute to the assembly:

From KioskFramework.dll:

[assembly:InternalsVisibleTo("KioskFramework.MonitoringService")]


I am not sure what your goal is - so some general pointers:

  • you can make those interfaces internal and define which assembly is allowed to see those members assembly: InternalsVisibleTo

  • you can make the members protected and access them via Reflection

0

精彩评论

暂无评论...
验证码 换一张
取 消