how-to get the name of the current project and class

  • Thread starter Thread starter Mark Kamoski
  • Start date Start date
M

Mark Kamoski

Hi Everyone--

Please help.

How can one get the name of the current project and the current class?

This is the situation.

Suppose there is a project called "P1".

Now, suppose that in the P1 project there is a class called "C1".

Now, suppose that in the C1 class there is a method called "M1".

Now, suppose that code is executing in M1 and one wants to grab the name of
current the project and the name of the current class and read them into
variables.

How can this be done?

(As a bonus, it would also be GREAT if one could grab the name of the
current method too, but I was once told this cannot be done; but, I don't
believe it.)

Any help is appreciated.

(Please note that I am looking in the help files under Reflection, but I
have not found it yet.)

Thank you.

--Mark
 
Mark Kamoski said:
How can one get the name of the current project and the current
class?

This is the situation.

Suppose there is a project called "P1".

Now, suppose that in the P1 project there is a class called "C1".

Now, suppose that in the C1 class there is a method called "M1".

Now, suppose that code is executing in M1 and one wants to grab the
name of current the project and the name of the current class and
read them into variables.

How can this be done?

(As a bonus, it would also be GREAT if one could grab the name of
the current method too, but I was once told this cannot be done; but,
I don't believe it.)

Dim mb As System.Reflection.MethodBase
Dim Type As Type
mb = System.Reflection.MethodBase.GetCurrentMethod
Type = mb.DeclaringType
MsgBox( _
Type.Assembly.Location & vbCrLf _
& Type.FullName & vbCrLf _
& mb.Name _
)
 
Hi Mark,

You can find out about the routine that you're in as well as all its
callers.

Public Function MeAndMyCaller As String
Dim CurrentStack As New System.Diagnostics.StackTrace()
Dim Myself As String = CurrentStack.GetFrame(0).GetMethod.Name
Dim MyCaller As String = CurrentStack.GetFrame(1).GetMethod.Name
Return "In " & Myself & vbCrLf & "Called by " & MyCaller
End Function

This can be very handy if you want a generalised error routine because it
can get the name of the caller (which would be where the error occurred).

Regards,
Fergus
 
Herfried--

Thank you for the reply.

Regarding getting the project name...

Unfortunately, that call yields a lot of other "junk" that is not needed in
this case.

All that is needed is the project name itself.


That is, it is good, but it is TOO much.

Note the following...


...this...

Response.Write("System.Reflection.Assembly.GetExecutingAssembly.FullName =
" & System.Reflection.Assembly.GetExecutingAssembly.FullName)

....yields...

System.Reflection.Assembly.GetExecutingAssembly.FullName =
TestVBGeneral20030818, Version=1.0.1342.16137, Culture=neutral,
PublicKeyToken=null

....and this...

Response.Write("Me.GetType.Assembly.GetExecutingAssembly.FullName() = " &
Me.GetType.Assembly.GetExecutingAssembly.FullName())

....yields...

Me.GetType.Assembly.GetExecutingAssembly.FullName() =
TestVBGeneral20030818, Version=1.0.1342.16137, Culture=neutral,
PublicKeyToken=null


....any ideas how I can get JUST the project name, (of course, without
having to parse the string)?






Hello,

Mark Kamoski said:
Now, suppose that in the P1 project there is a class called "C1".

You can get the name of the assembly:

\\\
MsgBox(System.Reflection.Assembly.GetExecutingAssembly.FullName)
///
(As a bonus, it would also be GREAT if one could grab the name of the
current method too, but I was once told this cannot be done; but, I don't
believe it.)

\\\
MsgBox(System.Reflection.MethodBase.GetCurrentMethod().Name)
///

HTH,
Herfried K. Wagner
 
Get name of project

system.Reflection.Assembly.GetExecutingAssembly.FullName.Split(",",3)(0)
 
Everyone--

OK, thank you so much for your help.

All set except for one small thing.

The call that I am using to get the class name returns "WebForm1_aspx"
rather than "WebForm1".

I do NOT want to have to parse the string.

Is there a way to do this in one call?

Please advise.

Below is what I am using.


'Code...

'.....

'This returns the project name.

Response.Write("ProjectName =
Me.GetType.Assembly.GetExecutingAssembly().GetName().Name = " &
Me.GetType.Assembly.GetExecutingAssembly().GetName().Name)

'This returns the class name, plus a little more. That is, it returns
"WebForm1_aspx" instead of "WebForm1".

Response.Write("ClassName = Me.GetType.Name = " & Me.GetType().Name)

'This returns the method name.

Response.Write("MethodName =
System.Reflection.MethodBase.GetCurrentMethod().Name = " &
System.Reflection.MethodBase.GetCurrentMethod().Name)

'.....



'Output...

'ProjectName = Me.GetType.Assembly.GetExecutingAssembly().GetName().Name =
TestVBGeneral20030818

'ClassName = Me.GetType.Name = WebForm1_aspx

'MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name =
btnTestGetTypeOfMe01_Click()






Hi Everyone--

Please help.

How can one get the name of the current project and the current class?

This is the situation.

Suppose there is a project called "P1".

Now, suppose that in the P1 project there is a class called "C1".

Now, suppose that in the C1 class there is a method called "M1".

Now, suppose that code is executing in M1 and one wants to grab the name of
current the project and the name of the current class and read them into
variables.

How can this be done?

(As a bonus, it would also be GREAT if one could grab the name of the
current method too, but I was once told this cannot be done; but, I don't
believe it.)

Any help is appreciated.

(Please note that I am looking in the help files under Reflection, but I
have not found it yet.)

Thank you.

--Mark
 
Hi MS News--

It seems that this works too...


Response.Write("ProjectName =
Me.GetType.Assembly.GetExecutingAssembly().GetName().Name = " &
Me.GetType.Assembly.GetExecutingAssembly().GetName().Name)

Thank you.

--Mark



Get name of project

system.Reflection.Assembly.GetExecutingAssembly.FullName.Split(",",3)(0)
 
Armin--

Here's what I mean...

Response.Write("ProjectName =
Me.GetType.Assembly.GetExecutingAssembly().GetName().Name = " &
Me.GetType.Assembly.GetExecutingAssembly().GetName().Name)

....which is exactly what I am looking to find...

....but, if you have a better way (such as faster, shorter) code, then
please let me know.

Thank you.

--Mark




Mark Kamoski said:
Regarding getting the project name...

How do you define "project name"?
 
Get class name
system.Reflection.MethodBase.GetCurrentMethod.DeclaringType().ToString()
 
How about

Me.GetType().ToString().Split(CType("_", Char))(0).ToString()

Get class name
system.Reflection.MethodBase.GetCurrentMethod.DeclaringType().ToString()
 
MS News--


Great. That did the trick.

Here's exactly what I was looking to find...

This is the code...

Response.Write("System.Reflection.MethodBase.GetCurrentMethod().DeclaringTy
pe.Name = " &
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name)

....and this is the output...

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name =
WebForm1

....Thank you!!!

--Mark



Get class name
system.Reflection.MethodBase.GetCurrentMethod.DeclaringType().ToString()
 
Mark Kamoski said:
Armin--

Here's what I mean...

Response.Write("ProjectName =
Me.GetType.Assembly.GetExecutingAssembly().GetName().Name = " &
Me.GetType.Assembly.GetExecutingAssembly().GetName().Name)

...which is exactly what I am looking to find...

Ah, the Assembly name, ok.
 
Hi Everyone--

What a great set of responses.

Timely too.

Below is the final version of what I am using because it: (1) is one line
of code each, (2) does not use Me, (3) does not involve any string parsing,
(4) is all done with the same paradigm-- that is-- through
System.Reflection, and (5) it gives me just what I want.

'...

'ProjectName

Response.Write(System.Reflection.Assembly.GetExecutingAssembly().GetName().
Name)

Response.Write("<br />")

'ClassName

Response.Write(System.Reflection.MethodBase.GetCurrentMethod().DeclaringTyp
e.Name)

Response.Write("<br />")

'MethodName

Response.Write(System.Reflection.MethodBase.GetCurrentMethod().Name)

Response.Write("<br />")

'...

....so, I'll consider this "answered" unless someone has something else to
add.

Thanks again, Everyone!!!

--Mark






Hi Everyone--

Please help.

How can one get the name of the current project and the current class?

This is the situation.

Suppose there is a project called "P1".

Now, suppose that in the P1 project there is a class called "C1".

Now, suppose that in the C1 class there is a method called "M1".

Now, suppose that code is executing in M1 and one wants to grab the name of
current the project and the name of the current class and read them into
variables.

How can this be done?

(As a bonus, it would also be GREAT if one could grab the name of the
current method too, but I was once told this cannot be done; but, I don't
believe it.)

Any help is appreciated.

(Please note that I am looking in the help files under Reflection, but I
have not found it yet.)

Thank you.

--Mark
 
Back
Top