1

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)

4

1 回答 1

1

This is a little hackey but works great:

In your derived class:

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
于 2012-05-25T18:51:48.410 回答