Visual Studio 2008为c#提供了两个很棒的功能,称为"使用指令排序"和"删除未使用的指令".
每次使用ctrl + k,ctrl + d格式化代码时,我都想调用"使用指令排序".
或者,更好的是,我希望能够重新格式化项目中的所有c#-source文件,并为所有源文件调用"sort using directives".
我怎样才能做到这一点?手动打开每个cs文件并在每次签入之前键入这些函数是单调乏味的!
c# code-formatting using-directives visual-studio-2008 visual-studio
我来自一个VB.Net使用Imports System然后IO.Directory.GetFiles(...)工作的环境.
另一方面,似乎using System;不足以在IO.Directory 没有前缀的情况下编写使用System..唯一的解决方法似乎是
using IO = System.IO;
为什么?
示例代码:
using System;
using System.IO;
namespace Test {
class Program {
static void Main(string[] args) {
System.Console.WriteLine(IO.Directory.GetFiles(System.Environment.CurrentDirectory)[0]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我的问题不是我应该怎么做才能使我的代码工作,但具体是"为什么我不能写IO.Directory.GetFiles?"
基础参考:VBA,Visual Basic .NET和C#的十个代码转换
注意:我已经创建并导入了一个*.dll,这个问题是关于 别名的.
假设一个Test类的程序化名称是TestNameSpace.Test
[ProgId("TestNamespace.Test")]
public class Test ...
Run Code Online (Sandbox Code Playgroud)
现在,假设一个C#解决方案已被密封并编译成一个*.dll,我在Excel的VBE中引用它.注意:此时我无法修改程序名称,就好像它*.dll不是由我编写的.
这是VBA:而不是像这样声明一个变量:
Dim myTest As TestNameSpace.Test
Set myTest = new TestNameSpace.Test
Run Code Online (Sandbox Code Playgroud)
我更喜欢称之为(仍在VBE中)
Dim myTest As Test
Set myText = new Test
Run Code Online (Sandbox Code Playgroud)
在C#中你通常会说
using newNameForTest = TestNamespace.Test;
newNameForTest myTest = new NewNameForTest;
Run Code Online (Sandbox Code Playgroud)
注意:假设有没有命名空间冲突的VBA项目
问:是否有一个等价的调用VBA来C# using或VB.NET imports别名?
是否有可能有一个using范围仅限于一个类的指令?
请注意,我想要"使用"的内容不包含在当前类的父级中.
为简单起见,假设以下例子:
#include<vector>
class foo
{
using std::vector; //Error, a class-qualified name is required
}
Run Code Online (Sandbox Code Playgroud)
另一个有趣的事情是,如果using包含指令,如果包含头:
MyClassFoo.h:
#include<vector>
using std::vector; //OK
class foo
{
}
Run Code Online (Sandbox Code Playgroud)
并在
NewHeader.h
#include "MyClassFoo.h"
...
Run Code Online (Sandbox Code Playgroud)
我怎么能阻止" using std::vector"在这里可见?
对于我的项目,我正在使用一些非常复杂的数据结构,例如
std::unordered_map<int, std::list<std::shared_ptr<const Foo>>>
Run Code Online (Sandbox Code Playgroud)
为此,我想声明类型别名以提高可读性。我在其上构建项目的代码已经通过将using语句全局放在头文件中来实现:
std::unordered_map<int, std::list<std::shared_ptr<const Foo>>>
Run Code Online (Sandbox Code Playgroud)
由于我的C ++知识有点生锈,所以我只是采用了这种样式-但现在我读到,using以这种方式使用可能会出现问题,因为它将这种别名强制应用于包含此标头的所有内容。
尽管进行了大量的谷歌搜索,但我找不到如何正确处理此问题的具体答案,只有很多关于不该做什么的陈述。因此,我只是将use放在类中:
// bar.h
#ifndef BAR_H
#define BAR_H
#include <unordered_map>
#include <list>
#include <memory>
#include "foo.h"
using FooTable = std::unordered_map<int, std::list<std::shared_ptr<const Foo>>>;
class Bar {
FooTable create_foo();
};
#endif
Run Code Online (Sandbox Code Playgroud)
但是,这样做的缺点是,我需要在源文件中重新声明别名:
// bar.h
#ifndef BAR_H
#define BAR_H
#include <unordered_map>
#include <list>
#include <memory>
#include "foo.h"
class Bar {
using FooTable = std::unordered_map<int, std::list<std::shared_ptr<const Foo>>>;
FooTable create_foo();
};
#endif
Run Code Online (Sandbox Code Playgroud)
尽管这似乎可行,但我不确定这是否安全...而且我的直觉告诉我这有点丑陋。因此,在我像这样重写整个项目之前,我想过要问:有没有一种更好/更优雅/更安全的方法呢?还是我应该完全避免在头文件中使用类型别名?
我的代码中有一个函数,它只接受类成员方法作为模板参数。我需要使用从父类继承的类方法来调用此方法。这是我的问题的示例代码:
template <class C>
class Test {
public:
template<typename R, R( C::* TMethod )()> // only a member function should be accepted here
void test() {}
};
class A {
public:
int a() { return 0; } // dummy method for inheritance
};
class B : public A {
public:
using A::a; // A::a should be declared in the B class declaration region
// int a() { return A::a(); } // if this lines is activated compliation works
};
int …Run Code Online (Sandbox Code Playgroud) 我过去几个月一直在使用ReSharper,除了广告之外,如果没有它,我看不到自己编码.因为我喜欢生活在流血的"什么地狱出错"的边缘,所以我决定试试我的运气w /最新的ReSharper 4.5夜间版本.这一切都很好.
但是,我注意到using指令分组格式已经改变,我想知道哪个更接近一般标准:
[旧]
#region Using directives
using System.X;
using System.Y;
using System.Z;
using System.A;
#region
namespace X { ... }
Run Code Online (Sandbox Code Playgroud)
[新]
namespace X {
#region Using directives
using System.X;
using System.Y;
using System.Z;
using System.A;
#region
...
}
Run Code Online (Sandbox Code Playgroud)
除了延迟加载引用之外,它是否可以用于任何特殊目的?(正在阅读Scott Hanselman对此的看法@ http://www.hanselman.com/blog/BackToBasicsDoNamespaceUsingDirectivesAffectAssemblyLoading.aspx)
谢谢;
我很确定我知道答案,但我想知道是否有办法在我的C#项目中定义全局"使用"指令,这样我就不必在每个代码文件的顶部重复该指令.
我的兴趣实际上源于在.NET Framework中引入扩展方法.使用扩展方法的唯一方法是为包含扩展方法的命名空间定义using指令.没有using指令,我失去了扩展方法的Intellisense功能,这意味着我不会总是看到可用的方法.
作为框架开发人员,确保框架中提供的类型和方法清晰可供开发人员使用对我来说至关重要.虽然文档和培训服务于他们的目的,但我发现大多数开发人员都会按时完成并滚动智能感知列表以查看可用的方法和属性.即使他们转到对象浏览器或查看参考文档,他们也不会知道扩展方法,除非他们知道它.这就是Intellisense的用武之地.
而且,虽然我可以将使用指令添加到VS使用的模板,但VS中的"删除并排序"选项将删除引用扩展方法的指令(如果没有使用).
那么,所有这一切,是否有任何方法可以在VS 2010中定义全局"使用"指令?如果没有,MS将来会考虑它的任何机会吗?
c# using-directives using-statement .net-4.0 visual-studio-2010
#include <iostream>
using namespace std;
void swap(int *a, int *b) {
*a = *a^*b;
*b = *a^*b;
*a = *a^*b;
}
int main()
{
int array[]={1,9,2,8,3,7};
for(int i=0; i<6; i++)
cout<<array[i];
cout<<endl;
swap(array[1], array[4]);
for(int i=0; i<6;i++)
cout<<array[i];
cout<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以上是一个测试样本.我发现如果使用swap(array[1], array[4]);它,它也会交换数组中两个位置的值.但这让我感到困惑,因为函数swap()需要两个指针,而不是两个整数值.
谢谢你的帮助:)
c++ swap namespaces using-directives argument-dependent-lookup
using-directives ×10
c# ×6
c++ ×4
using ×2
.net-4.0 ×1
alias ×1
boilerplate ×1
c++11 ×1
coding-style ×1
gcc ×1
namespaces ×1
scope ×1
swap ×1
type-alias ×1
vb.net ×1
vba ×1