Type question

K

Kimmo Laine

Hi,

is there a way to get the declaration name/information of the type:

namespace MySpace {
internal class MyClass {
internal void Foo() {
int i = 0;
PrintInfo( i );
}
}
}

// . . .
static void PrintInfo( object o ) {
Type t = o.GetType();
// ? ? ?
}

I want that PrintInfo would print something like "Declaration:
MySpace.MyClass.Foo int i"!

I can get the int-part (hehe), but the rest...

thx

Kimmo Laine
 
N

Nicholas Paldino [.NET/C# MVP]

Kimmo,

Generally speaking, you should be able to get up to "Foo" (namespace,
class, method). The Reflection APIs will give you this.

However, if you want to get "i", that's a little harder. You can
probably read the IL and determine that there is a variable of type int
initialized to 0, but it being named "i", that's only possible if you have
debug information in the assembly. If that is the case, then you can read
the source (however, you would have to do some parsing and matching), and
then get the variable name. As far as I know, there is nothing in the
framework that will aid you tremendously in this respect.

Hope this helps.
 
J

Jon Skeet [C# MVP]

Kimmo Laine said:
is there a way to get the declaration name/information of the type:

namespace MySpace {
internal class MyClass {
internal void Foo() {
int i = 0;
PrintInfo( i );
}
}
}

// . . .
static void PrintInfo( object o ) {
Type t = o.GetType();
// ? ? ?
}

I want that PrintInfo would print something like "Declaration:
MySpace.MyClass.Foo int i"!

I can get the int-part (hehe), but the rest...

All that has been passed to PrintInfo is the value, a reference to a
boxed int, 0. There is nothing which links it to MySpace.MyClass.Foo.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Kimmo,

No it is not possible.
Firstly because *i* is local variable and it is not part of any class
declaration. Secondly....If it was memeber of a type you could've probably
passed MemberInfo to the *PrintInfo* method, but passing instance of a type
doesn't give you any informaton beside you can get info regarding the type
of the object. This is not what you want as far as I understand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

operator question 2
cast to ref object 4
int to byte[] 1
Enum question 3
Method overloading 3
Hiding baseclass members 4
Use of event handling 6
Another noob question 8

Top