Reputation: 2595
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:
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
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
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
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
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