-1

I had Telerik Data Access contexts instantiated in many projects in my solution. Right now in separate project MyContextProject I created a class MyContext which instatiates and returns proper Telerik context and replaced all instantiations of Telerik's context with MyContext.

I want to prevent anybody from explicitly instantiating Telerik's Data Access context outside of MyContextProject however I don't want to restrict usage of any other types from Telerik Data Access.

Is this is achievable? Is there a way maybe to create an annotation as I don't know of any mechanism in C# that would support this.


I tried to make it a generic question, Telerik can be replaced with any other library.

Yoda
  • 17,363
  • 67
  • 204
  • 344
  • I'm not familiar with Telerik components and libraries, but is the Telerik's Data Access context a generic class exists in their assembly or it is a class like EF context that you create and inherit from their base class? If yes, you can set that class to be internal and make it visible only to a specific set of assemblies. – Saeid Jul 16 '18 at 13:38
  • 1
    You have a class that references a class of a third party assembly. And you want to prevent developers from accessing the class of the third party directly? Technically I don't think that is possible, as a developer could always reference the library directly. Or are you only trying to restrict access over an API? – Greg Jul 16 '18 at 14:54

1 Answers1

0

I want to prevent anybody from explicitly instantiating Telerik's Data Access context outside of MyContextProject

No, this is not possible - at least not that I am aware of. Since Telerik Data Access contexts are public within that library / assembly, any application that references the assembly can instantiate such a context class.

The situation would be a bit different if you could modify the source code of the library (i.e. Telerik Data Access). In that case you could set the visibility of the library context class to internal and add the InternalsVisibleTo attribute to it, so that your assembly can use the context class, but others cannot. E.g.:

[assembly: InternalsVisibleTo("MyAssembly")]

That way any application that uses the modified library cannot instantiate the library's context class, but would have to use the MyContext class from your library. However, that still does not prevent anybody from using the original, unmodified library if they want to use the library's context class. So in practice you cannot enforce this and the answer is still: No.

Striezel
  • 3,693
  • 7
  • 23
  • 37