Samer_Azar
Samer_Azar

Reputation: 433

C# interface how to hide code

I developed a class library containing an interface and an inherited class:

public interface testInterface
{
    string GetAge(int year);
}

and a class that extends this interface

public class testClass : testInterface
{
    public string GetAge(int year)
    {
        int age = DateTime.Today.Year - year;
        return string.Format("Your age is {0}", age);
    }
}

in another solution I created a console application and I referenced the class library dll. in the driver i created an object:

testClassLibrary1.testInterface obj = new testClassLibrary1.testClass();

and I called the method GetAge(); When I debug, I can still see the code of the method getAge() How can I hide this code? I mean isn't the interface used to hide the method abstract? please advise.

Upvotes: 1

Views: 1615

Answers (4)

Ian
Ian

Reputation: 879

You can never hide your code from debug mode. The only way to do this is it have that code execute outside of the scope of the Visual Studio application you're running.

FYI: In terms of hiding code visibility at a programming interface level, there are various options available, for example the 'new' keyword to hide derived members, and on a more basic level various protection levels for your classes, properties and methods, e.g. internal, protected, etc.

Upvotes: 1

Pablo
Pablo

Reputation: 75

As @nvoigt said, the only thing you could do is to not include any symbols (*.pdb files) with your dll/package.

However bear in mind that you are writing managed code and anyone using a managed code decompiler tool like Reflector or DotPeek can easily inspect the code.

A possible solution is to obfuscate your code in order to make it harder to see what you are doing but you can never truly prevent people from decompiling it. Where there's a will...

Upvotes: 1

Luca Salzani
Luca Salzani

Reputation: 132

I don't think this is possible in an approach which is not security through obscurity with interfaces.

Upvotes: 1

nvoigt
nvoigt

Reputation: 77364

You could simply not deliver the debug information (*.pdb) to the party that's using the dll.

However, the interface is meant to hide the implementation from the compiler. You can change it and the program will still compile because it's not dependent on it. The instant you have a single implementation active, it's very useful to debug into it.

Interfaces are a way to abstract things, not to hide or secure things.

Upvotes: 3

Related Questions