473,466 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DeclaringType vs. ReflectedType

The documentation says:

System.Type.DeclaringType
~~~~~~~~~~~~~~~~~~~~~~~~~~
public override System.Type DeclaringType [ get]
Member of System.Type

Summary:
Gets the class that declares this member.
System.Type.ReflectedType
~~~~~~~~~~~~~~~~~~~~~~~~~~
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 GetCallingMethodName
{
class Client
{
[STAThread]
static void Main(string[] args)
{
try
{
BarBase bar = new Bar();
bar.DoBar();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}

}
}
}


using System;

namespace GetCallingMethodName
{
interface IBar
{
void DoBar();
}
}

using System;

namespace GetCallingMethodName
{
/// <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 GetCallingMethodName
{
public class BarBase: IBar
{
public BarBase()
{

}

public virtual void DoBar()
{
Foo f = new Foo();
f.DoFoo();
return;
}
}
}

using System;
using System.Diagnostics;

namespace GetCallingMethodName
{
/// <summary>
/// Summary description for Foo.
/// </summary>
public class Foo
{
public Foo()
{
//Nothing
}

public void DoFoo()
{
Console.WriteLine("Doing foo.\n");

StackTrace trace = new StackTrace();
int count = trace.FrameCount;
Console.WriteLine("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.WriteLine("DeclaringType: " +
frame.GetMethod().DeclaringType.ToString());
Console.WriteLine("ReflectedType.FullName: " +
frame.GetMethod().ReflectedType.FullName.ToString( ));
Console.WriteLine("MemberType: " +
frame.GetMethod().MemberType.ToString());
Console.WriteLine("Name: " + frame.GetMethod().Name.ToString());
Console.WriteLine("ToString(): " + frame.GetMethod().ToString());
Console.WriteLine();
}
throw new Exception("Foo happened. Flee!");
}
}
}

Jul 19 '06 #1
4 5340
Sathyaish wrote:
The documentation says:

System.Type.DeclaringType
~~~~~~~~~~~~~~~~~~~~~~~~~~
public override System.Type DeclaringType [ get]
Member of System.Type

Summary:
Gets the class that declares this member.
System.Type.ReflectedType
~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
Well, using reflector, in 1.1, you can see that the implementation of
both of these properties is "return this;", so I don't think you'll
ever see a difference for them. In fact, a quick search through other
inheritors of MemberInfo, all of them either access an internal object
of type MemberInfo and use it's implementation, or they have the same
code.

I cannot find any implemented types with different implementations, so
I don't believe there'll ever be a difference.

Damien

Jul 19 '06 #2
Gah! Why am I always so lazy to never open Reflector before I post a
question?

Thanks!

Jul 19 '06 #3
Sathyaish wrote:
Gah! Why am I always so lazy to never open Reflector before I post a
question?

Thanks!
Because it gives me an excuse to open Reflector and look at something
different. I've never even seen these properties before, but now if I
ever do start working with them, I'll already know something about them
- so it gives me a chance to learn :-)

Damien

Jul 20 '06 #4
Sathyaish wrote:
System.Type.DeclaringType
Summary:
Gets the class that declares this member.

System.Type.ReflectedType
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.
No. ReflectedType has nothing to do with instances. It is the Type
that a particular MemberInfo belongs to. If DeclaringType !=
ReflectedType, the member is inherited; you will get a different
MemberInfo if you query a derived type than if you query the base
type, even if you are looking at the same member.

Try running this code:

// begin snippet
using System;
using System.Reflection;

namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
Type B = typeof(Base);
MethodInfo BaseFoo = B.GetMethod("Foo");
Console.WriteLine(
"In Base, DeclaringType = {0}, ReflectedTyped = {1}",
BaseFoo.DeclaringType.Name,
BaseFoo.ReflectedType.Name);

Type D = typeof(Derived);
MethodInfo DerivedFoo = D.GetMethod("Foo");
Console.WriteLine(
"In Derived, DeclaringType = {0}, ReflectedTyped = {1}",
DerivedFoo.DeclaringType.Name,
DerivedFoo.ReflectedType.Name);

Console.WriteLine(BaseFoo != DerivedFoo);

Console.ReadLine();
}
}

class Base
{
public virtual void Foo() { }
}

class Derived : Base { }
}
// end snippet

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Jul 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Richard Ward | last post by:
Hi I want to pull the name of the namespace a method is defined in from inside the method itself. MSDN say to use ISymbolMethod.GetNamespace. However, how do I get a reference to ISymbolMethod...
0
by: raca | last post by:
I am trying to create a generic SOA ServiceInvoker that will accept an XML string that will be used to deserialize an object generated by XSDObjectGen. The hierarchy goes like this:...
2
by: Jon Turner | last post by:
I am trying to read the attributes thru reflection in a method that is static to a class. my function GetList in the base class need to retrieve the attrutes from the derived class. Is there a...
2
by: Tony Maresca | last post by:
Don't know if there are any Delphi/Object Pascal converts here, but being one, I sorely miss the feature that permits class (e.g., static) methods to be virtual and be overridden in derived...
1
by: Charles Herring | last post by:
In my asp.net application I upload a video file and then need to get the duration and other properties. I get "Attempted to access an unloaded AppDomain" when I instanciate the Video object. I am...
11
by: DraguVaso | last post by:
Hi, I want to make a small application in VB.NET that relinks all the query's and tables in an Access database that are linked via ODBC to an SQL Server. It must be able to relink all the tables...
10
by: Ger | last post by:
I am having problems using VB.Net's Management base object on a machine hosting Windows Server 2003. I am trying to set file permissions from a Windows Service. These files may be loacted on a...
0
by: tatilou | last post by:
Hello, I need add new method to my properties. these methods should give me some attributes coming from my database. I think, to create a class with implement PropertyInfo but I don't know...
4
by: Sathyaish | last post by:
The documentation says: System.Type.DeclaringType ~~~~~~~~~~~~~~~~~~~~~~~~~~ public override System.Type DeclaringType Member of System.Type Summary: Gets the class that declares this...
3
by: my cats, Gag and yak | last post by:
the following keeps showing up in Application_Error I have re-installed VS2008 and it still happens I do not know what this means, thank you your help. {Name = "EventArgs" FullName =...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.