3

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?

epitka
  • 17,275
  • 20
  • 88
  • 141

3 Answers3

2

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
epitka
  • 17,275
  • 20
  • 88
  • 141
  • Thanks for posting the final answer. Would you mind saying how you learned this? – John Saunders Sep 04 '09 at 20:14
  • I used reflector to decompile my assembly and look what compiler did with my code. You can find it at http://www.red-gate.com/products/reflector/ – epitka Sep 04 '09 at 20:17
1

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.

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
  • Do you know of any articles or resources that would help one wanting to attempt such a thing? – CoderDennis Jan 30 '10 at 06:45
  • http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx covers CodeDOM. It doesn't specifically cover XML literals, though, since that's a VB-specific feature, so you'd need to use a literal expression - http://msdn.microsoft.com/en-us/library/system.codedom.codesnippetexpression.aspx – Pavel Minaev Jan 30 '10 at 08:55
0

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

Community
  • 1
  • 1
Larsenal
  • 49,878
  • 43
  • 152
  • 220