0

I need a solution for this: How to intercept ISet.Add method so I don't need to do children.Parent=parent, but jus parent.Children.Add(children);

public class MyClass
{
public MyClass Parent;
public ISet<MyClass> Childrens;
}

I want to do this:

var mc = new MyClass();
var mc2 = new MyClass();
mc.Childrens.Add(mc2);

and not

var mc = new MyClass();
var mc2 = new MyClass();
mc.Childrens.Add(mc2);
mc2.Parent=mc;
Luka
  • 4,075
  • 3
  • 35
  • 61

2 Answers2

3

I simply create an AddChild method which does that.

You could also expose the public property as readonly collection as Frederik demonstrates...

What is the best practice for readonly lists in NHibernate

Community
  • 1
  • 1
dotjoe
  • 26,242
  • 5
  • 63
  • 77
  • Yes I can do that but this will use a number of people, and I can't hide ISet.Add() method and some will use this. – Luka Jul 07 '10 at 06:44
0
public IList<MyClass> Childrens
ma7moud
  • 423
  • 1
  • 4
  • 8