CodeDom / Code Access Security - Restrict access to instantiate classes
Posted on 2006-07-16
I'm not all that familar with the Code Access Security model within .NET, and I'm wondering if someone can help me out here (or at least tell me I'm going about it all wrong).
I'm just working my way through how the CodeDom works, and how it can be used to add scripting to an application.
I have a piece of code that is using CodeDom to automatically generate a basic assembly that has a reference to my business objects. What I want is to be able to restrict the generated code from being able to instantite new instances of my business classes, while still being able to call the methods etc on them.
It is not an option to make the constructors Friend of Private because the classes are current used in multipe assemblies. Is there a way to specify which assemblies are allowed to create instances of a class?
Here's a sample of the output code. I want the first function to work , but the second one to fail.
' I want this to work
Public Function EvalCode(obj as MyObject)
' I want this to fail, because I do not want the
' constructor to be able to be called
Public Function EvalCode() As Object
Dim obj As New MyObject
Is what I'm looking for possible, or am I going in the wrong direction?