how can I get instance from the property
In my application I have a class which has properties of user-defined types like this:
class MyType
{
public A MyProperty
{
get;
set;
}
}
class A
{
.....some methods and proeprties
}
for some operations that I need to perform from my main program, I have created a List of A and add in to it MyProperty whenever creating object of MyType
List<A> myList = new List<A>();
MyType m1 = new MyType();
myList.Add(m1.MyProperty);
MyType m2 = new MyType();
myList.Add(m2.MyProperty);
......more instances
and pass it to my main program and there I perform di开发者_运维问答fferent operation on these properties which reflects in there instances also. Is there any way by which I could get the object instance for any particular MyProperty from that property in the list.
I suspect you mean you're creating a List<A>
and you want to be able to find out from an instance of A
which instance of MyType
"owns" it. There's no way of doing that without putting the information in A
(or having a comprehensive list of instances of MyType
and checking them). After all, two instances of MyType
could have the same A
:
MyType first = new MyType();
MyType second = new MyType();
A a = new A();
first.MyProperty = a;
second.MyProperty = a;
Which MyType
is a
logically associated with? Both or neither, really...
The cleanest approach is to make A
know about which MyType
it's associated with explicitly - then you can decide what should happen if you try to have one instance of A associated with two instances of MyType
(should it throw an exception? Keep both as a list? Keep the newer?) You probably want to make the MyType.MyProperty
update the value that's passed in, to keep the association in sync.
The simlest answer: use Dictionary, not List. But I think its ugly. Or put in A internal parent referense.
class A:ICloneable
{
internal MyClass _parent;
public MyClass Parent
{
get
{
return this._parent;
}
}
#region ICloneable Members
public object Clone()
{
return this.MemberwiseClone();
}
#endregion
.....some methods and proeprties
}
class MyType
{
private A _myProperty;
public A MyProperty
{
get
{
return this._myProperty;
}
set
{
this._myProperty = a.Clone();
this._myProperty._parent = this;
}
}
}
Important! Read about MemberwiseClone Function
Any reason why you could not do this instead:-
List<MyType> myList = new List<MyType>();
MyType m1 = new MyType();
myList.Add(m1);
And then in the main program you can get all of the MyProperty's from the MyType plus you would have access to any info on the MyType object which you may need.
The idea is a little similar to this...I often see developers create a method like this
public void ProcessFoo(int idOfFoo){}
then later on they need other details from the Foo object that they add to the signature
public void ProcessFoo(int idOfFoo, string name){}
The best approach (unless there is some weird limitation) is just pass in the whole Foo object in the first place...
public void ProcessFoo(Foo foo){}
hmmm well I hesitate to give any extra 'advice' without more context. I'm sure within the context of what you are doing it makes sense but it seems hazardous to me.
Jon's answer is very relevant with regard to one instance of A could be assigned to the MyProperty of MyType, then what you're saying is that instance of A could also belong to MyPropertyX and MyPropertyY.
Sorry stremlenye but what is the purpose of cloning the object? To me there would need to be a very good reason for creating a whole new separate instance...especially since cloning is rarely implemented well on all but the simplest of objects (perhaps there is a good reason, I just can't see it from the example).
精彩评论