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.