473,320 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2952
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...
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.