P
per9000
Hi all,
I have a question I am unable solve.
I have an inheritance graph like this:
// CAR ----- MUSICAR ---- NICECAR
// \ \
// \ +------ OLDCAR
// \
// +---- HOMEMADECAR
All originate from Car. In musiCar I introduce "public bool hasStereo"
that I can assess with a cast and the test
bool mightHaveStereo = myCar.GetType().IsSubclassOf(typeof(musiCar));
(is this the best way test that BTW?)
Now I "want" another class that does not derive from musiCar but that
has an identical member hasStereo. I reach it and I can print it on
the console. I see that it is a bool and that it has the "correct"
name by using the following snippet:
Type T = myCar.GetType();
MemberInfo[] MIF = T.GetMembers();
foreach (MemberInfo m in MIF)
{
if (m.Name == "hasStereo" && T != typeof(niceCar))
{
Console.WriteLine(" - '{0}' : '{1}' ", m.Name, m);
}
}
}
Is there any way to get the value of myCar? I was hoping on something
like this:
((T) myCar).m;
or
if (m.isBool) myBool = ((bool)m);
See below for the complete example.
Thanks,
Per
Home: http://www.pererikstrandberg.se/blog/ .
Optimization in .NET: http://tomopt.com/tomnet/ .
-------------------------
using System;
using System.Reflection;
class Program
{
//
// Inheritance diagram
//
// CAR ----- MUSICAR ---- NICECAR
// \ \
// \ +------ OLDCAR
// \
// +---- HOMEMADECAR
// base class
public abstract class Car
{
public string nickname;
public string color;
public int miles;
}
// class for a car that might have a stereo
public abstract class musiCar : Car
{
public bool hasStereo;
}
// inherited class
public class niceCar : musiCar
{
public bool hasMP314Player;
}
// inherited class
public class oldCar : musiCar
{
public int age;
}
// car that is only related to the base class car
public class homeMadeCar : Car
{
public bool hasStereo;
public bool isEpa;
}
public static void displayCar(Car myCar)
{
Console.WriteLine("The {0} car...", myCar.color);
Console.WriteLine(" - is called '{0}'", myCar.nickname);
Console.WriteLine(" - has run for '{0}' miles", myCar.miles);
// is it a musicar?
if (myCar.GetType().IsSubclassOf(typeof(musiCar)))
{
if (((musiCar)myCar).hasStereo)
Console.WriteLine(" - has a stero");
else
Console.WriteLine(" - has no stero");
// is it an old car?
if (myCar.GetType() == typeof(oldCar))
{
Console.WriteLine(" - is {0} years old",
((oldCar)myCar).age);
}
// is it a nice car?
if (myCar.GetType() == typeof(niceCar))
{
if (((niceCar)myCar).hasMP314Player)
Console.WriteLine(" - has an MP314 player");
else
Console.WriteLine(" - has no MP314 player");
}
}
// what if it is some other class that also might have a stereo
Type T = myCar.GetType();
MemberInfo[] MIF = T.GetMembers();
foreach (MemberInfo m in MIF)
{
if (m.Name == "hasStereo" && T != typeof(niceCar))
{
Console.WriteLine(" - '{0}' : '{1}' ", m.Name, m);
}
}
}
static void Main(string[] args)
{
oldCar c1 = new oldCar();
c1.age = 25;
c1.color = "beige";
c1.hasStereo = false;
c1.miles = int.MaxValue / 2;
c1.nickname = "ugly betty";
niceCar c2 = new niceCar();
c2.color = "light blue metallic";
c2.hasMP314Player = true;
c2.hasStereo = c2.hasMP314Player;
c2.miles = 1337;
c2.nickname = "Turing the Tempest";
homeMadeCar c3 = new homeMadeCar();
c3.color = "black";
c3.hasStereo = true;
c3.isEpa = true;
c3.miles = 555;
c3.nickname = "KB:_WAGON";
Console.WriteLine("CAR 1");
displayCar(c1);
Console.WriteLine("CAR 2");
displayCar(c2);
Console.WriteLine("\"CAR\" 3");
displayCar(c3);
}
}
I have a question I am unable solve.
I have an inheritance graph like this:
// CAR ----- MUSICAR ---- NICECAR
// \ \
// \ +------ OLDCAR
// \
// +---- HOMEMADECAR
All originate from Car. In musiCar I introduce "public bool hasStereo"
that I can assess with a cast and the test
bool mightHaveStereo = myCar.GetType().IsSubclassOf(typeof(musiCar));
(is this the best way test that BTW?)
Now I "want" another class that does not derive from musiCar but that
has an identical member hasStereo. I reach it and I can print it on
the console. I see that it is a bool and that it has the "correct"
name by using the following snippet:
Type T = myCar.GetType();
MemberInfo[] MIF = T.GetMembers();
foreach (MemberInfo m in MIF)
{
if (m.Name == "hasStereo" && T != typeof(niceCar))
{
Console.WriteLine(" - '{0}' : '{1}' ", m.Name, m);
}
}
}
Is there any way to get the value of myCar? I was hoping on something
like this:
((T) myCar).m;
or
if (m.isBool) myBool = ((bool)m);
See below for the complete example.
Thanks,
Per
Home: http://www.pererikstrandberg.se/blog/ .
Optimization in .NET: http://tomopt.com/tomnet/ .
-------------------------
using System;
using System.Reflection;
class Program
{
//
// Inheritance diagram
//
// CAR ----- MUSICAR ---- NICECAR
// \ \
// \ +------ OLDCAR
// \
// +---- HOMEMADECAR
// base class
public abstract class Car
{
public string nickname;
public string color;
public int miles;
}
// class for a car that might have a stereo
public abstract class musiCar : Car
{
public bool hasStereo;
}
// inherited class
public class niceCar : musiCar
{
public bool hasMP314Player;
}
// inherited class
public class oldCar : musiCar
{
public int age;
}
// car that is only related to the base class car
public class homeMadeCar : Car
{
public bool hasStereo;
public bool isEpa;
}
public static void displayCar(Car myCar)
{
Console.WriteLine("The {0} car...", myCar.color);
Console.WriteLine(" - is called '{0}'", myCar.nickname);
Console.WriteLine(" - has run for '{0}' miles", myCar.miles);
// is it a musicar?
if (myCar.GetType().IsSubclassOf(typeof(musiCar)))
{
if (((musiCar)myCar).hasStereo)
Console.WriteLine(" - has a stero");
else
Console.WriteLine(" - has no stero");
// is it an old car?
if (myCar.GetType() == typeof(oldCar))
{
Console.WriteLine(" - is {0} years old",
((oldCar)myCar).age);
}
// is it a nice car?
if (myCar.GetType() == typeof(niceCar))
{
if (((niceCar)myCar).hasMP314Player)
Console.WriteLine(" - has an MP314 player");
else
Console.WriteLine(" - has no MP314 player");
}
}
// what if it is some other class that also might have a stereo
Type T = myCar.GetType();
MemberInfo[] MIF = T.GetMembers();
foreach (MemberInfo m in MIF)
{
if (m.Name == "hasStereo" && T != typeof(niceCar))
{
Console.WriteLine(" - '{0}' : '{1}' ", m.Name, m);
}
}
}
static void Main(string[] args)
{
oldCar c1 = new oldCar();
c1.age = 25;
c1.color = "beige";
c1.hasStereo = false;
c1.miles = int.MaxValue / 2;
c1.nickname = "ugly betty";
niceCar c2 = new niceCar();
c2.color = "light blue metallic";
c2.hasMP314Player = true;
c2.hasStereo = c2.hasMP314Player;
c2.miles = 1337;
c2.nickname = "Turing the Tempest";
homeMadeCar c3 = new homeMadeCar();
c3.color = "black";
c3.hasStereo = true;
c3.isEpa = true;
c3.miles = 555;
c3.nickname = "KB:_WAGON";
Console.WriteLine("CAR 1");
displayCar(c1);
Console.WriteLine("CAR 2");
displayCar(c2);
Console.WriteLine("\"CAR\" 3");
displayCar(c3);
}
}