将字符串转换为未知的基本类型(最好的猜测) [英] Converting string to unknown primitive type (best guess)

查看:302
本文介绍了将字符串转换为未知的基本类型(最好的猜测)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,这是不是你想这样做,除非你是在无奈的情况下,但没有人有这样的一个很好的例子(加分更清晰的方法名思维):

Obviously this isn't something you would want to do unless you're in a helpless situation, but does anyone have a good example of something like this (bonus points for thinking of a clearer method name):

public static object ConvertToBestGuessPrimitive(string toConvert)
{
    if(looksLikeAnInt)
    {
        return as an int;
    }
    else if(looksLikeABoolean)
    {
        return as a boolean;
    }
    else
    {
        return as a string;
    }
}

我唯一的想法是链在一起了一堆的TryParse方法一起在一个合理的秩序,回落到字符串,如果没有什么作品。谁能想到更好的东西?这个问题有可能已经被回答的地方,但我似乎无法找到任何有用的结果与使用的过于通用的搜索条件。

The only idea I had was to chain together a bunch of TryParse methods together in a sensible order and fall back to string if nothing works. Can anyone think of something better? This question has probably already been answered somewhere, but I can't seem to find any useful results with the overly generic search terms being used.

编辑 - 因为有一直是多么有用,这将是没有一个例子一些批评,这里有一个我想出了以满足具体的需求(但令人难以置信的)......说我分析,我从得到一个错误日志文件不受控制的来源。日志文件中有规定发生错误的方法名和参数。我想自动化测试通过寻找最佳匹配的方法和参数,并试图再次打电话给他们的错误条件。也许一个愚蠢的例子(请不要试图拿出但你可以做到这一点在这种情况下反应,因为这只是一个例子),但它说明几点:1)输入超出我的控制。 2)最好的猜测是基于一些标准,找到一个合适的匹配。例如:一个2012年10月2日是更可能意味着一个DateTime不是字符串

Edit - Since there's been some criticism of how useful this would be without an example, here's one I came up with to satisfy a concrete need (however implausible)...Say I am parsing an error log file that I get from an uncontrolled source. The log file has method names and arguments that were provided for where the error occurred. I want to automate testing the error conditions by finding the best matched method and arguments and attempting to call them again. Maybe a stupid example (please don't try and come up with "but you could do this in this scenario" responses since this is just an example), but it illustrates a few points: 1) The input is out of my control. 2) The best-guess is based on some criteria for finding a suitable match. For example: a "10/2/2012" is more likely to mean a DateTime than a string.

推荐答案

所以,你想摆脱IF-别人的?什么是这样的:

So you're looking to get rid of if-elses? What about something like this:

interface IConverter
{
  bool TryConvert(string obj, out object result);
}

class IntConvert : IConverter
{
  public bool TryConvert(string obj, out object result) { /* stuff here */ }
}

class BoolConverter : IConverter {...}

// etc.
List<IConverter> converters = new List<IConverter>();
converters.Add(new IntConvert());
converters.Add(new BoolConvert());

public static object ConvertToBestGuessPrimitive(string toConvert)
{
  object obj;
  foreach(var converter in converters) {
    if(converter.TryConvert(toConvert, out obj))
       return obj;
  }

  return null;
}

更新:感谢Servy您的建议

UPDATE: Thanks Servy for the suggestion.

这篇关于将字符串转换为未知的基本类型(最好的猜测)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆