如何获取当前项目和类的名称 [英] how-to get the name of the current project and class

查看:101
本文介绍了如何获取当前项目和类的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




大家好 -


请帮助。


如何获得当前项目的名称和当前的类别?


这种情况​​。


假设有一个名为P1的项目。 。


现在,假设在P1项目中有一个名为C1的类。


现在,假设在C1中class有一个名为M1的方法。


现在,假设代码在M1中执行,并且想要获取名称

当前项目和当前班级的名称,并将其读入

变量。


如何做到这一点?


(作为奖励,如果人们也可以获取当前方法的名称,那也是很棒的,但我曾经被告知无法做到这一点;但是,我不会

相信它。)


任何帮助都表示赞赏。


(请注意我正在寻求帮助科幻反思下的les,但是我还没有找到它。)


谢谢。


- 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

推荐答案

" Mark Kamoski" < MK ****** @ yahoo.com> schrieb
"Mark Kamoski" <mk******@yahoo.com> schrieb
如何获得当前项目的名称和当前的课程?

这就是情况。

假设有一个名为P1的项目。

现在,假设在P1项目中有一个名为C1的类。

现在,假设在C1类有一种称为M1的方法。

现在,假设代码在M1中执行,并且想要获取当前项目的名称和当前名称将它们读成变量。

如何做到这一点?

(作为奖励,如果有人能抓住这个名字,它也会很棒
目前的方法也是如此,但我曾被告知无法做到这一点;但是,
我不相信。)
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

M sgBox(_

Type.Assembly.Location& vbCrLf _

& Type.FullName& vbCrLf _

& mb.Name _



-

Armin



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 _
)
--
Armin


嗨Mark,


您可以了解您所处的例程以及所有

来电者。


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" &安培;我自己& vbCrLf& 被...打电话 &安培; MyCaller

结束功能


如果你想要一个通用的错误例程,这可以非常方便,因为它可以得到名字的
调用者(这将是错误发生的地方)。


问候,

Fergus
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--


感谢您的回复。


关于获取项目名称...


不幸的是,这个电话产生了很多其他的垃圾。这种情况不需要




所需要的只是项目名称本身。

也就是说,它很好,但是太多了。


请注意以下几点......

...这...


Response.Write(" System.Reflection.Assembly.GetExec utingAssembly.FullName =

"& System.Reflection.Assembly.GetExecutingAssembly.Fu llName)


....收益......

System.Reflection.Assembly.GetExecutingAssembly.Fu llName =

TestVBGeneral20030818,Version = 1.0 .1342.16137,文化=中立,

PublicKeyToken = null


......这个...


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

Me.GetType.Assembly.GetExecutingAssembly.FullName())


....收益......

Me.GetType.Assembly.GetExecutingAssembly.FullName()=

TestVBGeneral20030818,Version = 1.0.1342.16137,邪教ure =中立,

PublicKeyToken = null

....任何想法我怎么能得到项目名称,(当然,没有

必须解析字符串)?


" Herfried K. Wagner [MVP]" <喜******* @ m.activevb.de>写在消息

新闻:ez ************** @ TK2MSFTNGP09.phx.gbl ...

您好,


" Mark Kamoski" < MK ****** @ yahoo.com> schrieb:


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.GetExec utingAssembly.FullName =
" & System.Reflection.Assembly.GetExecutingAssembly.Fu llName)

....yields...

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

....and this...

Response.Write("Me.GetType.Assembly.GetExecutingAs sembly.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)?


"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:ez**************@TK2MSFTNGP09.phx.gbl...
Hello,

"Mark Kamoski" <mk******@yahoo.com> schrieb:
现在,假设在P1项目中有一个名为C1的类。


你可以得到大会的名字:


\\\

MsgBox(系统.Reflection.Assembly.GetExecutingAsse mbly.FullName)

///

(作为奖励,如果可以获取
目前的方法,但我曾被告知无法做到;但是,我不相信它。)
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.GetExecutingAsse mbly.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.GetCurrentMeth od()。姓名)

///


HTH,

Herfried K. Wagner

-

MVP·VB Classic,VB.NET
http://www.mvps.org/dotnet


这篇关于如何获取当前项目和类的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆