views:

72

answers:

4

Hi, I'm facing a problem with a conflict between the DateTime class and a namespace for some unknown reason was also called DateTime.

Assembly CompanyDateTime has namespace Company.DateTime

My Application is in the namespace: Company

the problem is that everytime I need to use DateTime class, I have to explicitely say System.DateTime is their any way of getting around this?

Is is possible to say SomeRandomStuff = Company.DateTime and have DateTime always be System.DateTime

Note:

  1. I need to reference this Assembly in my application eventhough I do not use it because some Assembly that I need actually uses this class.
  2. I can use an entry on the app.config file to identify a dependent assembly but I cannot do that since company policy is against it and all referenced assembly needs to be in the output folder.
  3. Deployment goes through a build server

Possible Resolution? Is is possible for CompanyDateTime to get automatically deployed to the output folder without adding it in the reference?

+5  A: 

Yes, use a using directive at the top of your code files that reference it like this.

using SomeRandomStuff = Company.DateTime;

Edit: you may also need another one to resolve the ambiguity:

using DateTime = System.DateTime;
Steve Danner
It does not work, I still need to say System.DateTime to use the DateTime class.
Vivek
Try adding another one as described in the edit.
Steve Danner
It still does not work.
Vivek
+1  A: 

Use an alias.

If you want to use System.DateTime without having to use System, then try:

using SysDate = System.DateTime;

Then, just reference it like you would a class:

SysDate mySystemDotDateTime = new SysDate();

Ryan Hayes
I used the postbuild command rather than using an alias. It would have worked but I found the other solution better. Thank you.
Vivek
+1  A: 

Give the assembly an alias (the default is global, set yours to something else). You can set this in Visual Studio's property window when you select a reference, or using a compile switch if you're manually compiling. The compiler will only resolve things in the global alias, unless you specify an external alias at the top of your code file (extern alias myalias).

Note, this is different from the namespace alias others have mentioned. With this you should simply be able to use DateTime to refer to the System.DateTime, rather than using another name. If you need to refer to the other one later, you'd need to specify myalias::Company.DateTime...

Mark H
Yes It did work for me and compiled properly but I lost the Intellsence for the variable. Thats the only reason I did not use it. Thank you.
Vivek
+2  A: 

Question : the problem is that everytime I need to use DateTime class, I have to explicitely say System.DateTime is their any way of getting around this

Answer : Already answered above - use an Alias eg

using CompanyDateTime = Company.DateTime;

using StandardDateTime = System.DateTime;

Question : Is is possible for CompanyDateTime to get automatically deployed to the output folder without adding it in the reference?

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

Refer link for postbuild event details

InSane
I used your postbuild command to copy the file over. It worked and I'm hoping that It still works on the build server. Thank you
Vivek