The documentation says:
System.Type.Dec laringType
~~~~~~~~~~~~~~~ ~~~~~~~~~~~
public override System.Type DeclaringType [ get]
Member of System.Type
Summary:
Gets the class that declares this member.
System.Type.Ref lectedType
~~~~~~~~~~~~~~~ ~~~~~~~~~~~
public override System.Type ReflectedType [ get]
Member of System.Type
Summary:
Gets the class object that was used to obtain this member.
Looks like the first one gets the class where the guy is declared
whereas the second one gets the object instance name. I thought so too
until I tried several examples and each of them produced the same
output. The last example I just tried was:
using System;
namespace GetCallingMetho dName
{
class Client
{
[STAThread]
static void Main(string[] args)
{
try
{
BarBase bar = new Bar();
bar.DoBar();
}
catch(Exception ex)
{
Console.WriteLi ne(ex.ToString( ));
}
}
}
}
using System;
namespace GetCallingMetho dName
{
interface IBar
{
void DoBar();
}
}
using System;
namespace GetCallingMetho dName
{
/// <summary>
/// Summary description for Bar.
/// </summary>
public class Bar: BarBase
{
public Bar(): base()
{
//Nothing
}
/*public override void DoBar()
{
Foo f = new Foo();
f.DoFoo();
return;
}*/
}
}
using System;
namespace GetCallingMetho dName
{
public class BarBase: IBar
{
public BarBase()
{
}
public virtual void DoBar()
{
Foo f = new Foo();
f.DoFoo();
return;
}
}
}
using System;
using System.Diagnost ics;
namespace GetCallingMetho dName
{
/// <summary>
/// Summary description for Foo.
/// </summary>
public class Foo
{
public Foo()
{
//Nothing
}
public void DoFoo()
{
Console.WriteLi ne("Doing foo.\n");
StackTrace trace = new StackTrace();
int count = trace.FrameCoun t;
Console.WriteLi ne("StackFrame. FrameCount: " + count.ToString( ) +
"\n\n");
if (count == 1)
return; /* if no one called. Impossible, actually! */
for (int i = 1; i < count; i++)
{
StackFrame frame = trace.GetFrame( i);
Console.WriteLi ne("DeclaringTy pe: " +
frame.GetMethod ().DeclaringTyp e.ToString());
Console.WriteLi ne("ReflectedTy pe.FullName: " +
frame.GetMethod ().ReflectedTyp e.FullName.ToSt ring());
Console.WriteLi ne("MemberType : " +
frame.GetMethod ().MemberType.T oString());
Console.WriteLi ne("Name: " + frame.GetMethod ().Name.ToStrin g());
Console.WriteLi ne("ToString() : " + frame.GetMethod ().ToString());
Console.WriteLi ne();
}
throw new Exception("Foo happened. Flee!");
}
}
}
System.Type.Dec laringType
~~~~~~~~~~~~~~~ ~~~~~~~~~~~
public override System.Type DeclaringType [ get]
Member of System.Type
Summary:
Gets the class that declares this member.
System.Type.Ref lectedType
~~~~~~~~~~~~~~~ ~~~~~~~~~~~
public override System.Type ReflectedType [ get]
Member of System.Type
Summary:
Gets the class object that was used to obtain this member.
Looks like the first one gets the class where the guy is declared
whereas the second one gets the object instance name. I thought so too
until I tried several examples and each of them produced the same
output. The last example I just tried was:
using System;
namespace GetCallingMetho dName
{
class Client
{
[STAThread]
static void Main(string[] args)
{
try
{
BarBase bar = new Bar();
bar.DoBar();
}
catch(Exception ex)
{
Console.WriteLi ne(ex.ToString( ));
}
}
}
}
using System;
namespace GetCallingMetho dName
{
interface IBar
{
void DoBar();
}
}
using System;
namespace GetCallingMetho dName
{
/// <summary>
/// Summary description for Bar.
/// </summary>
public class Bar: BarBase
{
public Bar(): base()
{
//Nothing
}
/*public override void DoBar()
{
Foo f = new Foo();
f.DoFoo();
return;
}*/
}
}
using System;
namespace GetCallingMetho dName
{
public class BarBase: IBar
{
public BarBase()
{
}
public virtual void DoBar()
{
Foo f = new Foo();
f.DoFoo();
return;
}
}
}
using System;
using System.Diagnost ics;
namespace GetCallingMetho dName
{
/// <summary>
/// Summary description for Foo.
/// </summary>
public class Foo
{
public Foo()
{
//Nothing
}
public void DoFoo()
{
Console.WriteLi ne("Doing foo.\n");
StackTrace trace = new StackTrace();
int count = trace.FrameCoun t;
Console.WriteLi ne("StackFrame. FrameCount: " + count.ToString( ) +
"\n\n");
if (count == 1)
return; /* if no one called. Impossible, actually! */
for (int i = 1; i < count; i++)
{
StackFrame frame = trace.GetFrame( i);
Console.WriteLi ne("DeclaringTy pe: " +
frame.GetMethod ().DeclaringTyp e.ToString());
Console.WriteLi ne("ReflectedTy pe.FullName: " +
frame.GetMethod ().ReflectedTyp e.FullName.ToSt ring());
Console.WriteLi ne("MemberType : " +
frame.GetMethod ().MemberType.T oString());
Console.WriteLi ne("Name: " + frame.GetMethod ().Name.ToStrin g());
Console.WriteLi ne("ToString() : " + frame.GetMethod ().ToString());
Console.WriteLi ne();
}
throw new Exception("Foo happened. Flee!");
}
}
}
Comment