.net 未声明“TestString”。由于其保护级别,它可能无法访问。(BC30451)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11860742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:33:43  来源:igfitidea点击:

'TestString' is not declared. It may be inaccessible due to its protection level. (BC30451)

.netvb.netscriptingvbcodeprovider

提问by Matt Wilko

I am trying out some on the fly code compilation using the VBCodeProviderclass. What I want to be able to do is modify a public variable in my assembly in the code.

我正在尝试使用VBCodeProvider该类进行一些动态代码编译。我希望能够做的是在代码中修改我的程序集中的公共变量。

I have a Public TestString As String = ""in my application.

Public TestString As String = ""我的应用程序中有一个。

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

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

The error I get is 'TestString' is not declared. It may be inaccessible due to its protection level. (BC30451)

我得到的错误是未声明“TestString”。由于其保护级别,它可能无法访问。(BC30451)

采纳答案by Mark Hurd

Just like normal VB.NET, as well as adding a reference, you have to Importsthe relevant namespace, or specify it fully. (You've edited the question to now include this.)

就像普通的 VB.NET 一样,除了添加引用之外,您还必须Imports指定相关的命名空间,或者完全指定它。(您已将问题编辑为现在包含此内容。)

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.

在 VS2008 中将您的代码插入到一个新的控制台项目中(因为那是我打开的)并调整了我的程序集名称后,我看到了与您相同的情况。

I fixed it by adding Publicto the default Module Module1.

我通过添加Public到默认的Module Module1.