Jimmy Hoffa
Jimmy Hoffa

Reputation: 5967

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

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();
  }
}

Upvotes: 4

Views: 1771

Answers (4)

Patrick Karcher
Patrick Karcher

Reputation: 23613

This would not cause any security concerns on .Net's part, nor should it. In public methods you have to be able to use internal (/private/protected) members.

The solution to the potential security issues this might cause is: The developer making the public member has to know what they're doing.

Upvotes: 1

dlev
dlev

Reputation: 48596

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.

Upvotes: 3

Tigran
Tigran

Reputation: 62276

Don't see any problem with code provided, honestly.

May you can pass SomeClass like a parameter, but it's completely design decision.

Upvotes: 0

Jethro
Jethro

Reputation: 5916

"The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly" Taken from MSDN.

I don't see why not, this should work, and you are calling a public class thats calling an internal method within it's own assembly.

Upvotes: 0

Related Questions