PropertyInfo and ICloneable

PropertyInfo and ICloneable

Old forum URL: forums.lhotka.net/forums/t/4312.aspx


skagen00 posted on Thursday, February 07, 2008

A coworker asked an interesting question about supplying a default value for more of a reference type propertyinfo, such as a BusinessListBase child collection.

Obviously, one can't do something like MyChildCollection.NewCollection() for the default value because all instances of the root would end up with the same child collection.

So it got me to thinking if PropertyInfo might instead behave like this:

public virtual T DefaultValue
{  
 get    
 {       
  ICloneable cloneable = _defaultValue as ICloneable;
  if (cloneable == null)
  {
   return _defaultValue;
  }
  else
  {
   return cloneable.Clone();
  }
 }
}

We can obviously override DefaultValue to do this, which is great - but it almost seems to me like this would be the desired behavior. Am I missing any gotchas?

Chris

 

RockfordLhotka replied on Thursday, February 07, 2008

I don't know. It doesn't feel right somehow, but I can't put my finger on why...

skagen00 replied on Thursday, February 07, 2008

Fair enough - I am not sure how I felt about it, either -- but it was an interesting question and it seemed like an interesting subject to bring up. The default value has a lot of applicability to value types but for most reference types it really doesn't. 

 

Copyright (c) Marimer LLC