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

查看:346
本文介绍了可能在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,控制台应用程序,特别是银光应用)具有这种能力。 如果这样,则后续问题是我们是否能够在所述配置中对命名空间进行别名。

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.

此位代码的模拟,但通过配置:

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

using MyNamespace = System.Web.Mvc;

编辑:我的意图来自于查看诸如 silversprite ,其目的是为Silverlight的XNA提供相同的API。这允许你编写XNA游戏一次,然后使用silverlight将其部署到Web上。唯一的问题是所有的silversprite版本的API在不同的命名空间,所以要使用它,你需要使用ifdef周围的using语句。

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.

在执行时添加命名空间时间,如果代码已经编译?

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#绝对不是。我个人认为这是一件好事。 (您可能还需要查看此问题。我不知道它是否有效重复,因为您的意图目前无法清除。)

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天全站免登陆