2010-12-09 29 views
0

我正在嘗試爲我的VB .Net應用程序創建應用程序腳本環境。我在應用程序中有一個表單類型,名稱爲項目。我想要做的就是動態編譯(即Eval)VB.Net代碼,然後在項目表單上。這個動態代碼總是有一個函數。 主要參數是參考項目的形式。下面是評估和演示代碼:如何將對錶單的引用傳遞給動態編譯代碼

Public Function Eval(ByVal vbCode As String, ByRef MyAssembly As System.Reflection.Assembly, ByRef ProjectForm As Project) As ArrayList 
    Dim Provider As VBCodeProvider = CodeDomProvider.CreateProvider("VisualBasic") 
    Dim CompParams As CompilerParameters = New CompilerParameters 
    Dim CResult As CompilerResults 
    Dim EvalAssy As System.Reflection.Assembly 
    Dim InstanceObject As Object 
    Dim InstanceType As Type 
    Dim InstanceMI As MethodInfo 
    Dim CompError As CompilerError 
    Dim NumberOfErrors As Integer = 0 
    Dim ReturnValues As New ArrayList 
    Dim SkipAssembly As Boolean = False 
    Dim DebugTextBox As TextBox = ProjectForm.CompilationErrors 
    Dim DebugFilename As String = ProjectForm.DebugFile 
    Dim StatusBar As ToolStripStatusLabel = ProjectForm.MyMainForm.MessageToolStripStatusLabel 

    'Result = MsgBox("HMMM: " & CodeDomProvider.IsDefinedLanguage("VisualBasic").ToString(), MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "HMMM") 
    If Not DebugTextBox Is Nothing Then 
     DebugTextBox.Text &= "Running Eval On Assembly """ & MyAssembly.FullName & """." & vbCrLf 
    End If 
    CompParams.ReferencedAssemblies.Add("system.dll") 
    CompParams.ReferencedAssemblies.Add("system.xml.dll") 
    CompParams.ReferencedAssemblies.Add("system.data.dll") 
    CompParams.ReferencedAssemblies.Add("System.Windows.Forms.dll") 
    ' Pass Myself In 
    CompParams.ReferencedAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly.Location) 
    CompParams.CompilerOptions = "/t:library" 
    CompParams.GenerateInMemory = True 

    CResult = Provider.CompileAssemblyFromSource(CompParams, vbCode) 
    For Each CompError In CResult.Errors 
     If Not CompError.IsWarning Then 
      ' "Real Error" 
      NumberOfErrors += 1 
     End If 
     ReturnValues.Add("Error " & CompError.ErrorNumber & " On Line " & CompError.Line & ": " & CompError.ErrorText) 
    Next 
    If NumberOfErrors = 0 Then 
     Try 
      EvalAssy = CResult.CompiledAssembly 
      InstanceObject = EvalAssy.CreateInstance("Translation") 
      InstanceType = InstanceObject.GetType() 
      InstanceMI = InstanceType.GetMethod("Main") 
      InstanceMI.Invoke(InstanceObject, New Object() {MyAssembly, ProjectForm}) 
      'InstanceMI.Invoke(InstanceObject, BindingFlags.Default, Nothing, New Object() {MyAssembly, ProjectForm}, System.Globalization.CultureInfo.CurrentCulture) 
     Catch Ex As Exception 
      If Ex.InnerException Is Nothing Then 
       WriteDebugMsg(DebugTextBox, "Error Evaling Compiled Code. The Error Is """ & Ex.Message) 
      Else 
       WriteDebugMsg(DebugTextBox, "Error Evaling Compiled Code. The Error Is:" & vbCrLf & Ex.Message & vbCrLf & "Inner Exception Is:" & vbCrLf & Ex.InnerException.ToString) 
      End If 
     End Try 
    End If 
    Return ReturnValues 
End Function 

在動態(EVAL)代碼的主要功能是:

Imports System 
Imports System.Xml 
Imports System.Data 
Imports Microsoft.VisualBasic 
Imports System.Reflection 
Imports System.Diagnostics 
Imports System.IO 
Imports System.Windows.Forms 
Imports System.Collections 
Imports System.Text 
Imports UniversalTranslator 

Public Sub Main(ByRef MyAssembly As System.Reflection.Assembly, ByRef MyProjectForm As Project) 
    ' Create functions needed to parse assembly here and in subroutines called from here 
    Me.ProjectForm = MyProjectForm 
    DebugFile = ProjectForm.DebugFile ' Set Global Debug Filename Variable 
    DebugTB = ProjectForm.CompilationErrors 
    ' Initialize Log Files And Debug Textboxes 
    If Dir(DebugFile,FileAttribute.Normal) > "" Then 
     Kill(DebugFile) 
    End If 
    If Not DebugTB is Nothing Then 
     DebugTB.Text = "" 
    end if 
    RecurseAssemblyForms(MyAssembly,AddressOf OutputControlInfo) 
End Sub 

一切編譯罰款。編譯後的代碼將MyProjectForm識別爲從調用程序集中鍵入Project。然而,當代碼與參考主叫總裝項目的形式出現以下錯誤執行:

運行評估和演示在大會「LaserTrak V4軟件,版本= 4.507.3992.19399,文化=中立,公鑰=空」 。 錯誤評估編譯代碼。錯誤是: 調用的目標引發了異常。 內部例外是: System.MissingMemberException:找不到類型爲'Project'的公共成員'CompilationErrors'。 at Microsoft.VisualBasic.CompilerServices.Symbols.Container.GetMembers(String & MemberName,Boolean ReportErrors) at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance,Type Type,String MemberName,Object [] Arguments,String [] ArgumentNames,類型[] TypeArguments,布爾[]回拷) 在Translation.Main(大會& MyAssembly程序,項目& MyProjectForm)

我的猜測是,主要過程沒有 「信任」 的接入呼叫組件活動項目形式。我如何給它訪問? 感謝您的幫助。

+0

你沒有得到解決方案**沒有編譯錯誤**? – Kiquenet 2017-02-23 08:35:17

回答

0

我猜CompilationErrors不是公衆會員嗎?它是朋友嗎?還是私人?或受保護?

如果它是朋友成員,您可以使用InternalsVisibleToAttribute,否則,您將無法以這種方式訪問​​它。

如果您正在使用C#4.0中,你可以假設使用DynamicObject允許私有成員訪問...但這是另一個故事...

0

CompilationErrors是項目窗體上的文本框中。我忘了將它的Modifiers屬性設置爲Public,因爲我是個白癡;-)。

+0

沒有回答,更好的評論?恕我直言。 – Kiquenet 2017-02-23 08:36:12

相關問題