1

I have this class structure :

class A     {   }
class B:A   {   }

A x = new B();

this is the runtime type :

x.GetType(); //B

How can i get its compile time type ?

I tried typeof (x) - but the argument has to be a type...

my desire answer is : A

edit

why am i asking it ?

cause i think int is inistialized via polymorphic engine :

something like this :

object myInd = new Int32(); / /this DOES compile 

and i want to verify my assumption :

does it come from object ?

and in order to do it - i have to know its static type....

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 9
    Look to the left of the x. – Hans Passant May 27 '12 at 19:00
  • 2
    Could you provide a bit of context? I fail to see a use-case for this, since the compile-time type is always known to the developer... – Heinzi May 27 '12 at 19:00
  • @HansPassant Hans , I already know that. i just want the answer via code... – Royi Namir May 27 '12 at 19:01
  • @HansPassant read my edit please.:) – Royi Namir May 27 '12 at 19:03
  • possible duplicate of [How to know in C# code which type a variable was declared with](http://stackoverflow.com/questions/1786750/how-to-know-in-c-sharp-code-which-type-a-variable-was-declared-with) – BoltClock May 27 '12 at 19:04
  • @Heinzi: The type is not always known to the developer. Consider `var` variable assigned to by a function. Although you can check the function return type, you don't always want to depend on that. – Daniel May 27 '12 at 19:08
  • @Dani var is baking its type in compile time.... – Royi Namir May 27 '12 at 19:09
  • I understand what you're asking, but not why. Usually all you need to care about is what the variable is at run time, e.g. `x is B`. I don't see where you should code differently depending on whether the caller has `A x = new B();` or `B x = new B();`. – Tim S. May 27 '12 at 19:41
  • You need to read about boxing/unboxing (see my answer) – jeroenh May 27 '12 at 20:28
  • @jeroenh see my comment to your answer – Royi Namir May 28 '12 at 05:47

2 Answers2

12

You can create a generic method:

Type StaticTypeOf<T>(T t)
{
  return typeof(T);
}

To call it:

object i = 1;

// Writes out System.Object
Console.WriteLine(StaticTypeOf(i).ToString());

It's a solution - even though I don't see what the problem solved with the solution is ;-)

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • Actually you dont need `...(T t)`! – Felix K. May 27 '12 at 19:08
  • 2
    Then you'd have to say `StaticTypeOf()`, which defeats the purpose you're trying to achieve here. – Tim S. May 27 '12 at 19:36
  • 1
    Correct but hardly useful... You're just using the compiler feature of type inference to tell you what you already know - that `i` is of type `object`. – jeroenh May 27 '12 at 20:30
  • 1
    @jeroenh: I don't understand the use for this either, but apparently Royi Namir has a use for it so I helped him with the solution. – Anders Abel May 27 '12 at 20:33
1

As stated in comments by others - the static type of an object is something you tell the compiler yourself, at compile time. There is absolutely no point in finding it out at runtime.

object myInd = new Int32(); / /this DOES compile 

This compiles not because "int is initialized via a polymorphic engine", but because in C# everything can be converted to type object. This is not the same.

The conversion from a value type (such as Int32) to object is realized through the process of boxing. You can read all about it on this msdn page: http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx.

jeroenh
  • 26,362
  • 10
  • 73
  • 104
  • you didnt understand my question at all !. ok it is a boxed value. i know that. but when i write `object i=5` and later `i.Equals(3)` - the equals is resolved at RUNTIME via virtual function from the object ! so the chain starts from object downto int32... the equlas can be started at `int i=5;` and also `object i=5` - but the chain in the former will start at the object level and in the latter it will start from `int`. i needed to check that. – Royi Namir May 28 '12 at 05:47
  • Indeed everything incl. value types is derived from System.Object, but that has nothing to do with the compile time type. My answer stands: the compile time type is what you write yourself, there is no point in finding it out at runtime. See also http://stackoverflow.com/questions/436211/is-everything-in-net-an-object/436246 – jeroenh May 28 '12 at 08:52