简体   繁体   English

应用范围限制对象,方法等

[英]Application-wide Restriction of objects, methods etc

I have a very theoretical question: Is there a way to ban the use of some methods, objects etc. inside of my application/project map in C#, .Net and/or Visual Studio? 我有一个非常理论化的问题:有没有办法禁止在C#,。Net和/或Visual Studio中的应用程序/项目映射中使用某些方法,对象等?

To be more specific: I'm developing a DMS System where it should never be possible to delete files from an archive. 更具体一点:我正在开发一个DMS系统,永远不可能从存档中删除文件。 The archived files are just files inside a Windows folder structure. 归档文件只是Windows文件夹结构中的文件。

So, whenever someone tries to perform a System.IO.File.Delete() this should be forbidden. 因此,每当有人试图执行System.IO.File.Delete()时,都应该禁止这样做。 Instead I would force to use a custom-made FileDelete()-method which always ensures that the file to delete is not a file from inside an archive. 相反,我会强制使用自定义的FileDelete()方法,该方法始终确保要删除的文件不是存档内的文件。 (This doesn't have to happen automatically. It's ok when there is an error/exception that informs the developer of a banned method-call.) (这不必自动发生。当有错误/异常通知开发人员禁止方法调用时,它是可以的。)

Another way to implement this could be to observe all calls of System.IO.File.Delete() at runtime, catch them and execute my own FileDelete()-method. 另一种实现方法可能是在运行时观察System.IO.File.Delete()的所有调用,捕获它们并执行我自己的FileDelete()方法。

Of course these are a really theoretical questions but I would just know if there could be a way to implement this. 当然这些是一个真正的理论问题,但我只知道是否有办法实现这一点。

PS: I'm using C# with Visual Studio 2005. So it doesn't matter if I can realize this through my programming language or by Visual Studio (or by any other way I forgot). PS:我在Visual Studio 2005中使用C#。因此,如果我能通过我的编程语言或Visual Studio(或者我忘记的任何其他方式)实现这一点并不重要。

控制对归档文件的删除权限不是更简单吗?

you can define methods and adorn them with declarative security attributes http://msdn.microsoft.com/en-us/library/dswfd229.aspx 您可以定义方法并使用声明性安全属性来装饰它们http://msdn.microsoft.com/en-us/library/dswfd229.aspx

HTH HTH

Not for existing library functions. 不适用于现有的库函数。

For your own code, you could apply code-access-security on methods, but code running as "full trust" will breeze past this; 对于您自己的代码,您可以在方法上应用代码访问安全性,但是作为“完全信任”运行的代码将轻松过去; so to check for abuse via reflection you would probably have to check the caller manually ( Assembly.GetCallingAssembly ) - which is painful and still not 100% robust... 所以要通过反射检查滥用,你可能需要手动检查调用者( Assembly.GetCallingAssembly ) - 这很痛苦,但仍然不是100%健壮...

There is specific file/IO permissions, but again full trust will ignore it. 有特定的文件/ IO权限,但再次完全信任将忽略它。

I think "no" is a safer answer. 我认为“不”是一个更安全的答案。

One way you could go about doing this is to create a special user account and only grant that account the permissions necessary to remove the files. 您可以采用的一种方法是创建一个特殊的用户帐户,并仅授予该帐户删除文件所需的权限。 Just keep in mind that the user is in control of his computer (if he has administrative privileges ;) and while you can put some obstacles in his way there really is nothing you can do about it (and that's the way it should be). 请记住,用户可以控制他的计算机(如果他有管理权限;)虽然你可以在他的方式上设置一些障碍,但你真的无能为力(这就是应该的方式)。

What about writing your own FxCop rule for that case? 如何为该案例编写自己的FxCop规则?

With such a rule it will be impossible to compile if you treat warnings as errors. 有了这样的规则,如果将警告视为错误,则无法进行编译。

The closest I can come to a solution is to write you own System.IO.File class and keeping that in exe project. 我能找到最接近解决方案的是编写自己的System.IO.File类并将其保存在exe项目中。 That way you'll get a ambiguity compile error that can be resolved with giving you own implementation in an alias (using File=System.IO.File, Version=[version], cultuer=[correct culture], publicKey=[public key]). 这样你就会得到一个歧义编译错误,可以通过在别名中给你自己的实现来解决(使用File = System.IO.File,Version = [version],cultuer = [correct culture],publicKey = [public key] ])。 If you're unsure about what to write make a break point and write something like ?typeof(System.IO.File).AssemblyQualifiedName in the immediate window. 如果你不确定要写什么作出一个断点,并在即时窗口中写出类似?typeof(System.IO.File).AssemblyQualifiedName的内容。

It's not bullet proof but at least it will enforce the developer to be concious about the decision and you could even (tho I personally wouldn't do it) change the default class template to include the using directive for every class 这不是防弹,但至少它会强制开发人员对决策有所了解,你甚至可以(我本人不会这样做)更改默认的类模板以包含每个类的using指令

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM