0

Is there a way to get the current class instance (equivalent to 'this') except as the Type of the subclass, in a superclass?

When I do something similar (It's too big and complicated to show the actual code, I'm just hoping this works the same) to the following the type that gets supplied to C.DoStuff() is an instance of A, not B. I'm doing Reflection work on the Properties of a class and because it's of type A I don't get the Properties from type A

class A
{
  public void DoStuff()
  {
    C.DoStuff(this); 
  }
}

class B : A
{

}

class C
{
  public static void main(string[] args)
  {
    A inst = new B();
    inst.DoStuff();
  }

  public static void DoStuff<T>(T obj) {//do reflection on generic type}
}
UberMouse
  • 917
  • 2
  • 12
  • 26
  • "It's too big and complicated to show the actual code" - then you're doing something wrong. Do the simplest thing possible...When you run up against problem's like the one you are describing, it's usually because it's not the best/correct way. Take a step back ... – Mitch Wheat Feb 27 '13 at 01:45
  • It's not a lot of code, but I'd have to extract a decent amount of the architecture to show a meaningful example. – UberMouse Feb 27 '13 at 01:51
  • @MitchWheat If he's not willing to c/p 70 files of code, it means his code is bad? Weird. –  Feb 27 '13 at 01:53
  • @Markus Meskanen: If program logic is so spread out across that many files, then yes, there is something wrong with the code. I'm basing that on 27 years experience. – Mitch Wheat Feb 27 '13 at 01:55
  • @MitchWheat Windows in less than 70 files? Silly you. –  Feb 27 '13 at 01:57
  • @Markus Meskanen : What are you going on about? – Mitch Wheat Feb 27 '13 at 01:58
  • @MitchWheat Just proving you wrong. If the project is large, it doesn't mean the code is bad? –  Feb 27 '13 at 01:59
  • You proved nothing. I didn't say if the project was large, I said "If program logic is so spread out across that many files, then yes, there is something wrong with the code." – Mitch Wheat Feb 27 '13 at 01:59
  • 1
    @MitchWheat And what's the difference? In a huge project, there can be 700 classes inheriting from each other, and I surely wouldn't c/p 700 files to a question on this site. –  Feb 27 '13 at 02:02
  • 1
    You win. .................. – Mitch Wheat Feb 27 '13 at 02:03
  • @MitchWheat No, I wanna give up! You win sir. –  Feb 27 '13 at 02:06

2 Answers2

1

if you change your function to

public static void DoStuff<T>(T obj)
        {
            Console.WriteLine(typeof(A).IsInstanceOfType(obj));
            Console.WriteLine(obj.GetType());
        }

you will see its still of type B, its not of type A as you claim, but it is a instance of A, so yes, you can still treat it as a B

you can also do the same in

public void DoStuff()
  {
    Console.WriteLine(typeof(A).IsInstanceOfType(this));
    Console.WriteLine(this.GetType());
    C.DoStuff(this); 
  }

is still a B. But still also an instance of A

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
0

You can probably just get away with this:

Type subclass = typeof(B);

Then perform reflection on the subclass as intended.

TtT23
  • 6,876
  • 34
  • 103
  • 174
  • Class A has code that passes an instance of Class B (Which subclasses A) to a method on Class C to perform reflection on that instance, it copies the values of all the properties from one class to another. I have no idea what Type A is passing to C at compile time, unless I add a generic parameter with the Type. But I feel there should be a better solution than that. – UberMouse Feb 27 '13 at 01:57