可以在app.config中别名.NET命名空间? [英] Possible to alias .NET namespaces in app.config?

查看:237
本文介绍了可以在app.config中别名.NET命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET具有一项功能,允许你声明隐含使用的命名空间在web.config中。

ASP.NET has a feature that allows you to declare implicitly used namespaces in the web.config.

<configuration>
 <system.web>
  <pages>
   <namespaces>
    <add namespace="System.Web.Mvc"/>
   </namespaces>
  </pages>
 </system.web>
</configuration>

我很想知道,如果配置为其他.NET环境(如的WinForms,控制台应用程序,特别是,Silverlight应用程序)有这个能力。 如果这样,然后跟进问题是,我们是否能够别名命名空间中所述配置。

I'm curious to know if configuration for other .net environments (like winforms, console apps, and in particular, silverlight applications) have this ability. If so, then the follow up question is whether we are able to alias a namespace in said configuration.

的code此位的模拟,而是通过配置:

The analog of this bit of code, but via configuration:

using MyNamespace = System.Web.Mvc;

编辑:我的意思是来自在看项目,如 silversprite 其目的是为提供一个相同的API XNA为Silverlight。这允许你写一个XNA游戏一次,然后使用Silverlight将其部署到Web。唯一的问题是,所有的原料药的silversprite版本在不同的命名空间,所以使用它,你需要使用的ifdef放在using语句各地。这本来是真棒,如果可以简单地别名silversprite命名空间,使您的code就不必平台之间切换。

edit: my intention comes from looking at projects such as silversprite which aims to provide an identical API for XNA for silverlight. This allows you to write an XNA game once, and then deploy it onto the web using silverlight. The only problem is that all of the silversprite version of the APIs are in a different namespace, so to use it you need to use an ifdef around the using statements. It would have been awesome if one could simply alias the silversprite namespace so that your code wouldn't have to change between platforms.

推荐答案

没有。命名空间,以便于的编译时间的。这是只有在编译完成的迟到(如ASP.NET)场景,这使得任何意义。

No. Namespaces are required at compile time. It's only scenarios where the compilation is done "late" (as in ASP.NET) where this makes any sense.

会是什么,甚至意味着增加一个命名空间在执行时,如果code已被编译?

What would it even mean to add a namespace at execution time, if the code has already been compiled?

您的真正的只是该选项后,避免写使用指令在每个文件的顶部一堆的?如果是这样,和如果的C#支持的话,这将是在项目属性(这是关于编制,不执行)。然而,C#的的支持 - 只有进口的命名空间是使用指令在当前文件中通过中指定的

Are you really just after the option to avoid writing a bunch of using directives at the top of each file? If so, and if C# supported it, that would be in the project properties (which are about compilation, not execution). However, C# doesn't support that - the only imported namespaces are the ones specified by using directives in the current file.

我的认为的VB有默认命名空间,但C#的想法肯定没有。我个人认为这是一件好事。 (您可能还想看看这个问题。我不知道它是否是有效的重复或没有,因为你的本意ISN 'T清楚的时刻。)

I think VB has the idea of "default namespaces" but C# definitely doesn't. Personally I think that's a good thing. (You might also want to look at this question. I don't know whether it's effectively a duplicate or not, as your intention isn't clear at the moment.)

这篇关于可以在app.config中别名.NET命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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