C# 保存从列表中拾取的项目

C# 保存从列表中拾取的项目,c#,list,function,C#,List,Function,主要类别: public class Main { private string param; public Main(string param) { this.param = param; } public List<Foo> Foos { get {return GetFoos();} // add functionality of saving Foo (single item, not the whole

主要类别:

public class Main 
{
    private string param;
    public Main(string param) { this.param = param; }

    public List<Foo> Foos
    {
        get {return GetFoos();}

        // add functionality of saving Foo (single item, not the whole list) here?
    }

    private List<Foo> GetFoos()
    {
        List<Foo> x = new List<Foo>();
        return x;
    }

    public class Foo
    {
        public int Id { get; set; }
        public string Name { get; set; }
        // or maybe here?
    }
}
代码中的注释基本上说明了所有内容: 1.我想实现对象Foo的Save方法。 2.方法只能在从列表1中选取Foo对象时调用。 3.方法无法从单独创建的Foo对象调用。
4.方法必须使用在初始化主类期间传递的属性param的私有值。

您可以使用接口隐藏方法Save。 为此,必须显式实现Save方法。 此外,还需要指向主对象的链接。在子类Foo中,可以直接从Main访问private属性

接口:

public interface IFoo
{
    int Id { get; set; }
    string Name { get; set; }

    void Save();
}
类别:

public class Main
{
    private string param;
    private List<IFoo> foos = new List<IFoo>();

    public Main(string param) { this.param = param; }

    public List<IFoo> Foos
    {
        get { return this.foos; }
    }

    public void AddFoo(int pnId, string pnName)
    {
        this.foos.Add(new Foo(this) { Id = pnId, Name = pnName });
    }

    public class Foo : IFoo
    {
        private Main moParent;

        public Foo() { }

        public Foo(Main poParent)
        {
            this.moParent = poParent;
        }

        public int Id { get; set; }
        public string Name { get; set; }

        //Implement interface explicitly
        void IFoo.Save()
        {
            if (this.moParent == null)
                throw new InvalidOperationException("Parent not set");

            Console.WriteLine($"Save with Param: {this.moParent.param}, Id: {this.Id} Name: {this.Name}");
            //Save Item
        }
    }
}

main.Foo.Findp=>p.Id==1.Save调用Foo对象上的Save方法,就像Foo.Save一样。如果我理解正确,您正在寻找类似List.CurrentItem属性或List.SaveCurrent方法的内容,对吗?我将尝试提出一个可能的解决方案。Save函数可以在Foo类中定义,但它应该在Main类中以某种方式重载。如果我创建这个类Foo的新实例,我不想看到她。好吧,我知道你的意思,但是调用main.Foo.Findp=>p.Id==1会给你一个Foo对象。如果只使用List.Find的方式可以按照您希望的方式工作,这是否可以接受?或者,您希望它适用于从列表中拾取项目的所有情况吗?我希望使用所有方法搜索列表中的特定对象。一旦我有了一个对象,我就可以调用Save。如下所示:var find=main.Foos.FindLastp=>p.Id==1;找到。保存;原因是,在Save方法中,我想使用param,它在主类初始化期间只传递一次。这正是我需要的!谢谢
public class Main
{
    private string param;
    private List<IFoo> foos = new List<IFoo>();

    public Main(string param) { this.param = param; }

    public List<IFoo> Foos
    {
        get { return this.foos; }
    }

    public void AddFoo(int pnId, string pnName)
    {
        this.foos.Add(new Foo(this) { Id = pnId, Name = pnName });
    }

    public class Foo : IFoo
    {
        private Main moParent;

        public Foo() { }

        public Foo(Main poParent)
        {
            this.moParent = poParent;
        }

        public int Id { get; set; }
        public string Name { get; set; }

        //Implement interface explicitly
        void IFoo.Save()
        {
            if (this.moParent == null)
                throw new InvalidOperationException("Parent not set");

            Console.WriteLine($"Save with Param: {this.moParent.param}, Id: {this.Id} Name: {this.Name}");
            //Save Item
        }
    }
}
var main = new Main("hi!");

main.AddFoo(1, "Foo1");
// usage 1
main.Foos.Find(p => p.Id == 1).Save(); // method visible here

var foo = new Main.Foo();
// usage 2
//foo.Save(); // Save is not visible here