2012-08-08 5 views
3

VBCodeProvider 클래스를 사용하여 플라이 (fly) 코드 컴파일을 시도하고 있습니다. 내가 할 수 있기를 원하는 것은 코드에서 어셈블리의 공용 변수를 수정하는 것입니다.'TestString'이 (가) 선언되지 않았습니다. 보호 수준으로 인해 액세스 할 수 없습니다. (BC30451)

내 신청서에 Public TestString As String = ""이 있습니다.

Imports System 
Imports MyAssembly 

Class Script 
    Sub Main() 
     TestString="foo"' <-- This line fails compilation 
    End Sub 
End Class 

은 내가 오류가 'TestString에'

을 선언하지 않다 :

Dim codeProvider As New VBCodeProvider() 
    Dim optParams As New CompilerParameters 
    optParams.ReferencedAssemblies.Add("MyAssembly.exe") 
    optParams.ReferencedAssemblies.Add("system.windows.forms.dll") 
    optParams.CompilerOptions = "/t:library" 
    optParams.GenerateInMemory = True 
    Dim results As CompilerResults = codeProvider.CompileAssemblyFromSource(optParams, RichTextBox1.Text) 

    If results.Errors.Count > 0 Then 
     Dim sb As New StringBuilder("Compilation Failed with the following error(s)" + CR_LF + CR_LF) 
     For Each CompErr As CompilerError In results.Errors 
      sb.Append(String.Format("Line {0} - {1} ({2}){3}", CompErr.Line, CompErr.ErrorText, CompErr.ErrorNumber, CR_LF)) 
     Next 
     MessageBox.Show(sb.ToString, "Compile Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
    Else 
     Dim assy As System.Reflection.Assembly = results.CompiledAssembly 
     Dim exeinstance As Object = assy.CreateInstance("Script") 
     Dim typ As Type = exeinstance.GetType 
     Dim method As MethodInfo = typ.GetMethod("Main") 
     method.Invoke(exeinstance, Nothing) 
    End If 

이 내 텍스트 상자의 코드는 다음과 같습니다

내가 컴파일 사용하고있는 코드입니다. 보호 수준으로 인해 액세스 할 수 없습니다. (BC30451)

답변

3

참조를 추가 할뿐만 아니라 일반 VB.NET처럼 Imports 관련 네임 스페이스를 지정하거나 완전히 지정해야합니다. (이제 이것을 포함하도록 질문을 편집했습니다.)

VS2008의 새 콘솔 프로젝트에 코드를 삽입 한 후 (해당 항목이 열려 있기 때문에) 어셈블리 이름을 조정하면 동일한 결과가 나타납니다.

Public을 기본값 인 Module Module1에 추가하여 수정했습니다.

+0

도움을 주셔서 감사합니다. –

관련 문제