I have a basic filter class that stores a string
parameter name and a generic T
value. The filter has a method Write(writer As IWriter)
which will write the contents of the filter to an HTML page. The Write
method has two overloads, one which takes two strings and which takes a string and an object. This lets me auto-quote strings.
The problem is, when I call writer.Write(ParameterName, Value)
and T
is a string
, it calls the overload for string/object, not string/string! If I call the Write method on the writer
directly, it works as expected.
Here's an SSCE in C#. I tested this in both VB and C# and found the same problem
void Main() {
FooWriter writer = new FooWriter();
Filter<string> f = new Filter<string>() {ParameterName = "param", Value = "value"};
f.Write(writer); //Outputs wrote w/ object
writer.Write(f.ParameterName, f.Value); //Outputs wrote w/ string
}
class FooWriter {
public void Write(string name, object value) {
Console.WriteLine("wrote w/ object");
}
public void Write(string name, string value) {
Console.WriteLine("wrote w/ string");
}
}
class Filter<T> {
public string ParameterName {get; set;}
public T Value {get; set;}
public void Write(FooWriter writer) {
writer.Write(ParameterName, Value);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…