退出功能块后,内部对象列表再次变为空

问题描述 投票:-1回答:1

我有一个对象'ForValidation',该对象具有int列表作为属性,还有一个对象“ Validator”,它具有一个Verify(IEnumerable ForValidation)方法。验证方法在ForValidation列表属性中添加数字。

在主函数中,我具有IEnumerable的Validator和IEnumerable的ForValidation

每次Verify(IEnumerable)退出时,ForValidation中的列表都返回为0计数。

据我了解,对象是C#中的引用类型,任何地方的修改都应反映在同一对象中。

[我尝试逐行运行Visual Studio调试器以检查'ForValidation'中的列表实际上是否已添加数据,然后在Verify方法后消失。

    public class ForValidation
    {
        private readonly object @lock = new object();

        private readonly List<int> ExistenceChecks = new List<int>();

        public IEnumerable<int> ExistsPlaces => ExistenceChecks;

        public string CheckProperty { get; }

        public ForValidation(string checkProperty )
        {
            CheckProperty = checkProperty ;
        }

        public void ConfirmExistence(int place)
        {
            lock (@lock)
            {
                ExistenceChecks.Add(place);
            }
        }
    }
    public class Validator 
    {

        public int ValidatorNumber { get; }

        private readonly Datasource somedatasource;

        public Validator(int number, Datasource someds)
        {
            ValidatorNumber = number;
            somedatasource = someds;
        }

        public void Verify(IEnumerable<ForValidation> forValidations)
        {
            ForValidation[] copy = forValidations.ToArray();

            IEnumerable<string> checkProperties = from member in copy 
                                                  select member.CheckProperty;

            IEnumerable<CompareAgainst> existingMembers 
              = somedatasource.Filter(new CheckPropertiesFilter(checkProperties)).Execute();

            foreach (ForValidation forValidation in copy)
            {
                if (existingMembers.FirstOrDefault(m => m.CheckProperty == forValidation.CheckProperty) != null)
                {
                    forValidation.ConfirmExistence(ValidatorNumber);
                }
            }

            int x = copy.Length;
            //each forValidation.ExistsPlaces has items until this code block
        }
    }
    main
    {
        private readonly IEnumerable<ForValidation> forValidations {...}

        private readonly IEnumerable<Validator> validators {...}

        foreach (Validator validator in validators)
        {
            validator.Verify(forValidations); 
            // each forValidation.ExistsPlaces count is 0 again in this block
        }
    }

IEValpect forValidations中的每个ForValidation项将在验证器的每个Verify方法之后记住其IEnumerable ExistsPlaces属性中的项,但在foreach循环中,Verify方法的每次迭代后,它变为0计数

c# list iteration ienumerable
1个回答
0
投票

我无法重现您的问题。这是我的代码。

public static void Main(string[] args)
{
    var validators = new[] { new Validator(666), new Validator(667) };
    var forValidations = new [] { new ForValidation("v1"), new ForValidation("v2") };

    Console.WriteLine("Before Verify");
    foreach (var fv in forValidations)
        Console.WriteLine($"Object: {fv.CheckProperty} - count of places: {fv.ExistsPlaces.Count()}");

    foreach (Validator validator in validators)
        validator.Verify(forValidations);

    Console.WriteLine("After Verify");
    foreach (var fv in forValidations)
        Console.WriteLine($"Object: {fv.CheckProperty} - count of places: {fv.ExistsPlaces.Count()}");
}

结果

Before Verify
Object: v1 - count of places: 0
Object: v2 - count of places: 0
After Verify
Object: v1 - count of places: 2
Object: v2 - count of places: 2

班级:

public class ForValidation
{
    private readonly object @lock = new object();

    private readonly List<int> ExistenceChecks = new List<int>();

    public IEnumerable<int> ExistsPlaces => ExistenceChecks;

    public string CheckProperty { get; }

    public ForValidation(string checkProperty)
    {
        CheckProperty = checkProperty;
    }

    public void ConfirmExistence(int place)
    {
        lock (@lock)
        {
            ExistenceChecks.Add(place);
        }
    }
}

public class Validator
{
    public Validator(int validatorNumber)
    {
        ValidatorNumber = validatorNumber;
    }

    public int ValidatorNumber { get; }


    public void Verify(IEnumerable<ForValidation> forValidations)
    {
        ForValidation[] copy = forValidations.ToArray();

        IEnumerable<string> checkProperties = from member in copy
                                              select member.CheckProperty;

        foreach (ForValidation forValidation in copy)
        {
            //if (existingMembers.FirstOrDefault(m => m.CheckProperty == forValidation.CheckProperty) != null)
            {
                forValidation.ConfirmExistence(ValidatorNumber);
            }
        }

        int x = copy.Length;
        //each forValidation.ExistsPlaces has items until this code block
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.