【问题标题】:Saving item picked from list保存从列表中选择的项目
【发布时间】:2018-02-28 07:40:22
【问题描述】:

主类:

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?
    }
}

测试类:

public class Test
{
    public Test()
    {
       var main = new Main("hi!");

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

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

代码中的注释基本上说明了一切:
1.我要实现对象Foo的Save()方法。
2. 方法只能在从列表中取出 Foo 对象时调用(用法1)。
3.方法不能从单独创建的Foo对象调用(用法2)。
4. 方法必须使用主类初始化时传递的属性param的私有值。

【问题讨论】:

  • 好吧,main.Foo.Find(p =&gt; p.Id == 1).Save()Foo 对象上调用Save 方法,就像foo.Save() 一样。如果我理解正确,您正在寻找类似List.CurrentItem 属性或List.SaveCurrent 方法的东西,对吧?我会尝试想出一个可能的解决方案。
  • Save() 函数可以在Foo 类中定义,但它应该在Main 类中以某种方式重载。如果我创建此类Foo 的新实例,我不想看到她。
  • 好的,我知道你的意思,但是调用main.Foo.Find(p =&gt; p.Id == 1) 会给你一个Foo 对象。如果只有List.Find 的方式可以按照您希望的方式工作,是否可以接受?还是您希望它适用于所有“从列表中挑选项目”的情况?
  • 我想使用所有方法在列表中搜索特定对象。一旦我有一个对象,我可以打电话给Save ()。像这样:var find = main.Foos.FindLast(p =&gt; p.Id == 1); find.Save(); 这样做的原因是在Save ()方法中我要使用param,在Main类的初始化过程中传递了一次。

标签: c# list function


【解决方案1】:

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

界面:

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
        }
    }
}

用法:

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

【讨论】:

  • 这正是我所需要的!谢谢!
猜你喜欢
  • 2016-03-27
  • 2010-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
相关资源
最近更新 更多