PC Review


Reply
Thread Tools Rate Thread

Calling Class and Static

 
 
msnews.microsoft.com
Guest
Posts: n/a
 
      29th Dec 2004
In the following classes ... it is possible when calling

MyDerived.Test()

to know that I am calling Test from MyDerived and NOT from MyClass?!

I have tried MethodInfo.GetCurrentMethod().ReflectedType ... but that
returns back MyClass.


public class MyClass
{


public static string Test()
{

}

}

public class MyDerived : MyClass
{


}




MyDerived.Test();



 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      29th Dec 2004
msnews.microsoft.com <(E-Mail Removed)> wrote:
> In the following classes ... it is possible when calling
>
> MyDerived.Test()
>
> to know that I am calling Test from MyDerived and NOT from MyClass?!


No. It actually gets compiled to a call to MyClass.Test anyway.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
msnews.microsoft.com
Guest
Posts: n/a
 
      29th Dec 2004
Ahh ... good point. i hadn't thought of checking out the generated code ...

Thanks!

Brett

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> msnews.microsoft.com <(E-Mail Removed)> wrote:
>> In the following classes ... it is possible when calling
>>
>> MyDerived.Test()
>>
>> to know that I am calling Test from MyDerived and NOT from MyClass?!

>
> No. It actually gets compiled to a call to MyClass.Test anyway.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Jose Solorzano
Guest
Posts: n/a
 
      29th Dec 2004

You might be able to determine that from
new StackTrace().

Jose Solorzano

msnews.microsoft.com wrote:
> In the following classes ... it is possible when calling
>
> MyDerived.Test()
>
> to know that I am calling Test from MyDerived and NOT from MyClass?!
>
> I have tried MethodInfo.GetCurrentMethod().ReflectedType ... but that


> returns back MyClass.
>
>
> public class MyClass
> {
>
>
> public static string Test()
> {
>
> }
>
> }
>
> public class MyDerived : MyClass
> {
>
>
> }
>
>
>
>
> MyDerived.Test();


 
Reply With Quote
 
msnews.microsoft.com
Guest
Posts: n/a
 
      30th Dec 2004
Yea ... I had thought of that, but here are the results

at MyClass.Test()
at MyClass.Main()

Brett




"Jose Solorzano" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> You might be able to determine that from
> new StackTrace().
>
> Jose Solorzano
>
> msnews.microsoft.com wrote:
>> In the following classes ... it is possible when calling
>>
>> MyDerived.Test()
>>
>> to know that I am calling Test from MyDerived and NOT from MyClass?!
>>
>> I have tried MethodInfo.GetCurrentMethod().ReflectedType ... but that

>
>> returns back MyClass.
>>
>>
>> public class MyClass
>> {
>>
>>
>> public static string Test()
>> {
>>
>> }
>>
>> }
>>
>> public class MyDerived : MyClass
>> {
>>
>>
>> }
>>
>>
>>
>>
>> MyDerived.Test();

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a static function from a class member method is givinglinking errors Omar.Khalid79@gmail.com Microsoft VC .NET 2 20th Mar 2008 09:31 PM
Get Calling Class type in base clases static method. parez Microsoft C# .NET 1 22nd Nov 2006 08:49 PM
How to get calling object type in static method of Base Class Mirek Endys Microsoft C# .NET 5 24th Mar 2006 07:08 AM
Calling a Static Method on a Class Type passed as a Generics Argument. ESPNSTI Microsoft C# .NET 1 15th Jun 2005 02:08 AM
Static class method return non-static object? Paschalis Pagonidis Microsoft C# .NET 8 10th Nov 2004 10:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:13 PM.