【发布时间】:2015-03-15 21:39:35
【问题描述】:
我有一个抽象类,它需要能够加载程序集中包含的文件,这些文件构成了该类的对象。我可以为我班级的每个孩子使用FileAssembly = Assembly.GetCallingAssembly(),但由于它是一个人们可以扩展的库,我希望在不需要他们这样做的情况下发生这种情况。
我现在的设置大致如下:
public abstract class ExternalResource : Resource, IDisposable
{
public string File { get; private set; }
protected Assembly FileAssembly { get; set; }
protected ExternalResource(string id, string file)
: base(id)
{
File = file;
}
//and s'more code
}
public class Sound : ExternalResource
{
public Sound (string id, string file)
: base(id, file)
{
//this is the line I want to be able to get rid of
FileAssembly = Assembly.GetCallingAssembly();
}
}
使用我的库的人可以在不设置 FileAssembly 的情况下创建自己的 ExternalResource,这是不可取的。如果他们要从已经从 ExternalResource 继承的类继承,那将变得非常混乱。如何获得实例化对象的代码程序集?在不对现有系统进行太多更改的情况下解决它的任何其他方法也将不胜感激!
【问题讨论】:
-
你需要调用代码的程序集,还是定义派生类型的程序集?
-
调用代码,这个想法很好,无论文件提供到哪里,都必须是解析的程序集。
标签: c# .net inheritance assembly-resolution