Viv
Viv

Reputation: 2595

Namespace & Class Conflict

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?

Upvotes: 6

Views: 11576

Answers (4)

Mark H
Mark H

Reputation: 13907

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

Upvotes: 4

Ryan Hayes
Ryan Hayes

Reputation: 5310

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();

Upvotes: 2

Steve Danner
Steve Danner

Reputation: 22168

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;

Upvotes: 8

Jagmag
Jagmag

Reputation: 10366

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

Upvotes: 11

Related Questions