处理不支持的类型 [英] Handling Unsupported Types

查看:131
本文介绍了处理不支持的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



var data = new Data [] {1,string,true,// ..} //在这种情况下,假设字符串和布尔是支持的类型,但是整数不是。



当我存储数据集时,如果语句存储数据,它将运行一系列

  if (数据为字符串)StoreData(数据为字符串); 
else if(data is boolean)StoreData(data as boolean);
// ..

由于整数不受支持,我希望我的代码能够处理它是否适当(无论是日志记录等)。但是,我仍然希望能够存储第二,第三等支持的类型。



更新:我希望代码继续运行,即使发现不受支持的类型,因为可能存在后续支持的类型。 / p>

如何处理不受支持的类型,然后存储后续支持的类型?

解决方案

我将使用隐式运算符将支持的类型转换为Data类。为任何支持的数据类型添加隐式运算符,不支持的操作将无法编译。



http://www.codeproject.com/Articles/15191/Understanding-Implicit-Operator-Overloading-in-C

  public class Data 
{
private object UnderlyingValue;

私有数据(对象底层值)
{
this.UnderlyingValue = underlyingValue;
}

public static implicit operator数据(字符串值)
{
return new Data(value);
}

public static implicit operator Data(bool value)
{
return new Data(value);
}
}

var data = new Data [] {1,string,true}; //编译器错误1,因为没有提供支持的转换方法

正如Jon所说,要求有点模糊,所以我不知道还要补充什么。根据您所需要的可能,您可以使用通用版本的数据,或者您之后如何管理数据对象可能会有所不同。您还可以通过一些工厂来检查支持的类型并抛出格式很好的异常。



编辑:根据您的编辑,您可以执行以下操作:

  public class SupportedObjectsResults 
{
public List< object> SupportedObjects {get;私人集}
public List< object> UnsupportedObjects {get;私人集

public SupportedObjectsResults(List< object> supportedObjects,List< object> unsupportedObjects)
{
this.SupportedObjects = supportedObjects;
this.UnsupportedObjects = unsupportedObjects;
}
}

public static class SupportedTypeHelper
{
public static SupportedObjectsResults GetSupportedTypes(params object [] values)
{
列表< object> supportedObjects = new List< object>();
列表< object> unsupportedObjects = new List< object>();

foreach(值中的对象值)
{
if(CheckIsSupported(value))
supportedObjects.Add(value);
else
unsupportedObjects.Add(value);
}

返回新的SupportedObjectsResults(supportedObjects,unsupportedObjects);
}

private static bool CheckIsSupported(object underlyingValue)
{
return(underlyingValue is string ||
underlyingValue is bool
);
}
}

然后,您可以找出支持哪些对象,哪些不是:

  var supportedResults = SupportedTypeHelper.GetSupportedTypes(1,string,true); 
//supportedResults.SupportedObjects = [string,true]
//supportedREsults.UnsupportedObjects = [1];

就像我一样,我不喜欢第二个解决方案;大量的拳击和编译时没有检查。无论如何,我想这应该给您一个很好的开始,以解决您的具体设计问题。


I am creating an array of data:

var data = new Data[] { 1, "string", true, //.. }; //In this case, assume that strings and booleans are supported types, but integers are not.

When I store the set of data, it runs through a series of if statements to store data:

if (data is string) StoreData(data as string);
else if (data is boolean) StoreData(data as boolean);
//..

Because integers are not supported, I would like my code to handle it appropriately (whether it would be logging, etc.). However, I want to still be able to store the second, third, etc. supported types.

UPDATE: I would like the code to continue running even after an unsupported type is found as there may be subsequent supported types to store.

How would I handle an unsupported type, then store the subsequent supported types?

解决方案

I would use implicit operators to convert supported types to your Data class. Add implicit operators for whatever data type is supported and the ones that are not supported will not compile.

http://www.codeproject.com/Articles/15191/Understanding-Implicit-Operator-Overloading-in-C

public class Data
{
    private object UnderlyingValue;

    private Data(object underlyingValue)
    {
        this.UnderlyingValue = underlyingValue;
    }

    public static implicit operator Data(string value)
    {
        return new Data(value);
    }

    public static implicit operator Data(bool value)
    {
        return new Data(value);
    }
}

var data = new Data[] { 1, "string", true }; //compiler error on "1" because no supporting conversion method is provided

As Jon stated though, your requirements are a bit vague so I don't know what else to add to this. Depending on what you need perhaps you could use a generic version of Data, or how you want to manage the Data objects afterwards might be different. You could also run through some factory to check for supported types and throw nicely formatted exceptions.

EDIT: based on your edits, you could do something like this:

public class SupportedObjectsResults
{
    public List<object> SupportedObjects { get; private set; }
    public List<object> UnsupportedObjects { get; private set; }

    public SupportedObjectsResults(List<object> supportedObjects, List<object> unsupportedObjects)
    {
        this.SupportedObjects = supportedObjects;
        this.UnsupportedObjects = unsupportedObjects;
    }
}

public static class SupportedTypeHelper
{
    public static SupportedObjectsResults GetSupportedTypes(params object[] values)
    {
        List<object> supportedObjects = new List<object>();
        List<object> unsupportedObjects = new List<object>();

        foreach(object value in values)
        {
            if (CheckIsSupported(value))
                supportedObjects.Add(value);
            else
                unsupportedObjects.Add(value);
        }

        return new SupportedObjectsResults(supportedObjects, unsupportedObjects);
    }

    private static bool CheckIsSupported(object underlyingValue)
    {
        return (underlyingValue is string ||
                underlyingValue is bool
                );
    }
}

Then you can find out which objects were supported and which weren't:

var supportedResults = SupportedTypeHelper.GetSupportedTypes(1, "string", true);
//supportedResults.SupportedObjects = ["string", true]
//supportedREsults.UnsupportedObjects = [1];

Just as an aside, I don't like the second solution much; plenty of boxing and no checking at compile time. At any rate, I guess this should give you a good start to solving your specific design issues.

这篇关于处理不支持的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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