-2

This task is trivial in Javascript but in C#, I can't find a way to do it.

I have an anonymous type containing nested anonymous types. These child types contain the property rootLength that should ultimately be set to the value of the parent like a Global if you will.

How do I set the value of children based on the value of parent?

var opensslSettings = new 
{
    rootLength = 2048,
    pkcs3 = new 
    {
        rootLength = 2048,
        outDir = AppDomain.CurrentDomain.BaseDirectory,
        outDhp = "www_example_com_2048bit.pkcs3"
    },
    pkcs10 = new 
    {
        rootLength = 2048,
        outDir = AppDomain.CurrentDomain.BaseDirectory,
        outKeyFile = "www_example_com_2048bit.pkcs8",
        outCsrFile = "www_example_com_2048bit.pkcs10"
    },
};

Console.WriteLine(opensslSettings.ToString());
suchislife
  • 4,251
  • 10
  • 47
  • 78
  • Do you mean you want to do `opensslSettings.pkcs10.rootLength = opensslSettings.rootLength;`? – Sweeper Dec 26 '21 at 16:01
  • Correct. Set the children `rootLength` to the value of the `rootLength` sitting outside of them. Kinda like `this.rootLength`. Perhaps `this` in C# doesn't work that way. – suchislife Dec 26 '21 at 16:02
  • I can't use `this` on anything `static` and I can't use `rootLength = opensslSettings.rootLength` on the children while the object is being constructed. – suchislife Dec 26 '21 at 16:07
  • You can create a separate variable rootLength and assign its value to any part of anonymous type. I think, it's the only way. – Yevhen Cherkes Dec 26 '21 at 16:08
  • [Anonymous types' properties are not mutable](https://stackoverflow.com/questions/14794010/create-anonymous-type-with-mutable-fields). How about creating a constant that is equal to 2048? – Sweeper Dec 26 '21 at 16:08
  • Yeah. That seemed like the solution all things considered. Thanks for all the feedback! – suchislife Dec 26 '21 at 16:10

1 Answers1

-1

Below, the working example for completion.

using System.Diagnostics;
using System.Text.Json.Nodes;

namespace asynchronousCode
{
    class Program
    {
        public async static Task Main(string[] args)
        {
            var rootLength = 2048;

            var opensslSettingsJson = new JsonObject
            {
                ["rootLength"] = rootLength,
                ["pkcs3"] = new JsonObject 
                {
                    ["rootLength"] = rootLength,
                    ["outDir"] = AppDomain.CurrentDomain.BaseDirectory,
                    ["outDhp"] = $"www_example_com_{rootLength}bit.pkcs3"
                },
                ["pkcs10"] = new JsonObject
                {
                    ["rootLength"] = rootLength,
                    ["outDir"] = AppDomain.CurrentDomain.BaseDirectory,
                    ["outKeyFile"] = $"www_example_com_{rootLength}bit.pkcs8",
                    ["outCsrFile"] = $"www_example_com_{rootLength}bit.pkcs10"
                },
            };

            Console.WriteLine(opensslSettingsJson.ToJsonString() + Environment.NewLine);

            var opensslSettingsObject = new 
            {
                rootLength = rootLength,
                pkcs3 = new 
                {
                    rootLength = rootLength,
                    outDir = AppDomain.CurrentDomain.BaseDirectory,
                    outDhp = $"www_example_com_{rootLength}bit.pkcs3"
                },
                pkcs10 = new 
                {
                    rootLength = rootLength,
                    outDir = AppDomain.CurrentDomain.BaseDirectory,
                    outKeyFile = $"www_example_com_{rootLength}bit.pkcs8",
                    outCsrFile = $"www_example_com_{rootLength}bit.pkcs10"
                },
            };
        }
    }
}
suchislife
  • 4,251
  • 10
  • 47
  • 78