jac*_*eon 5 overloading c#-4.0
我需要能够调用一个方法并传入一个未知类型的对象,但然后调用正确的重载.我还需要一个接受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");
}
}
Run Code Online (Sandbox Code Playgroud)
好的,到目前为止一切顺利.现在我希望能够做到这一点:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
结果FancyStringBuilder.BuildTheString()是"hello world 2012-12-21 00:00:00.000",当我预料到的时候"hello world 20121221"
问题是接受a的重载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;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这确实假设您的对象在创建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();
}
}
Run Code Online (Sandbox Code Playgroud)
这样,调用代码如下所示:
Console.WriteLine(
"{0} {1}",
Formatter.Format(DateTime.Now),
Formatter.Format("banana"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3394 次 |
| 最近记录: |