什么是using语句并添加一个参考值之间的差异? [英] What's the difference between using statement and adding a reference?
问题描述
在Visual Studio中,当你有一个引用添加到一个dll?我总是有一个最低在我的项目引用,我尽量只包括那些真正需要的人。
In Visual Studio, when do you have to add a reference to a dll? I always try to have a minimum of references in my projects, I try to only include the ones that are really necessary.
我想我只需要一个引用我有一个使用
语句在我的源代码。但是,这还不够。
I would think that I only need a reference if I have a using
statement in my source. But that's not always enough.
比如说,我有一个非常简单的程序中使用系统和微软是。 Practices.EnterpriseLibrary.Data:
For instance, I have a very simple program that is using
System and Microsoft.Practices.EnterpriseLibrary.Data:
using System;
using Microsoft.Practices.EnterpriseLibrary.Data;
public class SimpleConnection {
private static void Main() {
var database = DatabaseFactory.CreateDatabase();
var command =
database.GetSqlStringCommand(
"select table_name from information_schema.tables");
using (var reader = database.ExecuteReader(command)) {
while (reader.Read()) {
Console.WriteLine(reader.GetString(0));
}
}
}
}
我会的认为的我只需要参考系统和Microsoft.Practices.EnterpriseLibrary.Data。但事实并非如此。如果我不引用System.Data这,代码将无法编译。
I would think I only have to reference System and Microsoft.Practices.EnterpriseLibrary.Data. But that's not true. If I don't reference System.Data, the code won't compile.
类型
'System.Data这。 Common.DbCommand'是在装配不是引用
定义
。您必须添加一个引用
到组装'System.Data这,
版本2.0.0.0 =文化=中立,
公钥= b77a5c561934e089'。
The type 'System.Data.Common.DbCommand' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
我怎么能事先知道,当我要补充的东西引用我不是使用
?
How can I know beforehand when I have to add a reference to something I'm not using
?
推荐答案
您必须添加一个引用到类驻留在装配和任何相关性,包括
You have to add a reference to an assembly the class resides in, and any dependencies, that includes
- 从其他组件的返回类型(即一个方法返回一个
的DbCommand
) - 基类或接口与其他组件(即一类从
的DbCommand
派生或实现一个接口)
- return types from other assembly (ie. a method returns a
DbCommand
) - base class or interface from other assembly (ie. a class derives from
DbCommand
or implements an interface)
这篇关于什么是using语句并添加一个参考值之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!