2011-09-02 12 views
0

VS 2010에서 .Net 4.0
주어진 DLL에서 모든 문자열 리소스의 이름을 얻으려고합니다.
궁극적 인 목표는 파일 집합에서 모든 리소스를 가져올 수 있는지 확인하는 것입니다. 어떤 것들은 Strings이며, 내가 찾고있는 핵심 단어가 있는지 확인하기 위해 이름을 테스트합니다.
요점은 다음과 같습니다. 나는 단지 dll 파일 만 가지고 있습니다.
하자 내가 반사를 얻어야한다 생각, 내 파일 이름이dll 파일에서 String 리소스를 가져 오는 방법은 무엇입니까?

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으로 설정됩니다.이제는 DLL을 올바르게로드하고 있다고 100 % 확신하지는 못했지만이를 실행할 수 있으므로 myAssembly.GetTypes를 예로들 수 있습니다. – PatTheFrog

답변

1

OK, 여기에 하나의 답변이 있습니다. .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 파일의 내용도 반환합니다 (문자열로 간주해야합니다). 따라서 파일의 텍스트를 원하지 않고 특정 길이의 문자열을 예상하면 간단한 테스트가 가능합니다. 그렇지 않으면 문제를 해결하는 방법을 모르겠습니다.

관련 문제