Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何限制可以从DLL访问的内容?_C#_Dll_Scope - Fatal编程技术网

C# 如何限制可以从DLL访问的内容?

C# 如何限制可以从DLL访问的内容?,c#,dll,scope,C#,Dll,Scope,我有一个DLL,其代码如下所示: public class MyInterface { private Class1 class1; public void DoSomething() { class1.DoSomething(); } } public class Class1 { public void DoSomething() { //Do something... } } 其他开发人员将使用我

我有一个DLL,其代码如下所示:

public class MyInterface
{
    private Class1 class1;

    public void DoSomething()
    {
        class1.DoSomething();
    }
}

public class Class1
{
    public void DoSomething()
    {
        //Do something...
    }
}
其他开发人员将使用我编写的DLL。他们只能使用
MyInterface
,不应该知道
Class1
。直接使用
Class1
可能会产生意外行为并引入bug


有没有办法只向使用DLL的开发人员显示DLL内部内容的子集?

这就是访问修饰符的用途。将您的类设置为内部

internal class Class1
{
}

它将仅在部件内部可见。查看MSDN以了解更多关于访问修饰符的信息:

您可以将
Class1
设置为内部,并警告其他人,如果他们直接通过反射访问它,那么所有赌注都将被取消,错误将由他们来修复。我想我将不得不将我的单元测试放在同一个项目中。对吗?或者使用反射…实际上,通过使用
InternalsVisibleToAttribute
,可以从其他程序集中看到
internal
。它完全是为自动化测试而设计的。请在此阅读: