Reflection/Metadata on an assembly to get Windows form controls

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

    Reflection/Metadata on an assembly to get Windows form controls


    I am building a utility that needs to extract all Windows forms control
    names from a compiled assembly for documentation purposes.

    I am currently using reflection, but not all of the controls are coming
    back. What should I be doing differently?

    Thanks,
    Ron

    private void Reflect(string AssemblyPath)
    {
    Assembly assem = Assembly.LoadFr om(AssemblyPath );

    Type[] types = assem.GetTypes( );

    foreach (Type t in types)
    {
    try
    {
    richTextBox1.Te xt += ("\r\n" + t.FullName + "\r\n");
    MemberInfo[] mbrInfoArray = t.GetMembers();
    foreach (MemberInfo mbrInfo in mbrInfoArray)
    {
    richTextBox1.Te xt += string.Format(" {0} is a
    {1}\r\n", mbrInfo, mbrInfo.MemberT ype);
    Application.DoE vents();
    }

    }
    }
    catch (System.NullRef erenceException )
    {
    Console.WriteLi ne("Error msg");
    }
    }

    }

    }

  • Peter Duniho

    #2
    Re: Reflection/Metadata on an assembly to get Windows form controls

    On Tue, 10 Jun 2008 11:17:10 -0700, Ron <rs_herhuth@yah oo.comwrote:
    >
    I am building a utility that needs to extract all Windows forms control
    names from a compiled assembly for documentation purposes.
    >
    I am currently using reflection, but not all of the controls are coming
    back. What should I be doing differently?
    It's hard to say without knowing more specifics about the assemblies
    you're inspecting and how you intend to use the information.

    However, not only can controls be instantiated and added to a form
    dynamically, even from within the VS Designer, you can opt to have a
    control in the form but not declare a class member to reference it.

    If you want to more reliably detect _every_ control that the assembly
    might create, you'll have to look not just as class fields, but also the
    code in class. And even that won't be 100% reliable.

    Pete

    Comment

    • Ron

      #3
      Re: Reflection/Metadata on an assembly to get Windows form controls

      Peter,

      Thanks for the reply.

      The assembly I want to run against is one of our Windows Forms projects.

      It has roughly 80+ forms.

      I am looking to extract each of the Windows controls on each of the forms
      through reflection, which I will be using for security level permissions.

      Ron


      "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
      news:op.ucjptt2 p8jd0ej@petes-computer.local. ..
      On Tue, 10 Jun 2008 11:17:10 -0700, Ron <rs_herhuth@yah oo.comwrote:
      >
      >>
      >I am building a utility that needs to extract all Windows forms control
      >names from a compiled assembly for documentation purposes.
      >>
      >I am currently using reflection, but not all of the controls are coming
      >back. What should I be doing differently?
      >
      It's hard to say without knowing more specifics about the assemblies
      you're inspecting and how you intend to use the information.
      >
      However, not only can controls be instantiated and added to a form
      dynamically, even from within the VS Designer, you can opt to have a
      control in the form but not declare a class member to reference it.
      >
      If you want to more reliably detect _every_ control that the assembly
      might create, you'll have to look not just as class fields, but also the
      code in class. And even that won't be 100% reliable.
      >
      Pete

      Comment

      • Peter Duniho

        #4
        Re: Reflection/Metadata on an assembly to get Windows form controls

        On Tue, 10 Jun 2008 11:45:42 -0700, Ron <rs_herhuth@yah oo.comwrote:
        Thanks for the reply.
        >
        The assembly I want to run against is one of our Windows Forms projects.
        >
        It has roughly 80+ forms.
        >
        I am looking to extract each of the Windows controls on each of the
        forms through reflection, which I will be using for security level
        permissions.
        None of that information changes what I wrote, nor does it elaborate on
        the specific reasons the code you posted isn't reporting the controls you
        seem to believe it should be reporting, or that you want it to report.

        Was there something else that your most recent post was intended to add?
        Is there something about my reply that wasn't clear?

        Pete

        Comment

        • Ron

          #5
          Re: Reflection/Metadata on an assembly to get Windows form controls

          Peter,
          Fair enough.

          Its not returning any controls...only properties and methods and forms. I
          dont see a single control in my output.

          Ron


          "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
          news:op.ucj4rrx n8jd0ej@petes-computer.local. ..
          On Tue, 10 Jun 2008 11:45:42 -0700, Ron <rs_herhuth@yah oo.comwrote:
          >
          >Thanks for the reply.
          >>
          >The assembly I want to run against is one of our Windows Forms projects.
          >>
          >It has roughly 80+ forms.
          >>
          >I am looking to extract each of the Windows controls on each of the
          >forms through reflection, which I will be using for security level
          >permissions.
          >
          None of that information changes what I wrote, nor does it elaborate on
          the specific reasons the code you posted isn't reporting the controls you
          seem to believe it should be reporting, or that you want it to report.
          >
          Was there something else that your most recent post was intended to add?
          Is there something about my reply that wasn't clear?
          >
          Pete

          Comment

          • parez

            #6
            Re: Reflection/Metadata on an assembly to get Windows form controls

            On Jun 11, 8:56 am, "Ron" <rs_herh...@yah oo.comwrote:
            Peter,
            Fair enough.
            >
            Its not returning any controls...only properties and methods and forms. I
            dont see a single control in my output.
            >
            Ron
            >
            "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote in message
            >
            news:op.ucj4rrx n8jd0ej@petes-computer.local. ..
            >
            On Tue, 10 Jun 2008 11:45:42 -0700, Ron <rs_herh...@yah oo.comwrote:
            >
            Thanks for the reply.
            >
            The assembly I want to run against is one of our Windows Forms projects.
            >
            It has roughly 80+ forms.
            >
            I am looking to extract each of the Windows controls on each of the
            forms through reflection, which I will be using for security level
            permissions.
            >
            None of that information changes what I wrote, nor does it elaborate on
            the specific reasons the code you posted isn't reporting the controls you
            seem to believe it should be reporting, or that you want it to report.
            >
            Was there something else that your most recent post was intended to add?
            Is there something about my reply that wasn't clear?
            >
            Pete
            Try using the following method for each of the form. that mite give
            you what you are looking for

            GetAllControls< Control>(form);

            protected List<TGetAllCon trols<T>(Contro l control) where T:Control
            {

            List<Tcontrols = new List<T>();

            foreach (Control child in control.Control s)
            {
            //if (child is T)
            T specificControl = child as T;

            if (specificContro l != null)
            controls.Add(sp ecificControl);

            if (child.HasChild ren)
            controls.AddRan ge(GetAllContro ls<T>(child));
            }
            return controls;
            }

            Comment

            • Peter Duniho

              #7
              Re: Reflection/Metadata on an assembly to get Windows form controls

              On Wed, 11 Jun 2008 05:56:15 -0700, Ron <rs_herhuth@yah oo.comwrote:
              Peter,
              Fair enough.
              >
              Its not returning any controls...only properties and methods and forms.
              I dont see a single control in my output.
              Well, control fields are usually "private" so you need to specifically ask
              for those when you reflect (with an overload that takes the options that
              specify public, private, etc.)

              So unless you have some public fields referencing controls, you wouldn't
              see any with the code you posted.

              But again, this won't necessarily tell you all of the controls that are
              actually _used_. It will only tell you the fields in each class that
              could store a control reference. You may have controls not stored in
              fields, and you may even have fields that wind up unused.

              If you're only using the VS Designer in the simplest default way, these
              issues probably won't matter. But you should still be aware of them.

              Also, you should fix your code so that you aren't calling
              Application.DoE vents(). That's just awful.

              Pete

              Comment

              Working...