尝试序列化对象时递归过多

时间:2017-04-12 08:46:18

标签: c# recursion serialization yaml

我目前正在测试我的应用程序中YML的使用情况,以便为用户编辑和配置电子邮件下载和导入提供更友好的方法。

出于测试目的,我想看看我的模拟YML是否大致相当于应用程序的推出。

所以,为了测试我有这个代码块,它永远不会抛出太多的递归"。

// TEST SERIALIZE DATA
        {
            var _testDomain = default(Domain);
            var _testSecureString = new SecureString();

            "testP455w0rd".ToCharArray().ToList().ForEach(_testSecureString.AppendChar);

            var _testConfig = new EmailBeamConfig() {
                DefaultProtocol = ProtocolType.Imap,
                Domains = new List<Domain> {
                    (_testDomain = new Domain {
                        Accounts = new List<EmailAccount> {
                            new EmailAccount {
                                Domain = _testDomain.DomainName,
                                InternalUid = "de.test_a_test-account",
                                Password = _testSecureString,
                                Username = "test-account@test.de"
                            },
                            new EmailAccount {
                                Domain = _testDomain.DomainName,
                                InternalUid = "de.test_a_test-account2",
                                Password = _testSecureString,
                                Username = "test-account2@test.de"
                            }
                        },
                        DomainName = "de.test",
                        EmailServer = "192.168.252.172",
                        UseEncryption = true
                    })
                },
                DownloadInterval = new TimeSpan(0, 10, 0),
                DownloadRoot = mainSystemConfig.GetString("importRootDir")
            };

            var _testSerializer = new SerializerBuilder()
                .WithNamingConvention(new UnderscoredNamingConvention())
                .Build();
            var _testSerializedData = _testSerializer.Serialize(_testConfig);

            File.WriteAllText(cfgFile.FullName, _testSerializedData);
        }

以下是EmailBeamConfig的来源:

internal class EmailBeamConfig {

    private string _downloadRoot;

    /// <summary>
    /// Gets or sets the download root dir.
    /// </summary>
    public string DownloadRoot {
        get => _downloadRoot;
        set {
            _downloadRoot = value;
            DownloadRootObj = new DirectoryInfo(value);
        }
    }

    /// <summary>
    /// Gets an object representing the download root.
    /// </summary>
    public DirectoryInfo DownloadRootObj { get; private set; }

    /// <summary>
    /// Gets the interval for downloading e-mails.
    /// </summary>
    public TimeSpan DownloadInterval { get; set; }

    /// <summary>
    /// Gets or sets the default protocol to be used,
    /// if a protocol is not defined for an individual user.
    /// </summary>
    public ProtocolType DefaultProtocol { get; set; }

    public List<Domain> Domains { get; set; }

}

和EmailAccount:

/// <summary>
/// Defines an e-mail account for use in this plugin.
/// </summary>
internal struct EmailAccount {

    private string _internalUid;

    /// <summary>
    /// Gets or sets the username for this e-mail account.
    /// (Used for logging in, etc)
    /// </summary>
    internal string Username { get; set; }

    /// <summary>
    /// gets or sets the password for this account.
    /// </summary>
    internal SecureString Password { get; set; }

    /// <summary>
    /// Gets or sets the internal user identifier for this user account.
    /// </summary>
    internal string InternalUid {
        get => _internalUid;
        set {
            if (value == "/" || value.Length < 10) {
                _internalUid = $"{ Domain }_a_{ Username?.Split('@')[0] }";
            } else _internalUid = value;
        }
    }

    /// <summary>
    /// Gets or sets the domain this e-mail account is associated with.
    /// </summary>
    internal string Domain { get; set; }

}

我完全忘记了此时无限递归的任何来源。

编辑:以下是域结构声明:

internal struct Domain {

    /// <summary>
    /// Gets or sets the name of this domain.
    /// </summary>
    internal string DomainName { get; set; }

    /// <summary>
    /// Gets or sets the URL/IP address of the e-mail server.
    /// </summary>
    internal string EmailServer { get; set; }

    /// <summary>
    /// Gets or sets a value determining whether to attempt to use an encrypted connection to 
    /// the e-mail server.
    /// </summary>
    internal bool UseEncryption { get; set; }

    /// <summary>
    /// Gets the list of accounts associated with this domain
    /// </summary>
    internal List<EmailAccount> Accounts { get; set; } 

}

0 个答案:

没有答案