title: How to define a C# object at run time?
body: <p>We have XML code stored in a single relational database field to get around the Entity / Attribute / Value database issues, however I don't want this to ruin my Domain Modeling, DTO, and Repository sunshine.  I cannot get around the EAV/CR content, but I can choose how to store it.  Question is how would I use it?</p>
  
  <p>How could I turn the XML metadata in to a class / object at run time in C#?</p>
  
  <p>For example:</p>
  
  <p>XML would describe that we have a food recipe which has various attributes, but usually similar, and one or more attributes about making the food.  The food itself can literally be anything and have any type of crazy preparation.  All attributes are searched and may link to existing nutritional information.</p>
  
  <pre><code>// &lt;-- [Model validation annotation goes here for MVC2]
  public class Pizza {
       public string kind  {get; set;}
       public string shape {get; set;}
       public string city  {get; set;}
       ...
  }
  </code></pre>
  
  <p>and in the ActionMethod:</p>
  
  <pre><code>makePizzaActionMethod (Pizza myPizza) {
      if (myPizza.isValid() ) {  // this is probably ModelState.isValid()...
          myRecipeRepository.Save( myPizza);
          return View("Saved");
      }
      else
          return View();
  }
  </code></pre>