tags:

views:

478

answers:

3

First, I am not talking about the new 'dynamic' keyword n C# 4.0. :) I believe I need to go the other way...

I want a class, at code/design-time, that is generated from a set of XML files, or files/folder combinations, etc. Anything I can do with a change to the extern source.

Is this possible with C#? I know it is possible with some design-time compliation of some hidden class name or something (i.e. the built in Properties for each project).

Is there an easier way to do this? ALmost like a dynamic enum? Here's what I am trying to do:

public static class MenuItems
{
}

And at design-time, I want the properties (or methods/fields, if that's easier) added based on an XML file definition. Like:

<MenuItems>
  <Item>Homepage</Item>
  <Item>Portfolio</Item>
  <Item>About</Item>
  <Item>Contact</Item>
</MenuItem>

Then, while coding in the designer I would have intellisense like:

if (page.ToString == MenuItems.Homepage)...
if (page.ToString == MenuItems.Portfolio)...

Notice how the properties were defined in the XML, and added to the class at design time?

Thanks in advance!

+1  A: 

If I may direct your attention to the ASP.NET Membership Provider, and the Web.sitemap, perhaps it will be of assistance to your actual problem.

Noon Silk
Right. Those create design-time properties. I'm looking for someone that has done the leg work already. :) Spent a few days tinkering with my own solutions that didn't work out... :)
eduncan911
But what exactly are you trying to do? I'm assuming you want to either hide/show relevant links based on a role, and also prevent access based on role. What I linked does that. If you're looking at just styling based on name, you do that per-page or similar. What are you actually trying to do?
Noon Silk
I am not sure where you are getting the "Role" or membership in my original question. :) I am asking to create a dynamic class based from a config file, at runtime. T4 Templates mentioned by Matt does it; but, it has to be compiled to generated that code ahead of time. I am looking for something almost real-time - i.e., has a dependency on an xml file and if the xml changes, it generates a new class version.
eduncan911
I was inferring it from your question; that's why I asked you what you're actually doing. I'll leave you to the templating solution, or some runtime type generation using `Reflection.Emit`. Personally though, I'd avoid that at all costs and address the real issue.
Noon Silk
Thanks silky! Yeah, the "real issue" is MVC uses "magic strings" everywhere... No strong-typed Views or RedirectResults. I am creating additional extensions beyond the Futures project has.
eduncan911
+3  A: 

You can use T4 Templates to generate source code. It is well supported by Microsoft and is used on several projects including ASP.NET MVC, and soon the ADO.NET Entity Framework.

Matt Brunell
Excellent point. But, T4 templates have to generate the source code. I am looking for a dynamic solution. Almost realtime changes taking effect.
eduncan911
Marking this as the answer for now. Seems I want too much from C#. :)
eduncan911
If you want your class to be generated entirely at run-time, then how is the rest of your code (which references members of that class) is supposed to compile? Or where would IDE obtain information needed to show IntelliSense tips from?
Pavel Minaev
I don't think you can -- not at runtime. I think this question may be mis-titled. The body of the question describes a compile-time feature more than a run-time one.
Matt Brunell
+1  A: 

I just encountered a situation where we need the same functionality at design-time. Basicly, based on the message type, we want only the valid properties as defined by an xml file to be exposed. If anyone came up with a working solution for this, please let me know.

Paul
Now that I have used dynamic more, and more importantly classes that inherit from the DynamicObject() class with TryGetMember and TrySetMember overrides, works great now. I need to delete this question...
eduncan911