如何阻止新表单使用命名空间 System::Collections
如果我创建一个名为 myForm 的新表单,则 myForm.h 的顶部如下所示:
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections; //<<<< THIS ONE
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
这些都不需要,因为出色的表单设计器总是完全限定其对象。
标有“THIS ONE”的那个特别烦人,因为它破坏了我的构建。这是因为我到处都使用 IList 的通用形式 - 我非常喜欢它,所以我把它放在 stdafx.h 中,如下所示:
using System::Collections::Generic::IList;
那么如果我想从我碰巧使用 IList 的任何其他文件中使用 myForm ,像这样:
#include "StdAfx.h"
#include "ABC.h"
#include "myForm.h"
ABC::ABC()
{
IList<int>^ myList;
...
}
然后它无法编译:
1>.\ABC.cpp(7) : error C2872: 'IList' : ambiguous symbol
1> could be 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::Generic::IList'
1> or 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::IList'
1>.\ABC.cpp(7) : error C2872: 'IList' : ambiguous symbol
1> could be 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::Generic::IList'
1> or 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::IList'
那么,我怎样才能阻止新表单添加所有这些无用和破坏性的用法?
If I create a new form called myForm, the top of myForm.h looks like this:
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections; //<<<< THIS ONE
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
None of these are even needed, because the wonderful forms designer always fully-qualifies its objects.
The one marked with THIS ONE is particularly annoying because it breaks my build. This is because I use the generic form of IList all over the place - I love it so much that I put it in stdafx.h, like this:
using System::Collections::Generic::IList;
So then if I want to use myForm from any other file where I happen to use IList, like this:
#include "StdAfx.h"
#include "ABC.h"
#include "myForm.h"
ABC::ABC()
{
IList<int>^ myList;
...
}
then it fails to compile:
1>.\ABC.cpp(7) : error C2872: 'IList' : ambiguous symbol
1> could be 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::Generic::IList'
1> or 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::IList'
1>.\ABC.cpp(7) : error C2872: 'IList' : ambiguous symbol
1> could be 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::Generic::IList'
1> or 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::IList'
So, how can I stop a new form from adding all these useless and destructive usings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过编辑您使用的特定语言的 ItemTemplates 目录中的 zip 文件来更改 Visual Studio 使用的默认模板。
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033
是 C# 模板所在的位置。我假设 C++ 模板位于类似的目录中,但我没有方便地安装 C++ 的 VS 实例。
You can change the default templates that Visual Studio uses by editing the zip files in the ItemTemplates directory for the specific language that you use.
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033
is where the C# templates are. I'm assuming the C++ templates would be in a similar directory but I don't have a VS instance installed with C++ handy.
您可以修改Visual Studio的表单模板。如需进一步阅读,请查看:
http:// msdn.microsoft.com/en-us/library/ms185319(VS.80).aspx
You can modify Form Template of Visual Studio. For further reading, take a look at:
http://msdn.microsoft.com/en-us/library/ms185319(VS.80).aspx