Click here to Skip to main content
15,899,754 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: General Question: A Language-Independent Design by Contract Framework--what would it be worth? Pin
PIEBALDconsult28-Nov-06 3:57
mvePIEBALDconsult28-Nov-06 3:57 
GeneralRe: General Question: A Language-Independent Design by Contract Framework--what would it be worth? Pin
Philip Laureano28-Nov-06 10:18
Philip Laureano28-Nov-06 10:18 
GeneralRe: General Question: A Language-Independent Design by Contract Framework--what would it be worth? Pin
PIEBALDconsult28-Nov-06 14:44
mvePIEBALDconsult28-Nov-06 14:44 
GeneralRe: General Question: A Language-Independent Design by Contract Framework--what would it be worth? Pin
Philip Laureano28-Nov-06 15:31
Philip Laureano28-Nov-06 15:31 
AnswerRe: General Question: A Language-Independent Design by Contract Framework--what would it be worth? Pin
PIEBALDconsult28-Nov-06 8:36
mvePIEBALDconsult28-Nov-06 8:36 
QuestionCrystalReport Pin
aPerfectCircle25-Nov-06 14:28
aPerfectCircle25-Nov-06 14:28 
AnswerRe: CrystalReport Pin
Paul Conrad26-Dec-06 17:06
professionalPaul Conrad26-Dec-06 17:06 
QuestionQuestion about MemberInfo.ReflectedType [modified] Pin
PIEBALDconsult24-Nov-06 16:09
mvePIEBALDconsult24-Nov-06 16:09 
I'm writing a method (MethodA) that needs to know what method of what class
called it (so it can say, "ClassX called me"). Without having to pass the
type down the chain.
To do this, MethodA walks back up the StackTrace and use GetMethod() looking
for the first method it finds with a custom attribute I created.
The problem I'm having is with derived classes which don't override a method
that calls MethodA. It turns out (in this case) that both DeclaringType and
ReflectedType return the base type rather than the derived type.
I boiled this down to:

namespace Template
{
public class ClassA
{
public virtual string
Declaring
{
get
{
return ( (new System.Diagnostics.StackTrace ( 0 , false
)).GetFrames() [ 0 ].GetMethod().DeclaringType.Name ) ;
}
}

public virtual string
Reflected
{
get
{
return ( (new System.Diagnostics.StackTrace ( 0 , false
)).GetFrames() [ 0 ].GetMethod().ReflectedType.Name ) ;
}
}
}

public class ClassB : ClassA
{
}

public class Template
{
[System.STAThread]
public static void
Main
(
string[] args
)
{
ClassA a = new ClassA() ;
ClassB b = new ClassB() ;

System.Console.WriteLine ( a.Declaring ) ;
System.Console.WriteLine ( a.Reflected ) ;

System.Console.WriteLine ( b.Declaring ) ;
System.Console.WriteLine ( b.Reflected ) ; // Shouldn't this
report ClassB?

return ;
}
}
}

The result is:

ClassA
ClassA
ClassA
ClassA

I can certainly understand why DeclaringType return the base type, but I had
hoped that ReflectedType would return the actual type that called it, and
that the result would be:

ClassA
ClassA
ClassA
ClassB <- note

How can I do this?
Am I doing something wrong?
Is there a better way?
Is ReflectedType broken?

(I posted this on MSDN too, and the response was more or less "that can't be done", so I figured I'd get a second opinion here.)


-- modified at 10:56 Monday 27th November, 2006
AnswerRe: Question about MemberInfo.ReflectedType Pin
Philip Laureano28-Nov-06 10:36
Philip Laureano28-Nov-06 10:36 
GeneralRe: Question about MemberInfo.ReflectedType Pin
PIEBALDconsult28-Nov-06 14:30
mvePIEBALDconsult28-Nov-06 14:30 
GeneralRe: Question about MemberInfo.ReflectedType Pin
Philip Laureano28-Nov-06 15:15
Philip Laureano28-Nov-06 15:15 
QuestionWindoow handler Pin
indian14324-Nov-06 3:59
indian14324-Nov-06 3:59 
AnswerRe: Windoow handler Pin
Dave Kreskowiak27-Nov-06 4:55
mveDave Kreskowiak27-Nov-06 4:55 
AnswerRe: Windoow handler Pin
Dave Kreskowiak28-Nov-06 1:24
mveDave Kreskowiak28-Nov-06 1:24 
QuestionFind all descendents of a type? Pin
Senor Plankton24-Nov-06 0:51
Senor Plankton24-Nov-06 0:51 
AnswerRe: Find all descendents of a type? Pin
Guffa24-Nov-06 1:03
Guffa24-Nov-06 1:03 
GeneralRe: Find all descendents of a type? Pin
Senor Plankton27-Nov-06 0:16
Senor Plankton27-Nov-06 0:16 
QuestionJAbber Pin
Tauseef A23-Nov-06 21:48
Tauseef A23-Nov-06 21:48 
AnswerRe: JAbber Pin
ednrgc27-Nov-06 3:50
ednrgc27-Nov-06 3:50 
QuestionText box shadowing Pin
~~~Johnny~~~23-Nov-06 12:04
~~~Johnny~~~23-Nov-06 12:04 
AnswerRe: Text box shadowing Pin
Thomas Stockwell23-Nov-06 15:00
professionalThomas Stockwell23-Nov-06 15:00 
AnswerRe: Text box shadowing Pin
Leah_Garrett23-Nov-06 15:13
Leah_Garrett23-Nov-06 15:13 
GeneralRe: Text box shadowing Pin
~~~Johnny~~~23-Nov-06 15:38
~~~Johnny~~~23-Nov-06 15:38 
QuestionEncryption Pin
alexfromto23-Nov-06 7:07
alexfromto23-Nov-06 7:07 
AnswerRe: Encryption Pin
Guffa23-Nov-06 8:34
Guffa23-Nov-06 8:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.