简体   繁体   English

从嵌套的子属性中获取父匿名类型属性的值。 如何?

[英]Get value of parent anonymous type property from within nested child properties. How?

This task is trivial in Javascript but in C#, I can't find a way to do it.这个任务在 Javascript 中是微不足道的,但在 C# 中,我找不到办法。

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.这些子类型包含属性rootLength ,如果您愿意,该属性最终应该设置为父对象的值,例如 Global。

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());

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"
                },
            };
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM