我需要能够调用方法并传递未知类型的对象
但随后调用了正确的重载。我还需要一个默认的实现,它接受object
作为其参数类型。我看到的是,默认重载是有史以来唯一使用过的重载。
这是我要执行的操作的要点:
class Formatter
{
private object Value;
public Formatter(object val){
Value = val;
}
public override string ToString()
{
return Format(Value);
}
private string Format(object value)
{
return value.ToString();
}
private string Format(DateTime value)
{
return value.ToString("yyyyMMdd");
}
}
好的,到目前为止很好。现在,我希望能够做到这一点:
public static class FancyStringBuilder()
{
public static string BuildTheString()
{
var stringFormatter = new Formatter("hello world");
var dateFormatter = new Formatter(DateTime.Now);
return String.Format("{0} {1}", stringFormatter, dateFormatter);
}
}
当我期望
FancyStringBuilder.BuildTheString()
时,"hello world 2012-12-21 00:00:00.000"
的结果是"hello world 20121221"
。问题是没有调用接受
DateTime
的重载,而是默认为接受object
的重载。 如何在不诉诸凌乱的switch语句的情况下调用适当的方法? 最佳答案
在Formatter.ToString()
中,覆盖Formatter.Format(object)
总是被称为的。这是因为重载解析发生在编译时,而不是运行时。在编译时,有关Value
的唯一已知信息是它是一个对象。
如果您真的想区分传入类型,则需要在Formatter
的构造函数中进行区分。在这种情况下,您可以立即调用ToString()
并仅存储格式化的结果,而不必卡在对象上:
class Formatter
{
string formattedValue;
public Formatter(object value)
{
formattedValue = value.ToString();
}
public Formatter(DateTime value)
{
formattedValue = value.ToString("yyyyMMdd");
}
public string ToString()
{
return formattedValue;
}
}
请注意,这确实假设您的对象在创建
Formatter
对象和调用Formatter.ToString()
之间没有变化,或者至少可以在创建Formatter
时对字符串表示形式进行快照。这也假设您在编译时知道传入的类型。如果您想要真正的仅运行时解决方案,则必须使用“is”运算符或
typeof()
比较。如果您的目标只是提供基于传入类型的自定义ToString()格式,那么我可能会使用从类型映射到格式字符串的列表来做到这一点:
static class Formatter
{
private static List<Tuple<Type, string>> Formats;
static Formatter()
{
Formats = new List<Tuple<Type, string>>();
// Add formats from most-specific to least-specific type.
// The format string from the first type found that matches
// the incoming object (see Format()) will be used.
AddMapping(typeof(DateTime), "yyyyMMdd");
// AddMapping(typeof(...), "...");
}
private static void AddMapping(Type type, string format)
{
Formats.Add(new Tuple<Type, string>(type, format));
}
public static string Format(object value)
{
foreach (var t in Formats)
{
// If we find a type that 'value' can be assigned to
// (either the same type, a base type, or an interface),
// consider it a match, and use the format string.
if (t.Item1.IsAssignableFrom(value.GetType()))
{
return string.Format(t.Item2, value);
}
}
// If we didn't find anything, use the default ToString()...
return value.ToString();
}
}
这样,调用代码将如下所示:
Console.WriteLine(
"{0} {1}",
Formatter.Format(DateTime.Now),
Formatter.Format("banana"));
关于c#-4.0 - 重载接受 `object`作为默认参数类型的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13962582/