views:

31

answers:

2

Hi, 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;
A: 
public IList<MyClass> Childrens
ma7moud
why IList<T> instead of ISet<T>?
Luka
+3  A: 

I simply create an AddChild method which does that.

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

http://stackoverflow.com/questions/645918/what-is-the-best-practice-for-readonly-lists-in-nhibernate/646824#646824

dotjoe
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