0

I have an abstract class compiled into a DLL (let's call it BaseClass.dll) which serves as a base for other classes to inherit from. These other classes are also compiled into DLL's (let's call one of them InheritedClass.dll) which can be used as plugins for my main application which can call the same methods in each plugin as the abstract class forces them to implement.

However, there's some functionality which will be common to all the plugins and so I would prefer if I could implement those in the base class. This cuts down on redundancy and also eliminates the possibility of whoever writes the plugin to make mistakes when implementing that functionality.

One such example is a piece of code that requires the name of the compiled DLL file. A method that would've worked could look like this:

public string GetName()
{
    return System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
}

The problem is that GetExecutingAssembly() returns the assembly for the code that is currently running, not the assembly into which it has been compiled. So, if I put the above function in BaseClass, it returns (BaseClass.cs), regardless of the name of the DLL that it is compiled into. This means that I have to put it in InheritedClass and can't put it in BaseClass. As explained above, I'd like to avoid that as I don't want to labour the developer of the plugins with this bit of common code and I don't want to rely on these third party developers to get it right.

Is this even possible? Is there a way I can get the name of the DLL which inherited from this base class through code that sits in the base class?

Dewald Swanepoel
  • 1,651
  • 4
  • 15
  • 38

1 Answers1

2

GetType always returns the actual type of an object, so you can use that to find out what type you actually are, then chase from there to the assembly:

public string GetName()
{
    return System.IO.Path.GetFileName(GetType().Assembly.CodeBase);
}

Hopefully you'll come up with a better name for this method later. GetName is horribly vague.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • That is fantastic, thanks. And no need to worry about the terrible method name, it doesn't even exist. This code is part of a bigger section which sits in a method called ReadPluginConfiguration :-) – Dewald Swanepoel Jul 29 '15 at 10:32
  • Damien_The_Unbeliever, I know it's 4 months later but I've hit a further hiccup with this particular issue. Would you mind having a look at this question and see if you might be able to answer? http://stackoverflow.com/questions/33867719/get-physical-file-name-of-inherited-class – Dewald Swanepoel Nov 23 '15 at 09:39