Possible to alias .NET namespaces in app.config?

早过忘川 提交于 2019-12-04 03:11:10

问题


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>

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;

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.


回答1:


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?

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.

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



来源:https://stackoverflow.com/questions/1159142/possible-to-alias-net-namespaces-in-app-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!