如何在 .NET 中继承然后覆盖资源或其内容? [英] How can I inherit and then override a resource or contents of it in .NET?

查看:18
本文介绍了如何在 .NET 中继承然后覆盖资源或其内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我们称之为BASE",它有一堆 .CSV 文件作为资源.该项目以不同的方法引用 My.Resources.XXXXXXX,其中 XXXXXXX 是 CSV 文件名.我正在研究一个派生自BASE"的类,我想更改一些 .CSV 文件并将BASE"项目中的文件替换为我的.我希望基础项目中的方法在调用 My.Resources.XXXXXX 时使用我的 CSV 文件,而不是BASE"资源中的文件.关于如何做到这一点的任何提示?(欢迎使用任何 .NET 语言)

I have a project, let's call it "BASE", that has a bunch of .CSV files as Resources. The project makes references to My.Resources.XXXXXXX in different methods where XXXXXXX is the CSV filename. I am working on a class that derives from "BASE" and I would like to change some of the .CSV files and replace the ones that are in the "BASE" project with mine. I would like the methods in the base project to use my CSV files when it calls My.Resources.XXXXXX instead of the files that are in "BASE"'s Resources. Any tips on how to do it? (any .NET language is welcome)

推荐答案

这是一个小技巧,但效果很好:

This is a little hackey but works great:

在您的派生类中:

Public Class MyApplication
    Inherits BaseApplication

    Private Shared Property ResourceManager As New MyResourceManager
    Shared Sub New()
        Const fieldName As String = "resourceMan"
        'set base resource manager to my specialized resource manager using reflection'
        ReflectionHelper.SetStaticFieldValue(ResourceManager.BaseResourceManagerType, fieldName, ResourceManager)
    End Sub

    ...
End Class

我的自定义资源管理器:

My Custom ResourceManager:

Public Class MyResourceManager
    Inherits ResourceManager

    Public Property BaseResourceManager As ResourceManager
    Public Property BaseResourceManagerType As Type
    Public Property MyResourceManager As ResourceManager = My.Resources.ResourceManager

    Public Sub New()
        MyBase.New()

        'get a reference to the resource manager for the base application to keep it around'
        Dim ass As Assembly = Assembly.Load("BaseApplication")
        BaseResourceManagerType = ass.GetType("BaseApplication.My.Resources.Resources")
        BaseResourceManager = ReflectionHelper.GetStaticPropertyValue(BaseResourceManagerType, "ResourceManager")
    End Sub

    Public Overrides Function GetString(ByVal name As String) As String
        Dim ret As String = MyResourceManager.GetString(name)
        If ret IsNot Nothing Then
            Return ret
        Else
            Return BaseResourceManager.GetString(name)
        End If
    End Function

    ... <override all the other members of ResourceManager in the same fashion as above>
End Class

这篇关于如何在 .NET 中继承然后覆盖资源或其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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