Marc Gravell escribió:
> Example using your names... note that "baseclass" and "derivedclass"
> are a little vaguewhen removed from inhetance, but I thought it would
> be clearer to keep the same for reference purposes. Note also that the
> base (either BaseClass or IBaseClass) could be made cloneable
> (ICloneable) if you needed to make copies of just the base section.
>
> Marc
>
> using System;
> class Program
> {
> static void Main()
> {
> DerivedClass dc = new DerivedClass();
> dc.PropertyA = "A";
> dc.PropertyB = "B";
> dc.PropertyC = "C";
> IBaseClass bc = dc.BaseClass;
> }
> }
> public interface IBaseClass
> {
> string PropertyA { get;set;}
> string PropertyB {get;set;}
> }
> public class BaseClass : IBaseClass
> {
> private string propertyA;
> public string PropertyA
> {
> get { return propertyA; }
> set { propertyA = value; }
> }
> private string propertyB;
> public string PropertyB
> {
> get { return propertyB; }
> set { propertyB = value; }
> }
> }
>
> public class DerivedClass : IBaseClass
> {
> public IBaseClass BaseClass { get { return baseClass; } }
> private readonly IBaseClass baseClass;
> public DerivedClass() : this(new BaseClass()) { }
> public DerivedClass(IBaseClass baseClass)
> {
> if (baseClass == null) throw new ArgumentNullException();
> this.baseClass = baseClass;
> }
> private string propertyC;
> public string PropertyC
> {
> get { return propertyC; }
> set { propertyC = value; }
> }
> public string PropertyA
> {
> get { return baseClass.PropertyA; }
> set { baseClass.PropertyA = value; }
> }
> public string PropertyB
> {
> get { return baseClass.PropertyB; }
> set { baseClass.PropertyB = value; }
> }
> }
Thanks for the example, but I think it's not valid as an answer for the
initial question, because:
1) You're obtaining a BaseClass object from the DerivedClass, which is
the opposite what I wanted (I want to create a DerivedClass object from
the BaseClass object).
2) You have avoided the "cloning/copying code" but you have had to
implement the properties of the IBaseClass in DerivedClass, so then
you're writing more code that wouldn't have been necessary because those
properties were already in the BaseClass (all I want to do is avoid much
writing so as to be productive

, I think I will end with a reflection
solution, or a new wishlist item for a future version of C#

)
Thanks.
Andrés [ knocte ]
--