C#泛型方法是否可以像C+一样重载+;函数重载 C3泛型方法重载,类似于C++函数重载。

C#泛型方法是否可以像C+一样重载+;函数重载 C3泛型方法重载,类似于C++函数重载。,c#,generics,C#,Generics,下面的代码是重载泛型方法的正确方法吗 class ReadFile<T> : IDisposable { private FileStream fstream; private BinaryReader br; public ReadFile(string filename) { // do all the initialization } public void readValue(double val)

下面的代码是重载泛型方法的正确方法吗

class ReadFile<T> : IDisposable
{

    private FileStream fstream;
    private BinaryReader br;

    public ReadFile(string filename)
    {
       // do all the initialization
    }

    public void readValue(double val)
    {
       val = br.ReadDouble();
    }

    public void readValue(float val)
    {
       val = br.ReadSingle();
    }

    public void readValue(T val)
    {
       throw new Exception("Not implemented");
    }
}
类读取文件:IDisposable
{
私有文件流;
私有二进制读取器br;
公共读取文件(字符串文件名)
{
//执行所有初始化
}
公共无效读取值(双val)
{
val=br.ReadDouble();
}
公共无效读取值(浮动值)
{
val=br.ReadSingle();
}
公共无效读取值(T val)
{
抛出新异常(“未实现”);
}
}

虽然有些人反对将泛型用于您描述的事情,但有时它们是合适的。尽管如此,对于让不同类型的代码执行,并没有一种特别方便的模式。最好的方法可能是让您的
私有静态操作readProc=InitReadProc并具有通用的
ReadValue(T val)
调用
readProc(val)
InitReadProc
方法可能类似于:

ReadFile<float>.readProc = readValue; // Binds to `float` overload
ReadFile<double>.readProc = readValue; // Binds to `double` overload
// etc. for all known overloads
if (readProc == InitReadProc) // The readProc of *this* type (T) didn't get set above
  throw new NotSupportedException(String.Format("Type {0} not available", typeof(T));
else
  readProc();
ReadFile.readProc=readValue;//绑定到“float”重载
ReadFile.readProc=readValue;//绑定到'double'重载
//对于所有已知的重载
if(readProc==InitReadProc)//上面没有设置*this*类型(T)的readProc
抛出新的NotSupportedException(String.Format(“Type{0}不可用”,typeof(T));
其他的
readProc();

使用这种方法,第一次尝试调用任何类型的泛型
readValue
,它将填充所有已知类型的
readProc
委托。以后尝试调用任何此类类型的
readValue
都将通过委托发送到相应的方法。请注意,如果需要,可以使用ld提供一个
ConfigureReadProc(Action proc)
方法,允许类的使用者为其他类型设置
ReadFile
方法。如果需要,还可以使用反射来检查type
T
是否支持静态
readValue(T)
方法,如果是,则将委托附加到该方法。

虽然有些人反对在您描述的事情上使用泛型,但有时它们是合适的。尽管如此,没有一种特别方便的模式可以让不同的代码针对不同的类型执行。也许最好的方法是让您的de>private static Action readProc=InitReadProc;
并具有通用的
ReadValue(T val)
调用
readProc(val)
InitReadProc
方法可能类似于:

ReadFile<float>.readProc = readValue; // Binds to `float` overload
ReadFile<double>.readProc = readValue; // Binds to `double` overload
// etc. for all known overloads
if (readProc == InitReadProc) // The readProc of *this* type (T) didn't get set above
  throw new NotSupportedException(String.Format("Type {0} not available", typeof(T));
else
  readProc();
ReadFile.readProc=readValue;//绑定到'float'重载
ReadFile.readProc=readValue;//绑定到'double'重载
//对于所有已知的重载
if(readProc==InitReadProc)//上面没有设置*this*类型(T)的readProc
抛出新的NotSupportedException(String.Format(“Type{0}不可用”,typeof(T));
其他的
readProc();

使用这种方法,第一次尝试调用任何类型的泛型
readValue
,它将填充所有已知类型的
readProc
委托。以后尝试调用任何此类类型的
readValue
都将通过委托发送到相应的方法。请注意,如果需要,可以使用ld提供一个
ConfigureReadProc(Action proc)
方法,允许类的使用者为其他类型设置
ReadFile
方法。如果需要,还可以使用反射来检查type
T
是否支持静态
readValue(T)
方法,如果是,则将委托附加到该方法。

您需要模板化readValue方法,而不是模板化该类。然后,您可以使用良好的ol'型重载来实现显式类型。别忘了将
out
关键字添加到您的readValue参数中。快速控制台应用程序演示:

class Program
{
    static void Main(string[] args)
    {
        var rf = new ReadFile();

        double d;
        float f;
        int i;

        Console.WriteLine(string.Format( "{1}: {0}", rf.readValue(out d), d ));
        Console.WriteLine(string.Format( "{1}: {0}", rf.readValue(out f), f ));
        // note you don't have to explicitly specify the type for T
        // it is inferred
        Console.WriteLine(string.Format( "{1}: {0}", rf.readValue(out i), i ));

        Console.ReadLine();
    }
}

public class ReadFile
{
    // overload for double
    public string readValue(out double val)
    {
        val = 1.23;
        return "double";
    }

    // overload for float
    public string readValue(out float val)
    {
        val = 0.12f;
        return "float";
    }

    // 'catch-all' generic method
    public string readValue<T>(out T val)
    {
        val = default(T);
        return string.Format("Generic method called with type {0}", typeof(T));
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var rf=new ReadFile();
双d;
浮动f;
int i;
WriteLine(string.Format(“{1}:{0}”,rf.readValue(out d),d));
WriteLine(string.Format(“{1}:{0}”,rf.readValue(out f),f));
//注意,您不必显式指定t的类型
//据推断
WriteLine(string.Format(“{1}:{0}”,rf.readValue(out i),i));
Console.ReadLine();
}
}
公共类读取文件
{
//双倍超载
公共字符串读取值(输出双值)
{
val=1.23;
返回“双”;
}
//浮子过载
公共字符串readValue(out float val)
{
val=0.12f;
返回“浮动”;
}
//“一网打尽”泛型方法
公共字符串读取值(out T val)
{
val=默认值(T);
返回string.Format(“使用类型{0},typeof(T)调用的泛型方法”);
}
}

您需要对readValue方法进行模板化,而不是对类进行模板化。然后,您可以使用良好的ol风格重载来实现显式类型。不要忘记将
out
关键字添加到您的readValue参数中。快速控制台应用程序演示:

class Program
{
    static void Main(string[] args)
    {
        var rf = new ReadFile();

        double d;
        float f;
        int i;

        Console.WriteLine(string.Format( "{1}: {0}", rf.readValue(out d), d ));
        Console.WriteLine(string.Format( "{1}: {0}", rf.readValue(out f), f ));
        // note you don't have to explicitly specify the type for T
        // it is inferred
        Console.WriteLine(string.Format( "{1}: {0}", rf.readValue(out i), i ));

        Console.ReadLine();
    }
}

public class ReadFile
{
    // overload for double
    public string readValue(out double val)
    {
        val = 1.23;
        return "double";
    }

    // overload for float
    public string readValue(out float val)
    {
        val = 0.12f;
        return "float";
    }

    // 'catch-all' generic method
    public string readValue<T>(out T val)
    {
        val = default(T);
        return string.Format("Generic method called with type {0}", typeof(T));
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var rf=new ReadFile();
双d;
浮动f;
int i;
WriteLine(string.Format(“{1}:{0}”,rf.readValue(out d),d));
WriteLine(string.Format(“{1}:{0}”,rf.readValue(out f),f));
//注意,您不必显式指定t的类型
//据推断
WriteLine(string.Format(“{1}:{0}”,rf.readValue(out i),i));
Console.ReadLine();
}
}
公共类读取文件
{
//双倍超载
公共字符串读取值(输出双值)
{
val=1.23;
返回“双”;
}
//浮子过载
公共字符串readValu