Handling Unsupported Types

∥☆過路亽.° 提交于 2019-12-13 00:13:03

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/10622694/handling-unsupported-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!