通用中没有静态.但是如何实现这一目标呢?

Boo*_*mer 0 .net c# oop

请检查以下代码部分(简化版)

我担心的是ReadPath我需要调用我正在使用的类型的GetPath()的类.我怎样才能做到这一点?

public interface IPath
{
    string GetPath();
}

public class classA: IPath
{
    string GetPath()
    {
        return "C:\";
    }
}
public class classB: IPath
{
    string GetPath()
    {
        return "D:\";
    }
}
public class ReadPath<T> where T : IPath
{        
    public List<T> ReadType()
    {
        // How to call GetPath() associated with the context type.
    }        
}
Run Code Online (Sandbox Code Playgroud)

dar*_*yal 5

public interface IPath
{
    string GetPath();
}

public class classA : IPath
{
    public string GetPath()
    {
        return @"C:\";
    }
}
public class classB : IPath
{
    public string GetPath()
    {
        return @"D:\";
    }
}
public class ReadPath<T> where T : IPath, new()
{
    private IPath iPath;
    public List<T> ReadType()
    {
        iPath = new T();
        iPath.GetPath();
        //return some list of type T

    }
}
Run Code Online (Sandbox Code Playgroud)