Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can extension access an internal method in same assembly and be used by externals?

Tags:

c#

.net

In the following code snippet assuming both these classes are in the same assembly, can an external assembly call .DoSomethingToSomeClass(); on an OtherClass, or would this bark at me about security concerns?

public class SomeClass
{
  internal void DoSomething()
  {
    //hah!
  }
}

public static OtherClassExtension
{
  public static DoSomethingToSomeClass(this OtherClass target)
  {
    new SomeClass().DoSomething();
  }
}
like image 861
Jimmy Hoffa Avatar asked Sep 06 '25 10:09

Jimmy Hoffa


1 Answers

That definitely works without security concerns. Imagine the world we'd live in if it didn't: Any public method you write would have to call only other public methods.

Access modifiers are there to give you a say in what portions of your class (and what classes you write) are directly accessible by calling code. The fact there is some chain where they could be executed isn't relevant.

like image 185
dlev Avatar answered Sep 07 '25 23:09

dlev