0

I have three classes,namely ClassA,ClassB,ClassC. ClassC inherits from ClassB,and ClassB inherits from ClassA.They are not static classes.

ClassC has a unique property,let it be "Name",which is not inherited from class B or C.

Suppose I create an instance of ClassC,namely instanceOfClassC:

ClassC instanceOfClassC = new ClassC();
instanceOfClassC.Name = "name of class";

Then I create an instance of ClassA and assign instanceOfClassC to it:

ClassA instanceofClassA = instanceOfClassC;

Then my question is: Is it possible to get "name of class" from instanceOfClassA? I've read this and this,but still haven't lost my heart. I'm writing a Converter, so I hope to get a solution without redesigning my classes.

Community
  • 1
  • 1
Chenxiao
  • 373
  • 1
  • 3
  • 15
  • No, you have to cast it back to ClassC – Sam Leach May 19 '14 at 10:43
  • ... but yes it is possible. – Rawling May 19 '14 at 10:43
  • @Rawling Then how to do it... – Chenxiao May 19 '14 at 10:44
  • 1
    "Is it possible to get "name of class" from instanceOfClassA?" Yes. `GetType()` method returns the actual object's type. – Alex Skiba May 19 '14 at 10:46
  • This is a classic. Looks to me like ClassC should have a ClassB as opposed to being a ClassB – Tony Hopkinson May 19 '14 at 10:54
  • @TonyHopkinson How can you possibly get that from three tiers of classes and one property? – Rawling May 19 '14 at 10:59
  • @Rawling Because Class C has a property that the op wants to access through ClassA. So the options are cast, or put the property on classA even though it isn't a property of A. Course the hierarchy could be wrong. NB either option could be the most pragmatic solution, but as soon as I see something like this, I ask myself the Is A , has A question again. – Tony Hopkinson May 19 '14 at 11:57

3 Answers3

2

You must cast ClassA back to ClassC to access members on ClassC:

ClassC instanceOfClassC = new ClassC();
instanceOfClassC.Name = "name of class";

ClassA insanceofClassA = instanceOfClassC;
// insanceofClassA.Name not possible as ClassA does not have a member "Name".

ClassC c = (ClassC)insanceofClassA;
string name = c.Name; 
Sam Leach
  • 12,746
  • 9
  • 45
  • 73
  • Um...I don't understand.Since ClassA does not have a property of **Name**,where is the **"name of class"** kept in instanceofClassA? – Chenxiao May 19 '14 at 10:47
  • 2
    In simple terms, `insanceofClassA` is a reference to `instanceOfClassC`. `insanceofClassA` is really a `ClassC` inside a `ClassA`. – Sam Leach May 19 '14 at 10:48
1

Naming the variables instanceOfClassA is misleading. An instance of ClassC will always be an instance of that class, even after assigning it to a variable of type ClassA. However when using a variable of type ClassA, the compiler does not know if this is your ClassC instance, or if you assigned a different ClassA instance to it.

To access ClassC members you need to cast it back, after optionally verifying that you are indeed dealing with a ClassC type:

ClassC myClassC = instanceOfClassA as ClassC;
if (myClassC != null)
{
    // It's a class C and it's been cast already
}

or

if (instanceOfClassA is ClassC)
{
    // It's a class C, cast it
    ClassC myClassC = (ClassC)instanceOfClassA;
}
C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
0

Variable instanceofClassA is declared as ClassA and it's value type is ClassB. It's possible get value of property with Reflection.

public T2 GetPropertyValue<T1, T2>(T1 sourceObject, string propertyName) {

    var value = typeof(T1).GetProperty(propertyName).GetValue(sourceObject);
    return (T2)value;
}

But, using reflection in some scenario has performance pitfall.

See: How costly is .NET reflection?

Mazaher Bazari
  • 421
  • 5
  • 12