Reputation: 35
I have an abstract base class called Rack and I have different types of racks that are the children of Rack. I want to be able to dynamically cast the generic C# object to different children of the Rack class in order to call the correct getData method in which all children have as a method. Here is what I have so far.
The code below calls the virtual method in the Rack base class. I need it to call the methods within the child classes of Rack.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace IO_Interface
{
class Channel
{
private object rack1;
private object rack2;
public Channel()
{
}
public Channel(object h1, object h2)
{
rack1 = h1;
rack2 = h2;
}
public void send()
{
Type rack1Info = rack1.GetType();
Type rack2Info = rack2.GetType();
string name = rack1.ToString();
MethodInfo castMethod = rack1.GetType().GetMethod("getData").;
castMethod.Invoke(rack1.GetType(), null);
}
}
}`
Upvotes: 1
Views: 589
Reputation: 4530
I think this will provide you with the implementation you want:
class Channel
{
private List<Rack> racks;
public Channel()
{
racks = new List<Rack>();
}
public Channel(params Rack[] racks)
{
this.racks = racks.ToList();
}
public void send()
{
foreach (Rack item in racks)
{
item.getData();
}
}
public void SendSpecificRack(Rack rack)
{
//calls the getdata of the rack object passed
rack.getData();
}
}
public class Rack
{
public virtual void getData()
{
Console.WriteLine("Base Rack");
}
}
public class RackChild1 : Rack
{
public override void getData()
{
Console.WriteLine("RackChild1");
}
}
public class RackChild2 : Rack
{
public override void getData()
{
Console.WriteLine("RackChild2");
}
}
Usage:
Channel chn = new Channel(new Rack[]{new RackChild1(),new RackChild2()});
chn.send();
RackChild2 rck = new RackChild2();
chn.SendSpecificRack(rck);
Output:
RackChild1
RackChild2
RackChild2
Upvotes: 0
Reputation: 1151
What you want to do is declare your rack1 and rack2 as Racks, which will be an abstract class with an abstract method GetData. You will instantiate them as child classes of Rack somewhere. Then, when you make a call to GetData on a Rack, it will find the overridden method and call it. Here's an example.
abstract class Rack
{
public abstract void GetData();
}
class ChildRack1 : Rack
{
public override void GetData(){}
}
class ChildRack2 : Rack
{
public override void GetData(){}
}
class Channel
{
private Rack rack1;
private Rack rack2;
public Channel()
{
}
public Channel(Rack h1, Rack h2)
{
rack1 = h1;
rack2 = h2;
}
public void send()
{
rack1.GetData();
}
}
Upvotes: 5