Namespace and class

I had a problem with the conflict between the DateTime class and the namespace for an unknown reason, also called DateTime.

The CompanyDateTime assembly has a Company.DateTime namespace

My application is in the namespace: Company

the problem is that every time I need to use the DateTime class, I have to explicitly say that System.DateTime is their way around this?

Is it possible to say SomeRandomStuff = Company.DateTime and have a DateTime always System.DateTime

Note:

  • I need to reference this assembly in my application, although I am not using it because some assembly I need really uses this class.
  • I can use the entry in the app.config file to identify the dependent assembly, but I can’t do this because the company’s policy is against it, and the entire assembly reference must be in the output folder.
  • Deployment occurs through the build server

Possible resolution? Is it possible to CompanyDateTime automatically deploy to the output folder without adding to the link?

+6
c # namespaces conflict
source share
4 answers

Question: the problem is that every time I need to use the DateTime class, I have to directly say that System.DateTime is any way around this

Answer: Already answered above - use an alias, for example

using CompanyDateTime = Company.DateTime;

using StandardDateTime = System.DateTime;

Question: Is it possible that CorporateDateTime can automatically expand to the output folder without adding to the link?

Answer. Put this DLL in the root folder of the application and create a postbuild event that copies it to the output folder. You can use the regular DOS COPY command here.

Link to postbuild event information

+9
source share

Yes, use the using directive at the top of your code files that reference it as follows.

 using SomeRandomStuff = Company.DateTime; 

Edit : you may need another option to eliminate the ambiguity:

 using DateTime = System.DateTime; 
+8
source share

Give the assembly an alias ( global by default, set something else for yourself). You can set this in the Visual Studio properties window when you select a link or use the compiler if you manually compile. The compiler will only allow actions in a global alias unless you specify an external alias at the top of your code file ( extern alias myalias ).

Note that this is different from the namespace alias mentioned by others. With this, you should simply use DateTime to refer to System.DateTime, and not to use another name. If you need to refer to another later, you need to specify myalias::Company.DateTime...

+4
source share

Use an alias .

If you want to use System.DateTime without using a system, try:

using SysDate = System.DateTime;

Then just specify it as a class:

SysDate mySystemDotDateTime = new SysDate();

+2
source share

All Articles