代码之家  ›  专栏  ›  技术社区  ›  demoncodemonkey

如何阻止新表单使用命名空间System::Collections

  •  1
  • demoncodemonkey  · 技术社区  · 16 年前

    如果我创建一个名为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;
    

    这些都不需要,因为出色的表单设计器总是完全限定其对象。

    用这个标记的那个特别讨厌,因为它破坏了我的身材。这是因为我到处都在使用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'
    

    那么,我怎样才能阻止一个新表单添加所有这些无用的和破坏性的用法呢?

    2 回复  |  直到 16 年前
        1
  •  2
  •   Boris Modylevsky    16 年前

    您可以修改VisualStudio的表单模板。如需进一步阅读,请参阅:

    http://msdn.microsoft.com/en-us/library/ms185319(VS.80).aspx

        2
  •  2
  •   womp    16 年前

    C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033

    就是C#模板所在的位置。我假设C++模板将在一个类似的目录中,但是我没有安装有C++的HANY实例。

    推荐文章