命名空间 &阶级冲突
我面临着 DateTime 类和命名空间之间的冲突问题,由于某种未知原因也称为 DateTime。
程序集 CompanyDateTime 具有命名空间 Company.DateTime
我的应用程序位于命名空间中: Company
问题是每次我需要使用 DateTime< /strong> 类,我必须明确地说 System.DateTime 他们有办法解决这个问题吗?
可以说 SomeRandomStuff = Company.DateTime 并让 DateTime 始终为 System.DateTime
注意:
- 我需要在我的应用程序中引用此程序集尽管我不使用它,因为我需要的某些程序集实际上使用了此类。
- 我可以使用 app.config 文件上的条目来标识依赖程序集,但我不能这样做,因为公司政策反对它,并且所有引用的程序集都需要位于输出文件夹中。
- 部署通过构建服务器
可能的解决方案? CompanyDateTime 是否可以自动部署到输出文件夹而不将其添加到引用中?
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:
- 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.
- 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.
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题:问题是每次我需要使用 DateTime 类时,我都必须明确地说 System.DateTime 是他们解决此问题的任何方法
答案:上面已经回答了 - 使用别名,例如
using CompanyDateTime = Company.DateTime;
using StandardDateTime = System.DateTime;
问题: CompanyDateTime 是否可以自动部署到输出文件夹而不添加它在参考文献中?
答案:将此 dll 放入应用程序根文件夹中,并创建一个构建后事件,将其复制到输出文件夹。您可以在此处使用常规 DOS COPY 命令。
有关构建后事件详细信息的参考链接
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
是的,在代码文件的顶部使用 using 指令来引用它,如下所示。
编辑:您可能还需要另一个来解决歧义:
Yes, use a using directive at the top of your code files that reference it like this.
Edit: you may also need another one to resolve the ambiguity:
为程序集指定一个别名(默认为
global
,将您的别名设置为其他名称)。您可以在选择引用时在 Visual Studio 的属性窗口中进行设置,或者在手动编译时使用编译开关进行设置。编译器只会解析全局别名中的内容,除非您在代码文件顶部指定外部别名 (extern alias myalias
)。请注意,这与其他人提到的名称空间别名不同。有了这个,您应该能够使用
DateTime
来引用 System.DateTime,而不是使用其他名称。如果您稍后需要引用另一个,则需要指定myalias::Company.DateTime...
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 specifymyalias::Company.DateTime...
使用别名< /a>.
如果您想使用 System.DateTime 而不必使用 System,请尝试:
using SysDate = System.DateTime;
然后,只需像引用类一样引用它:
SysDate mySystemDotDateTime = new SysDate ();
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();