C#动态类在运行时? [英] C# dynamic classes at run-time?

查看:101
本文介绍了C#动态类在运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不是在谈论新的动态的关键字ñC#4.0。 :)我认为我需要去另一条路......

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

我想一类,在代码/设计时,即从一组XML文件生成或文件/文件夹组合等什么我可以用换到的extern源做。

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.

这可能与C#?我知道这是可能的一些隐藏的类名或某​​事的一些设计时compliation(即内置的属性为每个项目)。

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
{
}

和在设计时,我要的属性(或方法/场,如果是比较容易)新增基于XML文件定义。这样的:

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)...

通知的属性是如何在XML定义,并且添加到类在设计时

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

在此先感谢

推荐答案

您可以使用的 T4模板生成的源代码。这是深受微软的支持,在几个项目,包括ASP.NET MVC,很快ADO.NET实体框架使用。

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.

这篇关于C#动态类在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆