2
出於實驗目的,我試圖構建一個二叉搜索樹庫,從中可以實例化BST以保存唯一或冗餘節點(冗餘節點爲一個其值等於樹中的另一個值)。爲了可重用性,我定義了一個通用接口ITree和兩個子接口:IUnique和IRedundant。通用接口庫:無法從程序集中加載類型
正在爲我的答覆Explicit C# interface implementation of interfaces that inherit from other interfaces的原因,庫代碼可以證明如下[文件名:itest.cs]:
namespace MyNS {
public interface INode<T,N>
where N : INode<T,N> {
N LChild { get; set; }
N RChild { get; set; }
T Value { get; set; }
}
public interface ITree<T,N,I>
where N : INode<T,N>
where I : ITree<T,N,I> {
void Add(N node);
}
public interface IUnique<T,N>
: ITree<T,N,IUnique<T,N>>
where N : INode<T,N> {
}
public interface IRedundant<T,N>
: ITree<T,N,IRedundant<T,N>>
where N : INode<T,N> {
}
public class Node<T>
: INode<T,Node<T>> {
public Node<T> LChild { get; set; }
public Node<T> RChild { get; set; }
public T Value { get; set; }
}
public class Tree<T>
: IUnique<T,Node<T>>,
IRedundant<T,Node<T>> {
void ITree<T,Node<T>,IUnique<T,Node<T>>>.Add(Node<T> node) {
/// Add node only if there is none with an equivalent value ///
}
void ITree<T,Node<T>,IRedundant<T,Node<T>>>.Add(Node<T> node) {
/// Add node regardless of its redundancy ///
}
}
}
而且一例的主要方法[文件名:主的.cs]:
public class ITest {
public static void Main() {
System.Console.WriteLine(typeof(MyNS.Tree<int>));
}
}
試圖編譯庫如從下面的錯誤主可執行文件的結果的單獨組件:
$ mcs -out:itest.dll -t:library itest.cs
$ mcs -out:itest.exe main.cs -reference:itest
error CS0011: Could not load type 'MyNS.ITree`3[T,N,MyNS.IUnique`2[T,N]]' from assembly 'itest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
Compilation failed: 1 error(s), 0 warnings
然而,編譯兩人在一起工作完全按預期:
$ mcs -out:itest.exe main.cs itest.cs
$ mono itest.exe
MyNS.Tree`1[System.Int32]
爲了保持模塊化,我怎麼可能想保留該庫從我的應用程序邏輯中分離出來?
編輯(2010年1月11日):是的,這是一個單聲道2.8.x的bug,並已在2.10版本中修復。
在.NET上正常工作,我會提交一個單聲道錯誤。 – 2011-01-11 16:08:04
@Hans Passant,完成。 https://bugzilla.novell.com/show_bug.cgi?id=663727 – Dylon 2011-01-11 17:16:57