简体   繁体   English

如何防止将方法添加到类中?

[英]How can I prevent methods from being added to a class?

I'm trying to find out if there's a way to stop functions/methods from being added ( EDIT: by other developers ) to a class for the case where the object is a Model or DTO which should not contain methods (to prevent 'abuse' of the Models/DTOs by others, who may try and add 'helper' methods etc). 我试图找出是否有办法阻止函数/方法被添加( 编辑:由其他开发人员 )到一个类,对象是一个模型或DTO,不应该包含方法(以防止滥用其他人的模特/ DTO,他们可能会尝试添加'辅助'方法等)。

Is there any way to achieve this? 有没有办法实现这个目标?

Use reflection and write a unit test that fails if a model-class has methods. 如果模型类具有方法,则使用反射并编写单元测试失败。

Mark all you model classes with a custom attribute. 使用自定义属性标记所有模型类。 Then make a unit test that uses reflection to load a given assembly, iterate all classes in that assembly and check that classes marked with the model attribute does not have methods. 然后进行单元测试,使用反射来加载给定的程序集,迭代该程序集中的所有类,并检查标记有model属性的类是否没有方法。 This should be fairly straight forward using reflection. 这应该是使用反射相当直接的。

I believe you are trying to solve a procedural issue with code where you should be using communication. 我相信您正在尝试使用代码解决程序问题,您应该使用通信。

Your colleagues (i assume) are operating on the code files with 'full trust' privileges. 您的同事(我假设)正在使用“完全信任”权限操作代码文件。 If they break that privilege you should open a dialogue. 如果他们打破了这种特权,你应该打开一个对话。 Use the change as an opportunity to educate them on the intended design. 将更改作为一个机会,让他们了解预期的设计。 Perhaps they are correct and you will be educated! 也许他们是正确的,你将受到教育!

I suggest simply making the intended design obvious in the class name and with a comment stating the intended nature. 我建议简单地在类名中明确设计预期的设计,并注明说明预期的性质。 Perhaps quote the design document(s) that informed the class. 也许引用通知班级的设计文件。

You cannot hinder anyone with full write-access to your code-base to do so. 您不能阻止对您的代码库具有完全写入权限的任何人这样做。 The only two things you may do to avoid it are create some CodeAnalysis-rule for FXCop as mentioned by Christian.K in the comments or by writing your DTO-class so that it is undoubtly a DTO that should not have any methods by using a unambigious name for the class and if this is not enough provide some code-comments that notifies the coder to do not so. 你可以做的唯一两件事就是为了避免它而创建一些CodeCnalysis规则,如Christian.K在评论中提到的那样,或者通过编写你的DTO类,这样它无疑是一个不应该有任何方法的DTO。该类的名称不明确,如果这还不够,请提供一些代码注释,通知编码人员不这样做。

However you may need some kind of method if using collections eg where you will need some kind of comparision if two instances of your DTO are equal, so you have to provide at least an Equals - and GetHashCode -method. 但是,如果使用集合,您可能需要某种方法,例如,如果您的DTO的两个实例相等,则需要进行某种比较,因此您必须提供至少一个Equals - 和GetHashCode -method。

You don't need to use a struct to prevent additions to a class. 您不需要使用结构来阻止添加类。 You can use the sealed keyword 您可以使用sealed关键字

public sealed class MyDTOObject { ... }

Now, you can not inherent a class and also prevent inheritance (which is essentially what you're asking). 现在,你不能固有一个类,也不能阻止继承(这基本上就是你所要求的)。 The very fact of inheriting MyDTOObject is creating a new class which is based off of not equal to, or restricted, or defined in any way by the implementation of MyDTOObject . 继承MyDTOObject事实是创建一个新类,它基于 MyDTOObject的实现不等于,限制或以任何方式定义。

You can use an abstract class, to force derived classes to implement certain methods, but not the other way around. 您可以使用abstract类来强制派生类实现某些方法,但不能反过来。

If you want to prevent others from deriving from your class and implementing helper methods, you must use the sealed keyword, or mark the class internal. 如果要防止其他人从您的类派生并实现帮助器方法,则必须使用sealed关键字,或者将类标记为internal。

You may prevent the class being extended or inherited by marking it final that way nobody would be able to extend your class and hence not being able to add any behavior. 您可以通过将其标记为最终来阻止该类被扩展或继承,因为没有人能够扩展您的类,因此无法添加任何行为。 But stop and ask yourself whether you want to do that or not, because then you'd be signing an invisible contract that everything ever required by the class is written in the class and this class needs no further addition. 但是停下来问问自己是否要这样做,因为那时你将签署一个无形的合同,这个类所需的一切都写在类中,而且这个类不需要进一步添加。

To be clear, I was talking in Java context. 为了清楚起见,我在Java语境中谈论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止它在列表视图中添加两次 - How to prevent it from being added 2 times in listview 如何防止提供扩展方法的公共类出现在Intellisense中? - How can I prevent a public class that provides extension methods from appearing in Intellisense? 生成成员时,如何防止在 C# 的 VS 代码中添加“private”访问修饰符? - How can I prevent the `private` access modifier from being added in VS Code for C# when generating members? 如何防止 class 通过依赖注入注入其他类? - How can I prevent a class from being injected into other classes via dependency injection? 如何防止WinForms Chart在添加数据时调整大小? - How can I prevent WinForms Chart from resizing as data is added? 防止调用DI类中的任何方法 - Prevent any methods from DI class from being called 如何阻止第三方调用某些方法? - How can I prevent a third party from calling certain methods? c# 如何防止将一个特定字符添加到文本文件中 - c# how do i prevent one specific character from being added to a text file 如何防止数据管理序列化 - How can I prevent a datamember from being serialized 如何防止 ObservableCollection 在 WPF 中无故刷新? - How can I prevent the ObservableCollection from being refreshed for no reason in WPF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM