返回后代类的程序集版本的属性

发布于 2024-09-27 13:06:17 字数 646 浏览 0 评论 0原文

在一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港小镇 2024-10-04 13:06:17

您需要获取使用 GetType().Assembly 声明动态类型的程序集:

public string Version
{
    get
    {
        return GetType().Assembly.GetName().Version.ToString(); 
    }
}

You need to get the assembly where the dynamic type is declared with GetType().Assembly:

public string Version
{
    get
    {
        return GetType().Assembly.GetName().Version.ToString(); 
    }
}
三岁铭 2024-10-04 13:06:17

请改用 this.GetType().Assembly

Use this.GetType().Assembly instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文