简体   繁体   English

Visual Studio 2010中的参考转发(从库到可执行文件)

[英]Reference forwarding in Visual Studio 2010 (from library to executable)

I have a C# library which has System.Data.SQLite as a reference, and an executable (as a 2nd project) which uses the library. 我有一个C#库,其中有System.Data.SQLite作为参考,还有一个使用该库的可执行文件(作为第二个项目)。

In the library, I have a function which references a class in System.Data.SQLite (Specifically the SQLiteDataReader class): 在该库中,我有一个函数引用了System.Data.SQLite中的SQLiteDataReader类(特别是SQLiteDataReader类):

public static IEnumerable<T> SQLiteFetch<T>(Func<SQLiteDataReader, T> formator, string connectionString, string query)

The library compiles properly, but not the executable which calls this function. 该库可以正确编译,但是不能编译调用此函数的可执行文件。

This comes up as an error on build, saying I "must add a reference to assembly 'System.Data.SQLite'". 这是在构建时出现的错误,说我“必须添加对程序集'System.Data.SQLite'的引用”。 I get that it's due to the IEnumerable, but why isn't the reference forwarded through my library? 我知道这是由于IEnumerable造成的,但是为什么引用不通过我的库转发呢? Do I really have to add a reference to System.Data.SQLite in my executable as well? 我真的也必须在可执行文件中添加对System.Data.SQLite的引用吗?

An assembly is only aware of its immediate references (such as your library), and does not recursively load the references of its references, so you will have to add the reference to your executable. 程序集仅知道其直接引用(例如您的库),而不会递归地加载其引用的引用,因此您必须将引用添加到可执行文件中。

Your library can include a wrapper class that inherits from SQLiteDataReader to get around this, but I don't see any particular advantage of doing this other than if you really don't want the executable to have the extra reference. 您的库可以包含一个继承自SQLiteDataReader的包装器类,以解决此问题,但是除了您真的不希望可执行文件具有额外的引用之外,我看不出这样做的任何特殊优势。

Yes, you have to add a reference to your executable because when you are creating your formator , .NET expects it to be of type Func<SQLiteDataReader, T> . 是的,您必须添加对可执行文件的引用,因为在创建formator程序时,.NET希望它的类型为Func<SQLiteDataReader, T> And since your executable doesn't have a reference to System.Data.SQLite, it doesn't know how to deal with your formator. 而且由于您的可执行文件没有对System.Data.SQLite的引用,因此它也不知道如何处理格式化程序。 This is the case even if you pass null as the formator. 即使您将null作为格式器传递,也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM