epitka
epitka

Reputation: 17627

VB.NET XML Literals Parsing

If I have a text stored in db, file, resource does not matter:

<code><%= Integer.MaxValue %></code>

Is there a way to do this:

Dim el As XElement = XElement.Parse({variable containing text above})

and to have it evaluate the expression Integer.MaxValue?

Upvotes: 3

Views: 648

Answers (3)

Pavel Minaev
Pavel Minaev

Reputation: 101555

Not easily. The closest you could get it to use CodeDOM to generate some code - you'll need to produce a wrapper module and a Function with Return, then put the expression from the database into that Return - compile it (and check for compile errors), run it, and see the result. All in all, this would be very slow.

Upvotes: 1

epitka
epitka

Reputation: 17627

Short answer is no. Compiler knows how to parse this and replaces your source code with something that looks like one would do in C# code.

Test code:

Dim source As String = "a,s,d,f"
Dim ar As String() = source.Split(","c)
Dim el As XElement = <code>
                         <%= From s In ar Select s + "22" %>
                     </code>

Reflected code:

Dim VB$CG$t_i4$S0 As Integer
Dim ar As String() = "a,s,d,f".Split(New Char() { ","c })
Dim VB$t_ref$S0 As New XElement(XName.Get("code", ""))
VB$t_ref$S0.Add(ar.Select(Of String, String)(New Func(Of String, String)(AddressOf Test._Lambda$__1)))
Dim el As XElement = VB$t_ref$S0

Upvotes: 2

Larsenal
Larsenal

Reputation: 51146

I think they key is understanding how embedded expressions work. I've asked this as a separate question.

Upvotes: 0

Related Questions