Simple Inheritance : Creating derived classes from instances of base class.

N

nyathancha

Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}


In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);


/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(allTaggedAnimals);


}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}


but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.
 
T

Tom Porterfield

Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

I would create a copy constructor in the base Animal class that takes in an
Animal and sets all the data in 'this' with the values from the passed in
instance of Animal.

public Animal(Animal copy)
{
this.x = Animal.GetX();
// etc
}
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}

Then add a constructor to TaggedAnimal that takes in an Animal.

public TaggedAnimal(Animal an) : base (an)
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);

In here, for each instance of Animal in allAnimals you would create a
TaggedAnimal, passing in the Animal to the constructor.
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(allTaggedAnimals);

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

You can't assign to the base instance like this. You could assign
properties in base based on the same property value in currentAnimal, or add
the copy constructor to Animal as I have stated above.
Public string GetTag()
{/**/}

}


but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

You can't. Once you are within your derived classes constructor, your base
instance is already created. You can't assign a new base instance.
 
N

nyathancha

Tom said:
I would create a copy constructor in the base Animal class that takes in an
Animal and sets all the data in 'this' with the values from the passed in
instance of Animal.

public Animal(Animal copy)
{
this.x = Animal.GetX();
// etc
}


Then add a constructor to TaggedAnimal that takes in an Animal.

public TaggedAnimal(Animal an) : base (an)

Unfortunately I can't create a copy constructor. Thats why i said in
the original post "because I am not always able to control/create all
the different constructors the base class has.". The problem is that
the base classes are auto generated for me in the data access layer by
the ORM tool. I don't want to modify auto generated code. And
modifying/tweaking the ORM to add custom code doesn't seem very elegant
either.


In here, for each instance of Animal in allAnimals you would create a
TaggedAnimal, passing in the Animal to the constructor.


You can't assign to the base instance like this. You could assign
properties in base based on the same property value in currentAnimal, or add
the copy constructor to Animal as I have stated above.


You can't. Once you are within your derived classes constructor, your base
instance is already created. You can't assign a new base instance.

That does seem to be the case, but can we intercept it before the
derived constructor is called and pass it a new base somehow, rather
than letting the CLR instantiate a new base. Something along the lines
of

public TaggedAnimal(Animal currentAnimal) : base = currentAnimal
{

}

I would prefer not to use reflection as it will be expensive to run
over a collection of objects

 
B

Bruce Wood

Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}


In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);


/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(allTaggedAnimals);


}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}


but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.

So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?
 
C

Christof Nordiek

Bruce Wood said:
So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?

The only way, this can be done is by either:

- add the method to the code of the base class (wich surely is only
possible, if one has access to that code.)

- use a static method, that has a first parameter of type Animal. That does
the same, with some restrictions: It can't be used in delegate creation as a
insctance method, with an instance expression of Animal. But in C# 2.0 this
can be solved by an anonymous method wich catches a variable of type Animal.

In C# 3.0 this method could be declared as extension method, so it can be
called as if it were an instance method.

Christof
 
T

Tom Leylan

Would you be willing to wrap your Animal objects in TaggedAnimal clothing?
In other words your TaggedAnimal would contain an Animal. This permits you
to reference the tag properties/methods through the TA reference but still
reference Animal properties via the Animal object it contains.

myAnimal.Tag()
myAnimal.Animal.AnimalProperty

The collection would work the same way, methods you wrote are in the
TaggedAnimals collection which contains a collection of Animals. You could
even hide your ThirdPartyORMTool methods in the TaggedAnimal Collection
class.

TaggedAnimalCollection.GetAllAnimals() would fill the inner collection.
 
B

Bruce Wood

This looks to me like Decorator, but Decorator requires that you
inherit from each class, which is what you're trying to avoid, if I
understand you correctly.

A question for the folks who are on .NET 2.0: Can you do this with
generics:

public class GenericDecorator<T> where T : Animal
{
...
}

? That is, can a generic class inherit from its own type parameter?
 
T

Tom Leylan

I don't believe so, it is simple containment. He can create a TaggedAnimal
class that has one property an Animal object. He can add methods that add
"tag" behavior and can reference the Animal property if he ever needs to
pass the Animal itself to something outside of his control.

As I understood it his own routines understand TaggedAnimal objects but some
of the existing routines only accept Animal objects. These routines aren't
going to understand anything about tagging in any case so it doesn't need
(nor want) a TaggedAnimal.

Who knows? :)
 
B

Bruce Wood

Tom said:
I don't believe so, it is simple containment. He can create a TaggedAnimal
class that has one property an Animal object. He can add methods that add
"tag" behavior and can reference the Animal property if he ever needs to
pass the Animal itself to something outside of his control.

As I understood it his own routines understand TaggedAnimal objects but some
of the existing routines only accept Animal objects. These routines aren't
going to understand anything about tagging in any case so it doesn't need
(nor want) a TaggedAnimal.

Who knows? :)

Maybe I misunderstood his original problem, then.

As I understand it, he has an ORM that is generating lots of different,
unrelated classes from data representations. He wants to graft on a Tag
property (or some other method / property / whatever) to every class
and then use the resulting class interchangeably with the original
class. The issue, as I understand it, is that he doesn't want to have
to write all of the subclasses individually, by hand, and maintain
them, and he doesn't want to have to modify the base classes, which are
automatically generated.

I could be wrong, though.

Another solution might be partial classes in C# 2.0: just modify every
automatically generated class to be partial, then write any additional
code in another file, for each class. At least then the additions
wouldn't be overwritten when you used the ORM tool to regenerate the
classes. In fact, this is how the ORM tool should be doing things:
generating partial classes that you can then expand at will.
 
B

Brian Gideon

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);


/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(allTaggedAnimals);


}

}

How would I go about doing this?

It looks like what you want is a proxy.

public interface IAnimal
{
void Eat();
}

public interface ITaggedAnimal : IAnimal
{
string getTag();
}

public class TaggedAnimalProxy : ITaggedAnimal
{
private Animal m_Target;

public TaggedAnimalProxy(IAnimal target)
{
m_Target = target;
}

public string getTag()
{
return "tag";
}

public void Eat()
{
m_Target.Eat();
}
}

public static void Main()
{
IAnimal animal = GetSomeAnimalObject();
ITaggedAnimal taggedAnimal = new TaggedAnimalProxy(animal);
}

Does that help?

Brian
 
T

Tom Leylan

I believe you recapped it more or less correctly. I'm not sure if they are
"unrelated" animal types since he expressly mentions an "Animal" base type.
He can't (apparently) subclass this the way he wants to (or doesn't want to)
so I thought he might contain them in a specialized TaggedAnimal class and
be done with it.

Again I certainly don't know for sure :)
 
B

Bruce Wood

Tom said:
I believe you recapped it more or less correctly. I'm not sure if they are
"unrelated" animal types since he expressly mentions an "Animal" base type.
He can't (apparently) subclass this the way he wants to (or doesn't want to)
so I thought he might contain them in a specialized TaggedAnimal class and
be done with it.

Again I certainly don't know for sure :)

My impression is that the "Animal" thing was just and example. In
reality he's using an ORM, so he'll have lots of different, unrelated
base classes to contend with. At least, that's what I understood.
 
T

Tom Leylan

Animal is almost assuredly just an example. If he is dealing with lots of
different classes then I would suggested the example include Dog(), Cat(),
etc. So basically you think he wants TaggableCat(), TaggableDog(), etc.?
So again can't he just contain these? The Cat and Dog objects are exposed
for what they are when he needs them to be and they have tag functionality
by virtue of their container.

Perhaps his interest has waned or he has solved it another way...
 
C

chaitanya.gurrapu

Hi,
Thanks for the replies guys. No, my interest hasn't waned yet. After
the friday evening post, i had some stuff to do on saturday morning
before I checked the newsgroups again in the evening. Sorry about the
delay :)

First of all "Animal" is an example, maybe I should have used
"customer" with the northwind database as a reference.

Tom, I did consider using the decorator pattern. You are right in some
ways that the methods that take the base "Animal" wouldn't know about
the "TaggedAnimal" class anyway and I could just pass them the animal I
get from "TaggedAnimal.GetAnimal()". But the problem is when I am
binding the object to a control such as a GridView, I want to display
the properties from the "TaggedAnimal" class as well as the base class.
This means that each of the properties in the base class would have to
be exposed as a property in the derived class. So, the TaggedAnimal
would look something like

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}


The problem with this approach is that every time I change the
underlying database, say by adding another column "BloodType", I don't
just have to use the ORM tool to regenerate my ORM DB layer, I will
also have to change some of the code in my own DB/Business Logic layer.
Basically with a new column, I would have to go back and change the
TaggedAnimal class to

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
public string BloodType {get {return currentAnimal.BloodType;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}

And I have to do that everytime I make some change to the underlying
database, which is exactly what I am trying to avoid by using the ORM
tool.


But if the TaggedAnimal inherited from the Animal class, the data in
the new column would be visible in the TaggedAnimal as well, without me
having to do anything. All I have to do is change the view layer to
include another column in the Gridview that displays the BloodType (or
not even that, if i set the AutoGenerate property to true in the
gridview)


I suppose I can still use the decorater pattern and use the
GetAnimal()."PropertyName" in the boundfield column. Do something like

<asp:BoundColumn HeaderText="Tag" DataField="Tag"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Height"
DataField="GetAnimal().Height"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Weight"
DataField="GetAnimal().Weight"></asp:BoundColumn>

Does the Gridview allow for calling methods as part of the datafield?
Need to try it out.


Nonetheless, It still seems to me that a good "design" for this calls
for the use of inheritance rather than composition. The functionality
that I am adding is basically transforming/manipulating a small bit of
data in the underlying class. This shouldn't require me to change the
type of the class itself. In a way, it is still an "Animal", I haven't
changed its nature or used it as a part of a larger, different entity.
All I did was add a tiny bit of information/processing on top, so
conceptually it seems to me that it should retain its "Animal" nature
(type).


Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities anyway. For example another class (related to the
database table) might be "Equipment". I might want to add a property
that calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}


}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"

Thanks again for the help guys, much appreciated.
 
N

nyathancha

Hi,
Thanks for the replies guys. No, my interest hasn't waned yet. After
the friday evening post, i had some stuff to do on saturday morning
before I checked the newsgroups again in the evening. Sorry about the
delay :)

First of all "Animal" is an example, maybe I should have used
"customer" with the northwind database as a reference.

Tom, I did consider using the decorator pattern. You are right in some
ways the methods that take the base "Animal" wouldn't know about the
"TaggedAnimal" class anyway and I could just pass them the animal I get
from "TaggedAnimal.GetAnimal()". But the problem is when I am binding
the object to a control such as a GridView, I want to display the
properties from the "TaggedAnimal" class as well as the base class.
This means that each of the properties in the base class would have to
be exposed as a property in the derived class. So, the TaggedAnimal
would look something like

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}


The problem with this approach is that every time I change the
underlying database, say by adding another column "BloodType", I don't
just have to use the ORM tool to regenerate my ORM DB layer, I will
also have to change some of the code in my own DB/Business Logic layer.
Basically with a new column, I would have to go back and change the
TaggedAnimal class to

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
public string BloodType {get {return currentAnimal.BloodType;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}

And I have to do that everytime I make some change to the underlying
database, which is exactly what I am trying to avoid by using the ORM
tool.


But if the TaggedAnimal inherited from the Animal class, the data in
the new column would be visible in the TaggedAnimal as well, without me
having to do anything. All I have to do is change the view layer to
include another column in the Gridview that displays the BloodType (or
not even that, if i set the AutoGenerate property to true in the
gridview)


I suppose I can still use the decorater pattern and use the
GetAnimal()."PropertyName" in the boundfield column. Do something like

<asp:BoundColumn HeaderText="Tag" DataField="Tag"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Height"
DataField="GetAnimal().Height"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Weight"
DataField="GetAnimal().Weight"></asp:BoundColumn>

Does the Gridview allow for calling methods as part of the datafield?
Need to try it out.


Nonetheless, It still seems to me that a good "design" for this calls
for the use of inheritance rather than composition. The functionality
that I am adding is basically transforming/manipulating a small bit of
data in the underlying class. This shouldn't require me to change the
type of the class itself. In a way, it is still an "Animal", I haven't
changed its nature or used it as a part of a larger, different entity.
All I did was add a tiny bit of information/processing on top, so
conceptually it seems to me that it should retain its "Animal" nature
(type).


Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities. For example another class (related to the database
table) might be "Equipment". I might want to add a property that
calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}


}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"

Thanks again for the help guys, much appreciated.
 
N

nyathancha

Hmmm... As i said in my earlier posts, reflection seems a bit top
heavy, especially if i have to do it for objects in a potentiall large
collection.

What would be good (or hoping to find) is some way of letting the CLR
know which instance of a base object to use and turn off its default
base object creation functionality.

A constructor such as public " TaggedAnimal(Animal currentAnimal) :
base = currentAnimal " would ofcourse be ideal, but is clearly not
supported. Maybe there is an event that is fired when the CLR is
creating the base class that I can subscribe to and intercept?
something along the lines of ...

public TaggedAnimal : Animal
{
public static void ReplaceBaseClass(object obj, EventArgs arg)
{
/* Hopfully, CLR will pass me some stuff in the arguments that will
allow me to change the base */
}

public TaggedAnimal(Animal currentAnimal) : base
{

}


}

And in the main class I can do something like

Public void MyMainMehtod()
{
System.CLR.OnBaseCreationEvent(typeof (TaggedAnimal)) +=
TaggedAnimal.ReplaceBaseClass()
}

Or something along those lines... You get the idea. Just thinking out
loud now basically....
 
B

Bruce Wood

Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities. For example another class (related to the database
table) might be "Equipment". I might want to add a property that
calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}


}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"

What about using partial classes? Can your ORM tool generate partial
classes? Then you could actually alter each class in a separate .cs
file and the additional properties / methods you add would survive
regeneration of the class by the ORM tool.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top