0

I have 2 classes that look exactly the same but reside in different namespaces. One property of the nested class is an array of itself, which allows property nesting/recursion (sort of like a command pattern)

I am trying to convert/cast the class from one namespace to the class in another namespace. I have the below code:

    namespace Common.Class
    {
        public class Root
        {
            public string Key { get; set; }
            public Child[] Children { get; set; }
            public class Child
            {
                public string Content { get; set; }
                public Child[] RecursionChild { get; set; }
            }
        }
    }
    namespace Uncommon.Class
    {
        class Root
        {
            public string Key { get; set; }
            public Child[] Children { get; set; }
            public class Child
            {
                public string Content { get; set; }
                public Child RecursionChild { get; set; }
            }
        }
    }

The main program

        static void Main(string[] args)
        {
            var commonRoot = new Common.Class.Root
            {
                Key = "1234-lkij-125l-123o-123s",
                Children = new Common.Class.Root.Child[]
                {
                    new Common.Class.Root.Child
                    {
                        Content = "Level 1 content",
                        RecursionChild = new Common.Class.Root.Child[] { }
                    }
                }
            };


            var uncommonRoot = new Uncommon.Class.Root
            {
                Key = commonRoot.Key,
                Children = commonRoot.Children // here I get error: Cannot implicitly convert type 'Common.Class.Root.Child[]' to 'Uncommon.Class.Root.Child[]'
            };
        }
1
  • Just something that popped into my head, but perhaps a good strategy would be to try convert it to a more universal JsonObject or a string before doing a cast to the class in another namespace. Commented Jul 27, 2021 at 22:05

1 Answer 1

3

You need to convert the children too.

Because you've got that recursive child, you can't pull this off with just anonymous functions, because the function has to be able to call itself, and you need a name for that. So we need to introduce a local function with a real name, e.g. Converter.

Uncommon.Root.Child Converter(Common.Root.Child source) => new Uncommon.Root.Child
{
    Content = source.Content, 
    RecursiveChild = Converter(source.ResursiveChild) 
};

var uncommonRoot = new Uncommon.Class.Root
{
    Key = commonRoot.Key,
    Children = commonRoot.Children.Select(Converter).ToArray();
};
Sign up to request clarification or add additional context in comments.

2 Comments

sorry I need to edit the question, because Uncommon.Class.Root also has Child[] RecursionChild. I missed it when typing
Just need another Select statement that uses the Converter. Can you see where it would go?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.