返回后代类的程序集版本的属性
在一个 C# 项目中,有一个如下所示的基类:
public abstract class AbstractVersionedBase
{
public string Version { get {
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}}
}
该项目的版本为 0.0.1.1。
在另一个 C# 项目中,我有一个从 AbstractVersionedBase 继承的类,如下所示:
public class Versioned : AbstractVersionedBase
{
}
该项目的版本为 0.0.1.2。
当我调用具体类上的属性时,它返回定义抽象类的程序集的版本。
var versioned = new Versioned();
Console.WriteLine("Version" + versioned.Version);
//writes 0.0.1.1, but I want 0.0.1.2
我希望后代类返回其程序集的版本,但不必重写代码来执行此操作后代阶级。你有什么想法吗?
In one C# project have a base class like this:
public abstract class AbstractVersionedBase
{
public string Version { get {
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}}
}
This project has a version of 0.0.1.1.
In another C# project I have a class that inherits from AbstractVersionedBase like this:
public class Versioned : AbstractVersionedBase
{
}
This project has a version of 0.0.1.2.
When I call the property on the concrete class, it returns the version of the assembly that the abstract class is defined in.
var versioned = new Versioned();
Console.WriteLine("Version" + versioned.Version);
//writes 0.0.1.1, but I want 0.0.1.2
I would like the descendant class to return the version of its assembly, but not have to rewrite the code to do that in the descendant class. Do you have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要获取使用
GetType().Assembly
声明动态类型的程序集:You need to get the assembly where the dynamic type is declared with
GetType().Assembly
:请改用
this.GetType().Assembly
。Use
this.GetType().Assembly
instead.