DeclaringType vs. ReflectedType

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sathyaish

    #1

    DeclaringType vs. ReflectedType

    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!");
    }
    }
    }

  • Damien

    #2
    Re: DeclaringType vs. ReflectedType

    Sathyaish wrote:
    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.
    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

    Comment

    • Sathyaish

      #3
      Re: DeclaringType vs. ReflectedType

      Gah! Why am I always so lazy to never open Reflector before I post a
      question?

      Thanks!

      Comment

      • Damien

        #4
        Re: DeclaringType vs. ReflectedType

        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

        Comment

        • Jon Shemitz

          #5
          Re: DeclaringType vs. ReflectedType

          Sathyaish wrote:
          System.Type.Dec laringType
          Summary:
          Gets the class that declares this member.
          >
          System.Type.Ref lectedType
          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.Reflecti on;

          namespace Inheritance
          {
          class Program
          {
          static void Main(string[] args)
          {
          Type B = typeof(Base);
          MethodInfo BaseFoo = B.GetMethod("Fo o");
          Console.WriteLi ne(
          "In Base, DeclaringType = {0}, ReflectedTyped = {1}",
          BaseFoo.Declari ngType.Name,
          BaseFoo.Reflect edType.Name);

          Type D = typeof(Derived) ;
          MethodInfo DerivedFoo = D.GetMethod("Fo o");
          Console.WriteLi ne(
          "In Derived, DeclaringType = {0}, ReflectedTyped = {1}",
          DerivedFoo.Decl aringType.Name,
          DerivedFoo.Refl ectedType.Name) ;

          Console.WriteLi ne(BaseFoo != DerivedFoo);

          Console.ReadLin e();
          }
          }

          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.

          Comment

          Working...