在C#中,如何限制谁可以调用在编译时间的方法 [英] In C#, how to restrict who can call a method at compile time

查看:208
本文介绍了在C#中,如何限制谁可以调用在编译时间的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,这可能限制谁可以在编译时调用的方法?



我已经研究过的指令,但因为我没有工作不能分配值的符号。

 的#define WHO VisualStudioUser.Current //不起作用

我也看了成代码访问安全(CAS),但这是运行时执行,而不是编译时间。



的要求是要限制在给出的方法中存在一个预编译组件编译时对于特定开发访问的方法。



这里的详情...
我建立了一个开发团队的框架或一系列或组件。因为我们的软件许可证的限制,我只能允许少数开发人员编写代码来拨打电话到一些受限制的方法。开发商将无法获得该框架的源代码,但他们将有机会获得编译框架组件。


解决方案

的要求是要限制在给出的方法中存在一个预编译组件编译时对于特定开发访问的方法。




的一种方法是,以纪念该方法私人内部,它不会被调用的任何的组装之外。 更新:也看看的的 InternalsVisibleTo 属性,它被用来定义哪些组件可以看到您的程序集内部。



另一种方法是划分你不希望别人叫成的独立的组件,你要从代码分发代码。也许你只是分享大多是与您的用户界面的组件,这二者对编译;和你有,他们不应该直接引用实现一个单独的程序。你的内部团队将有机会获得实施装配。这是依赖管理的只是一种常见的形式,在依赖倒置原则


In C#, is it possible to restrict who can call a method at compile time?

I've looked into directives, but that didn't work since I can't assign values to symbols.

#define WHO VisualStudioUser.Current // does not work

I also looked into Code Access Security (CAS) but that's runtime enforcement, not compile time.

The requirement is to restrict access to a method at compile time for specific developers given the method exists in a pre-compiled assembly.

here's more details... I'm building a framework or a series or assemblies for a team of developers. Because of our software license restrictions, I can only allow a few developers to write code to make a call to some restricted methods. The developers will not have access to the source code of the framework but they'll have access to the compiled framework assemblies.

解决方案

The requirement is to restrict access to a method at compile time for specific developers given the method exists in a pre-compiled assembly.

One way is to mark the method private or internal, it won't be callable by anyone outside the assembly. UPDATE: Also take a look at the InternalsVisibleTo attribute, which is used to define which assemblies can "see" internals of your assembly.

Another way is to divide the code you want to distribute from the code you don't want people to call into separate assemblies. Maybe you just share an assembly mostly of interfaces with your users, that they them compile against; and you have a separate assembly with implementations that they shouldn't reference directly. Your internal team would have access to the implementation assembly. This is just a common form of dependency management, the dependency inversion principle.

这篇关于在C#中,如何限制谁可以调用在编译时间的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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