0

I have

new List<string> {"MyTypeA", "MyTypeB"};

How can i generate types at runtime?

public class MyTypeA {decimal value {get;set;}};
public class MyTypeB; {decimal value {get;set;}};

Let me explain why I need this. Here, I wrote about the serialization of an expression-tree. In the end I used for this task Serialize.Linq. Serialization and deserialization works fine but I was faced with another problem. My algorithm builds an expression with the following body:

{(args[0] - args[1] + args[0])}

This body is obtained for the expression (MyTypeA - MyTypeB + MyTypeA), where MyTypeA and MyTypeB it is parameters. args[n] it is array of parameters. Problem is that I don't know association between array elements and arguments. If I am not mistaken, I should have something similar to this:

{(MyTypeA - MyTypeB + MyTypeA)}

Where MyTypeA & MyTypeB are runtime generated types. I know that's possible to write a wrapper and send expression and array with parameters at one time. But in my situation i can send expression on server today and parameters a week later. So I ask this question.

Here a Source code.

Functionality in which I doubt:

public Expression<Func<decimal[], decimal>> Parse() method read expression string by characters.

private Expression ReadParameter(TextReader reader, Expression arrayParameter)

Creates an Expression that represents applying an array index operator.

Community
  • 1
  • 1
user6408649
  • 1,227
  • 3
  • 16
  • 40
  • 3
    I'm sorry but your question really is not clear. Are you looking for reflection based on the values in your list? – Joakim Hansson Jan 30 '17 at 08:41
  • Well, I think you need to compile code at runtime. You can see more here - http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments – eocron Jan 30 '17 at 08:56
  • I added a link to the source code. – user6408649 Jan 30 '17 at 09:01
  • Why would you need to generate types for this? The parameter name (or index, if you prefer) should be sufficient. When you later evaluate an expression, you can use those names (or indices) to look up associated values. You'll probably want to wrap those names in some kind of AST node, to differentiate them from string literals - see Linq's `ParameterExpression` for example. – Pieter Witvoet Jan 30 '17 at 09:11
  • Probably I don't something understand. Can you looks at the source code by the link at the bottom of my post? It is works and create expression from string. But I think that somewhere made a logical error. – user6408649 Jan 30 '17 at 09:27
  • The code you linked to is not complete - it's missing `Operation` and `Parentheses`. Also, which part of that code is relevant to this question? – Pieter Witvoet Jan 30 '17 at 09:30
  • I'm sorry, a class "operation" was in another file. Now is correct. – user6408649 Jan 30 '17 at 10:07
  • So all your expressions are treated as function bodies for a function that takes a single argument named `args`, of type `decimal[]`, and that returns a `decimal`? If that's the case, why would you allow expressions like `a + b` (and surprisingly turn that into `args[0] + args[1]`)? Why not just require these expressions to always use `args[..]` instead? Or maybe I don't understand your intent here - how exactly is your expression system meant to be used? – Pieter Witvoet Jan 30 '17 at 10:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134361/discussion-between-seva-and-pieter-witvoet). – user6408649 Jan 30 '17 at 10:40

0 Answers0