0

Please is possible optimise this code, with dictionary? Or otherwise.

if (node.Type.Equals(NodeType.CONTAINER))
{
    DataUtils.ContainerToData((INode<ContainerValue>) node, data);
}
else if (node.Type.Equals(NodeType.TEXT))
{
    DataUtils.TextToData((INode<TextValue>) node, data);
}

I have 15 this conditions.

I thought that dictionary will work, but how make casting? (INode<pair.Value>) node. I looking somethink solution like is this:

var dictionary = new Dictionary<string, Type>();
dictionary.Add(NodeType.CONTAINER, tyeof(ContainerValue));
dictionary.Add(NodeType.TEXT, tyeof(TextValue));

foreach (var pair in dictionary)
{
    if(node.Type.Equals(pair.Key))
    {
        // wrong
        DataUtils.ContainerToData((INode<pair.Value>) node, data);
    }
}
Abedron
  • 744
  • 1
  • 6
  • 20
  • "but how make retyping?" - Sorry, didn't understand you. – Anu Viswan Feb 07 '19 at 15:51
  • what is the type of `data` ? – Franck Feb 07 '19 at 15:52
  • retype = casting :) – Abedron Feb 07 '19 at 15:55
  • Your first snippet contains `DataUtils.TextToData`, is that correct? – Vlad Feb 07 '19 at 15:55
  • details for data or node are not importat. It is are objects – Abedron Feb 07 '19 at 15:56
  • ``DataUtils.TextToData`` is correct but text is not string. is it not important – Abedron Feb 07 '19 at 15:59
  • @Abedron We need some definition of `Node`, `INode`, `ContainerValue` and `TextValue` to know the relation. At least the structure. And is `NodeType.CONTAINER` a struct of an actual `Type` or is it an override of `Type` in the class. It's confusing. – Franck Feb 07 '19 at 16:00
  • I think you want something like [Pass An Instantiated System.Type as a Type Parameter for a Generic Class](https://stackoverflow.com/q/266115/215552) but the question is quite unclear. – Heretic Monkey Feb 07 '19 at 16:07
  • Considering the solutions in that question use reflection, you might be better off with your original solution... – Heretic Monkey Feb 07 '19 at 16:09
  • Are you actually bothered about performance? (Your question asks about optimization.) I'd be surprised if this was actually a bottleneck. – Jon Skeet Feb 07 '19 at 16:41
  • But why can't you just use single method in the `DataUtil` like `ToData` which would have multiple overloads for all types you need to handle and then use some generic to cast ? – Dmytro Mukalov Feb 07 '19 at 18:06

1 Answers1

0

I think that you can use a switch stament:

csharp-pattern-matching-7-0

                switch (node)
                {
                    case INode<ContainerValue> containerValue:
                        DataUtils.ContainerToData(containerValue, data);
                        break;
                    case INode<TextValue> textValue:
                        DataUtils.ContainerToData(textValue, data);
                        break;

                }

Hope this help.

  • Thanks, but I need as first check ``node.Type.Equals`` and rewrite conditions to ``foreach`` cycle. – Abedron Feb 08 '19 at 11:10