Is the .NET alias namespaces possible in app.config?

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

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

I am curious to see if this configuration is available for other .net environments (e.g. winforms, console applications, and in particular Silverlight applications). If so , then the next question is whether we can have a namespace alias in the specified configuration.

An analog of this bit of code, but through the configuration:

 using MyNamespace = System.Web.Mvc; 

edit: my intention comes from viewing projects like silversprite , whose goal is to provide an identical XNA API for silverlight. This allows you to write an XNA game once and then deploy it online using silverlight. The only problem is that the whole version of the silversprite APIs is in a different namespace, so to use it you need to use ifdef around using statements. It would be great if it were just a silversprite namespace alias so that your code shouldn't change between platforms.

+7
namespaces alias configuration
source share
1 answer

Not. Namespaces are required at compile time. These are only scenarios where compilation is done late (as in ASP.NET), where this makes sense.

What would it mean to add a namespace at runtime if the code is already compiled?

Do you really avoid writing a bunch of using directives at the top of each file? If so, and if C # supports it, it will be in the project properties (which relate to compilation, not execution). However, C # does not support this β€” the only imported namespace is those specified in the using directives in the current file.

I think VB has the idea of ​​a "default namespace", but C # definitely does not. Personally, I think this is good. (You might also want to look at this question . I do not know if this is actually a duplicate or not, as your intention is still unclear.)

+10
source share

All Articles