本文介绍了如何启用隐式行继续在VB codeProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Visual Studio编译时,下面的VB.NET code工作:

 子的Main()
    昏暗的光源作为词典(字符串,整数)=新词典(字符串,整数)

    昏暗的结果=从我在源
                 凡String.IsNullOrEmpty(i.Key)
                 选择i.Value

结束小组
 

然而,尝试使用编译它,当 codeDOM 好像还没有使用隐式续行(我可以把它通过把下划线工作,但是这正是我想避免)。

使用的code:

 静态无效的主要(字串[] args)
        {
            字符串vbSource = @
进口系统
进口System.Collections.Generic
进口System.Linq的

模块模块1

    副主()
        昏暗的光源作为词典(字符串,整数)=新词典(字符串,整数)

        昏暗的结果=从我在源
                     凡String.IsNullOrEmpty(i.Key)
                     选择i.Value

    结束小组

前端模块
;
            VAR providerOptions =新字典<字符串,字符串>();
            providerOptions.Add(CompilerVersion,版本3.5); // .NET v3.5版本

            codeDomProvider codeProvider =新Microsoft.VisualBasic.VB codeProvider(providerOptions);

            CompilerParameters参数=新CompilerParameters();
            parameters.GenerateInMemory = TRUE;
            parameters.ReferencedAssemblies.Add(System.Core.dll的);

            CompilerResults结果= codeProvider.CompileAssemblyFromSource(参数,vbSource);
        }
 

解决方案

现在的问题是,你告诉它使用3.5版本的编译器。隐式续行未添加作为一个功能,直到4.0版本的.NET Framework,所以你需要,如果你想隐行继续合作,使用4.0(或更高版本)版本的编译器。尝试改变这样的:

  providerOptions.Add(CompilerVersion,V3.5); // .NET v3.5版本
 

要这样:

  providerOptions.Add(CompilerVersion,V4.0); // .NET 4.0版
 

The following VB.NET code works when compiled from the Visual Studio:

Sub Main()
    Dim source As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

    Dim result = From i In source
                 Where String.IsNullOrEmpty(i.Key)
                 Select i.Value

End Sub

However, when trying to compile it by using CodeDom it appears not use implicit line continuation (I can make it to work by putting underscores but that's exactly what I want to avoid).

The code used:

        static void Main(string[] args)
        {
            string vbSource = @"
Imports System
Imports System.Collections.Generic
Imports System.Linq

Module Module1

    Sub Main()
        Dim source As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

        Dim result = From i In source
                     Where String.IsNullOrEmpty(i.Key)
                     Select i.Value

    End Sub

End Module
";
            var providerOptions = new Dictionary<string, string>();
            providerOptions.Add("CompilerVersion", "v3.5"); // .NET v3.5

            CodeDomProvider codeProvider = new Microsoft.VisualBasic.VBCodeProvider(providerOptions);

            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateInMemory = true;
            parameters.ReferencedAssemblies.Add("System.Core.dll");

            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, vbSource);
        }
解决方案

The problem is that you are telling it to use the 3.5 version of the compiler. Implicit line-continuation was not added as a feature until version 4.0 of the .NET Framework, so you'll need to use the version 4.0 (or later) compiler if you want the implicit line-continuation to work. Try changing this:

providerOptions.Add("CompilerVersion", "v3.5"); // .NET v3.5

To this:

providerOptions.Add("CompilerVersion", "v4.0"); // .NET v4.0

这篇关于如何启用隐式行继续在VB codeProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:12