获取DLL的继承类的文件名 [英] Get DLL's file name of inherited class

查看:369
本文介绍了获取DLL的继承类的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类编译成一个DLL(让我们称之为 BaseClass.dll ),作为其他类继承的基础。这些其他类也被编译成DLL(我们称之为 InheritedClass.dll ),可以用作我的主应用程序的插件,可以在每个插件中调用相同的方法作为抽象类强制他们实现。



然而,有一些功能将是所有的插件都是常见的,所以我宁愿如果我可以在基地类。这减少了冗余,也消除了在实现该功能时编写插件时出错的可能性。



一个这样的例子是一段需要名称的代码的编译DLL文件。一个可以工作的方法可能如下所示:

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

问题是 GetExecutingAssembly()返回当前正在运行的代码的程序集,而不是编译它的程序集。所以,如果我把这个函数放在BaseClass中,它返回(BaseClass.cs),而不管它被编译成的DLL的名字。这意味着我必须把它放在InheritedClass中,不能把它放在BaseClass中。如上所述,我想避免这种情况,因为我不想用这个常见代码来为插件的开发人员劳动,我不想依靠这些第三方开发人员来正确的。 p>

这是否可以?有没有办法可以通过位于基类中的代码获取从这个基类继承的DLL的名称?

解决方案

p> GetType 总是返回一个对象的实际类型,所以你可以用它来找出你实际上是什么类型,然后从那里追逐到大会:

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

希望以后再提供一个更好的名称。 GetName 是非常模糊的。


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?

解决方案

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.

这篇关于获取DLL的继承类的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆