从使用 VBCodeProvider 执行的脚本访问本地类型 [英] Accessing local types from script executed with VBCodeProvider

查看:19
本文介绍了从使用 VBCodeProvider 执行的脚本访问本地类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VBCodeProvider,但是我希望它能够访问诸如来自实体框架的类型.这可能吗?

I'm using VBCodeProvider, however I would like it to be able to access types such as those from Entity Framework. Is this possible?

这是我的代码...

Imports Quartz.Impl
Imports Quartz
Imports System.CodeDom.Compiler

Module Module1

    Private factory As New StdSchedulerFactory

    Sub Main()

        Dim scheduler As IScheduler = factory.GetScheduler

        Dim trigger1 As ITrigger = TriggerBuilder.Create.
            WithIdentity("expire", "Primary").WithCronSchedule("*/5 * * * * ?").Build

        Dim executeScript As IJobDetail = JobBuilder.
            Create.OfType(Of ExecuteScript).
            UsingJobData("Script", My.Computer.FileSystem.ReadAllText("ExampleScript.vb")).
            Build

        With scheduler
            .ScheduleJob(executeScript, trigger1)
            .Start()
        End With

    End Sub

End Module

Class ExecuteScript
    Implements IJob

    Public Sub Execute(context As IJobExecutionContext) Implements IJob.Execute
        Dim dataMap As JobDataMap = context.JobDetail.JobDataMap
        Using codeProvider As New VBCodeProvider
            Dim parameters As New CompilerParameters With {
                .GenerateInMemory = True,
                .TreatWarningsAsErrors = False
            }
            Dim results As CompilerResults = codeProvider.
                CompileAssemblyFromSource(parameters, dataMap("Script"))
            If results.Errors.Count > 0 Then
                Console.WriteLine("ERROR")
                For Each e As CompilerError In results.Errors
                    Console.WriteLine(e.ErrorText)
                Next
            End If
        End Using
    End Sub

End Class

文件ExampleScript.vb"包含一个脚本,其中引用了我的项目内部的类型,但是我无法使用它们,因为两者之间似乎存在分离.

The file 'ExampleScript.vb' contains a script with references to types inside of my project, however I am unable to use these as it seems there's separation between the two.

推荐答案

您需要向编译器添加所有必要的引用.假设您的实体之一名为 SomeEntity,您需要在编译前执行以下操作:

You need to add all necessary references to the compiler. Assuming one of your entity is called SomeEntity you need to do the following before compiling:

parameters.ReferencedAssemblies.Add(GetType(SomeEntity).Assembly.Location)

这篇关于从使用 VBCodeProvider 执行的脚本访问本地类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆