2011-09-02 184 views
0

在VS 2010中,.Net 4.0
我试图获取给定dll中所有字符串资源的名称
最终目标是,从一组文件中,能够获取所有资源,检查哪些是字符串,并测试这些名称以查看它们是否具有我正在寻找的关键词。
重点是:我只有dll文件,没有别的。
让说我的文件名是如何从dll文件获取字符串资源?

Dim myDllFullFileName as string = "C:\Bob.dll" 

首先,我想我应该得到反思:

Dim myAssembly As New Reflection.Assembly 
    myAssembly=Reflection.Assembly.LoadFrom(myDllFullFileName) 

那么问题开始。
如果我只是寻找我的当前项目的字符串资源我能做到这一点:

Dim myResourceSet As Resources.ResourceSet 
    myResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True) 
    For Each Dict As DictionaryEntry In myResourceSet.OfType(Of Object)() 
      If TypeOf (Dict.Value) Is String Then 
       If Dict.Key.ToString.Contains("myOwnKeyHere") Then 
        'work with dict.value' 
       End If 
      End If 
    Next 

这将正常工作为我的当前项目。结果正是我所期待的。
我的问题是,我想用dll文件来做。
我想我应该能够从myAssembly创建一个ResourSet。为此,我需要一个ResourceManager。
从MyAssembly程序建立一个新的ResourceManager,我也许应该用这个方法:

Public Sub New(baseName As String, assembly As System.Reflection.Assembly) 

而这正是我丢:我不设法摆脱,我现在用的是dll文件的基本名称。


我也尝试与

myAssembly.GetManifestResourceNames() 

但它返回一个空数组

如果任何人有一个想法,我将不胜感激。

谢谢。

+0

在这里检查:http://stackoverflow.com/questions/27757/how-can-i-discover-the-path-of-an-embedded-resource你是100%确定你正在正确加载DLL并且它确实包含嵌入式资源? –

+0

我确信我已经嵌入资源(在resx文件中)。资源也设置为PUBLIC而不是Friend。现在我不是100%确定我正确加载dll,但我认为是这样,因为我可以执行此操作:例如,myAssembly.GetTypes。 – PatTheFrog

回答

1

好的,这里是一个答案: 我想获取.dll或.resx或.exe文件中所有资源字符串的名称和值。

Dim myDllFullFileName As String = "C:\Bob.dll" 
    'it will also work for .resx and .exe files' 
    Dim myAssembly As Reflection.Assembly 
    myAssembly = Reflection.Assembly.LoadFile(myDllFullFileName) 
    Dim myBaseNames(-1) As String 
    myBaseNames = myAssembly.GetManifestResourceNames 
    Dim myResourcesBaseName As String = "" 
    For i As Integer = 0 To myBaseNames.Length - 1 
     If myBaseNames(i).Contains(".Resources.") Then 
      myResourcesBaseName = myBaseNames(i) 
      'in the case of a .exe file, you will get more than one file' 
      'Example with an .exe file from a Windows Application having one form:' 
      'WindowsApplication1.Form1.resources' 
      'WindowsApplication1.Resources.resources' 
      'you want the one containing ".Resources."' 
     End If 
    Next 
    myResourcesBaseName = myResourcesBaseName.Substring(0, myResourcesBaseName.LastIndexOf(".")) 
    'then you cut the last part and keep "WindowsApplication1.Resources"' 

    Dim myManager As System.Resources.ResourceManager 
    myManager = New System.Resources.ResourceManager(myResourcesBaseName, myAssembly) 

    Dim myResourceSet As Resources.ResourceSet = Nothing 
    myResourceSet = myManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True) 'the second true (tryparent, is vital)' 
    Dim myResourceNames(-1) As String 
    Dim myResourceValues(-1) As String 
    If myResourceSet IsNot Nothing Then 
     myResourceNames = GetStringRessources(myResourceSet) 'see below for this routine' 
     ReDim myResourceValues(myResourceNames.Length - 1) 
     For i As Integer = 0 To myResourceNames.Length - 1 
      myResourceValues(i) = myResourceSet.GetString(myResourceNames(i)) 
     Next 
    End If 

这里是常规GetRessources:

Public Function GetStringRessources(valResourceSet As ResourceSet) As String() 
    Dim myList(-1) As String 
    For Each Dict As DictionaryEntry In valResourceSet.OfType(Of Object)() 
     If TypeOf (Dict.Value) Is String Then 
      ReDim Preserve myList(myList.Length) 
      myList(myList.Length - 1) = Dict.Key.ToString 
     End If 
    Next 
    Return myList 
End Function 

这使我能够存储的名称和所有资源字符串的值。

您需要注意以下几点:
- 上面的代码还会返回TEXT文件的内容(我们也应该将它们视为字符串)。所以如果你不想要文件的文本,并且你期望字符串具有一定的长度,那么可以进行简单的测试。否则,我不知道如何解决这个问题。

相关问题