当前位置:编程学习 > C#/ASP.NET >>

泛型接口的问题

我有个第三方dll,在自己工程里引用,发现第三方dll中有个IComputable<T1, T2>引用不到,而IComputable<T>可以引用

于是我找dll的源码看,发现有两个文件名相似的文件:IComputable`1.cs 和 IComputable`2.cs

IComputable`1.cs里有 public interface IComputable<T>
IComputable`2.cs里有 public interface IComputable<TInput, TOutput>: IComputable<TOutput>

我以前都是用C++,刚刚接触C# 想问下:
1.IComputable`1.cs IComputable`2.cs这两个文件什么关系?
2.怎么才能引用到IComputable<T1, T2>这个接口? --------------------编程问答-------------------- 1)这两个文件就相当于你c++的两个头文件(.h)而已;
2)把文件IComputable`2.cs添加进引用就行,鼠标右击Reference文件夹,添加dll;最后再文件顶部解析一下,即假如你的IComputable`2在dll中的名称空间xxx下,或者鼠标右击IComputable<T1, T2>解析:

using xxx;
--------------------编程问答-------------------- 谢谢sudazf!
dll已经加到引用路径了,文件顶部也加了using xxx声明,并且IComputable<T>和IComputable<TInput, TOutput>都在这个命名空间下面。为什么一个能用一个不能用呢?
IComputable<T>和IComputable<TInput, TOutput>接口名一样,只是IComputable<TInput, TOutput>是对IComputable<T>进一步的泛化,在dll源码中是这样声明的:
public interface IComputable<TInput, TOutput>: IComputable<TOutput>
--------------------编程问答-------------------- 1)我刚刚试了一下,新建了个类库工程,生成了dll文件:ClassLibrary1
该dll项目下有两个class1.cs,class2.cs文件,对应你的那两个.cs文件;
class1.cs中定义了你的第一个委托:

    public interface IComputable<T>
    { 
    }

class2.cs中定义了你的第二个委托:

    public interface IComputable<TInput, TOutput> : IComputable<TOutput>
    {
 
    }

2)我在测试项目下,引用了该dll,并且在文件头部引用了:

using ClassLibrary1;

然后使用它们:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary1;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IComputable<object> ss;
            IComputable<object, object> dd;
        }
    }
}

没有任何引用不到的问题呀!你看看是不是你的dll文件里引用出了问题。
--------------------编程问答-------------------- 主要是持cs文件IComputable<TInput, TOutput>上面的namespace
然后在你的程序中先using
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,