“的TestString”未声明。它可能无法访问由于其保护水平。 (BC30451) [英] 'TestString' is not declared. It may be inaccessible due to its protection level. (BC30451)
问题描述
我尝试了一些对飞code编译使用 VB codeProvider
类。我希望能够做的就是在我的code组装修改公共变量。
I am trying out some on the fly code compilation using the VBCodeProvider
class. What I want to be able to do is modify a public variable in my assembly in the code.
我有一个公开的TestString的String =
在我的应用程序。
I have a Public TestString As String = ""
in my application.
这是在code我使用编译:
This is the code I am using to compile:
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
这是在code在我的文本框:
This is the code in my textbox:
Imports System
Imports MyAssembly
Class Script
Sub Main()
TestString="foo"' <-- This line fails compilation
End Sub
End Class
我得到的错误是的TestString未声明。它可能无法访问由于其保护水平。 (BC30451)
推荐答案
就像正常的VB.NET,以及添加一个参考,你必须进口
相关命名空间,或完全指定。 (你编辑的问题,现在包括这个。)
Just like normal VB.NET, as well as adding a reference, you have to Imports
the relevant namespace, or specify it fully. (You've edited the question to now include this.)
插入你的code到VS2008中的一个新的控制台项目(因为这是我不得不打开),并调整了我的程序集名称后,我看到了一样的你。
After inserting your code into a new Console project in VS2008 (because that's what I had open) and adjusting for my assembly names, I saw the same as you.
我固定它通过添加公开
为默认的模块模块1
。
I fixed it by adding Public
to the default Module Module1
.
这篇关于“的TestString”未声明。它可能无法访问由于其保护水平。 (BC30451)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!