如何使基础设施的代码只有在命名空间中可见? [英] How do you make infrastructure code only visible in namespace?

查看:190
本文介绍了如何使基础设施的代码只有在命名空间中可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有调度场算法,我想明确地融入我们的框架的实现。目前,我有这一切包装成一个简单的公共接口的类。

I have an implementation of the Shunting-Yard algorithm that I am trying to integrate clearly into our framework. Currently I have it all packed into a class with a simple public interface.

namespace MathematicalParser {
  public class ExpressionParser {
    public ExpressionParser(string expression, List<string> variables);
    public double GetNumericValue(Dictionary<string,double> variableValues);
  }
}



这个类里面有很多辅助类的,帮手枚举,静态变量等,以不同的名称将函数映射。所有这些都是私人,这样不用担心图书馆的用户。

Inside this class there are a lot of helper classes, helper enums, static variables etc to map different names to functions. All these are private so that is no concern to the user of the library.

在提高代码我想单独的代码是逻辑上的可维护性努力无关的到自己的类,这些类但没有ExpressionParser以外的任何意义,所以我想限制自己的知名度命名空间MathematicalParser(其中只包含ExpressionParser)。

In an effort to improve maintainability of the code I'm trying to separate code that is logically unrelated into their own classes, these classes do however not have any meaning outside of the ExpressionParser so I'd like to limit their visibility to the namespace MathematicalParser (which only contains the ExpressionParser).

你如何最好的C#实现这个目标,内部关键字仅适用于组件和私人不能在命名空间中使用。

How do you best accomplish this in c#, the internal keyword only works on assemblies and private can't be used in namespaces.

推荐答案

我不会做这个(相同的看法乔),但这里的距离Lopina的回答得出另一种解决方案:嵌套类+部分类

I wouldn't do this (same opinion as Joe), but here's another solution derived from Lopina's answer: nested classes + partial classes.

PublicClass.cs:

PublicClass.cs:

namespace MyNamespace
{
    public partial class PublicClass
    {
        public int ReturnSomeStuff()
        {
            MyHelperClass1 tmp = new MyHelperClass1();
            MyHelperClass2 tmp2 = new MyHelperClass2();

            return tmp.GetValue1() + tmp2.GetValue2();
        }
    }
}



PrivateClass1.cs:

PrivateClass1.cs:

namespace MyNamespace
{
    public partial class PublicClass
    {
        private class MyHelperClass1
        {
            public int GetValue1()
            {
                return 5;
            }
        }
    }
}



PrivateClass2的.cs:

PrivateClass2.cs:

namespace MyNamespace
{
    public partial class PublicClass
    {
        private class MyHelperClass2
        {
            public int GetValue2()
            {
                return 10;
            }
        }
    }
}



计划的.cs:

Program.cs:

public class Program
{
    private static void Main(string[] args)
    {
        PublicClass tmp = new PublicClass();

        MyHelperClass2 zz;  // Can't access MyHelperClass2 here cause it's private

        Console.WriteLine(tmp.ReturnSomeStuff());
        Console.ReadLine();
    }
}



正如你所看到的,您不同的辅助类是物理在不同的文件分隔(也许它会帮助你保持你的代码)。而且你无法直接访问他们,他们是私有的 PublicClass

这篇关于如何使基础设施的代码只有在命名空间中可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆