Calling an internal constructor

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

    Calling an internal constructor

    I know I can use reflection to call internal, protected, and private methods
    in a class, but I need to instantiate an object that is public but has an
    internal constructor (The CurrencyManager class).

    Is there a way to do this with reflection?

    I'm so mad at MS about the CurrencyManager design it makes me want to
    scream. It's been causing me headaches for months.

    All of this would be easily fixable if I could create my own CurrencyManager
    derived from BindingManagerB ase, and though the documentation has a "Note to
    inheritors" section, BindingManagerB ase actually has internal virtual
    methods, so there's no way to derive from it outside of S.W.F. ARGH.

    Pete


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Calling an internal constructor

    Pete,

    Unfortunately, there is nothing you can do to call this constructor from
    your constructor in code (even through reflection) so that your class can
    derive from that class correctly.

    I would recommend going to the Product Feedback center and creating an
    entry there regarding the issue. I'll definitely vote for it. The Product
    Feedback center is located at (watch for line wrap):



    I'll also see what I can do about raising awareness at MS about this
    issue.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Pete Davis" <pdavis68@NOSPA M.hotmail.com> wrote in message
    news:ArGdnSPCFr ng3sjfRVn-rg@giganews.com ...[color=blue]
    >I know I can use reflection to call internal, protected, and private
    >methods
    > in a class, but I need to instantiate an object that is public but has an
    > internal constructor (The CurrencyManager class).
    >
    > Is there a way to do this with reflection?
    >
    > I'm so mad at MS about the CurrencyManager design it makes me want to
    > scream. It's been causing me headaches for months.
    >
    > All of this would be easily fixable if I could create my own
    > CurrencyManager
    > derived from BindingManagerB ase, and though the documentation has a "Note
    > to
    > inheritors" section, BindingManagerB ase actually has internal virtual
    > methods, so there's no way to derive from it outside of S.W.F. ARGH.
    >
    > Pete
    >
    >[/color]


    Comment

    • Pete Davis

      #3
      Re: Calling an internal constructor

      Which issue? The CurrencyManager one?

      Submitted it here:



      Their response was "this is by design" because there's no way to "inject"
      the new BMB derived class into the BindingContext.

      My response was that you can get around the injection problem via
      reflection, but the correct fix would be for them to simply provide a method
      to do that.

      They apparently don't seem to excited about fixing this, though. Because as
      well all know, MS has forseen every possible use of their CurrencyManager so
      why would anyone need to write their own?

      As for creating the CurrencyManager , I found at least two ways to do it (I'm
      very stubborn and am very reluctant to take "no" for an answer).

      The first is using Activator:

      Type cmType = typeof(Currency Manager);
      Type[] paramTypes = new Type[1];
      paramTypes[0] = typeof(object);
      object[] argList = new object[1];
      argList[0] = myDataSource;
      CultureInfo currCulture = Application.Cur rentCulture;

      CurrencyManager cm = (CurrencyManage r) Activator.Creat eInstance(cmTyp e,
      BindingFlags.No nPublic | BindingFlags.In stance,
      null,
      argList,
      currCulture,
      null);


      Optionally, you can replace that last line with:

      ConstructorInfo ci = cmType.GetConst ructor(BindingF lags.NonPublic |
      BindingFlags.In stance,
      null,
      paramTypes,
      null);
      CurrencyManager cm = (CurrencyManage r) ci.Invoke(Bindi ngFlags.NonPubl ic,
      null, argList, currCulture);


      I didn't realize that invoking the constructor would create an object. I
      guess I was thinking back to the old C++ days where to pull something like
      this off, you actually had to allocate the object first, then invoke the
      constructor.

      Anyway, both methods will result in a CurrencyManager .

      Now, I need to see if I can do it for a RelatedCurrency Manager which is an
      internal type. ARGH...

      Thanks for the help Nicolas.

      Pete


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:%23l8cvA4O FHA.3880@tk2msf tngp13.phx.gbl. ..[color=blue]
      > Pete,
      >
      > Unfortunately, there is nothing you can do to call this constructor[/color]
      from[color=blue]
      > your constructor in code (even through reflection) so that your class can
      > derive from that class correctly.
      >
      > I would recommend going to the Product Feedback center and creating an
      > entry there regarding the issue. I'll definitely vote for it. The[/color]
      Product[color=blue]
      > Feedback center is located at (watch for line wrap):
      >
      > http://lab.msdn.microsoft.com/produc...k/default.aspx
      >
      > I'll also see what I can do about raising awareness at MS about this
      > issue.
      >
      > Hope this helps.
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Pete Davis" <pdavis68@NOSPA M.hotmail.com> wrote in message
      > news:ArGdnSPCFr ng3sjfRVn-rg@giganews.com ...[color=green]
      > >I know I can use reflection to call internal, protected, and private
      > >methods
      > > in a class, but I need to instantiate an object that is public but has[/color][/color]
      an[color=blue][color=green]
      > > internal constructor (The CurrencyManager class).
      > >
      > > Is there a way to do this with reflection?
      > >
      > > I'm so mad at MS about the CurrencyManager design it makes me want to
      > > scream. It's been causing me headaches for months.
      > >
      > > All of this would be easily fixable if I could create my own
      > > CurrencyManager
      > > derived from BindingManagerB ase, and though the documentation has a[/color][/color]
      "Note[color=blue][color=green]
      > > to
      > > inheritors" section, BindingManagerB ase actually has internal virtual
      > > methods, so there's no way to derive from it outside of S.W.F. ARGH.
      > >
      > > Pete
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Pete Davis

        #4
        Re: Calling an internal constructor

        It took a little trickery, but I managed to also create the
        RelatedCurrency Manager.

        This is fantastic since this really solves my major problem.

        My problem is that I have a custom hierarchical grid control and unlike the
        MS DataGrid, this one is really hierarchical in the sense that you can have
        multiple parents expanded or collapsed at a given point.

        My problem was I needed to traverse the data hierarchy to figure out the
        parents and children in the hierarchy. I could have written code to
        duplicate what the CurrencyManager does, I suppose, but why re-invent the
        wheel? So instead, I create these "shadow" currency managers that aren't in
        BindingContext and thus not bound to any controls, but still allow me to
        navigate the records without affecting other controls that are bound to the
        same data source.

        This has been plagueing me for weeks. I kept coming up with different ideas
        but nothing was working. My last attempt was pretty extreme. Using
        reflection, I got all the currency managers tied to the same data source,
        then unbound all controls bound to those currency managers, navigated the
        currency managers, then rebound the controls. The problem of course, is that
        the controls were still receiving event notifications and there was no way
        that I could find to unsubscribe and resubscribe them.

        Fortunately, the current method is far cleaner and much faster.

        Thanks for the help Nicholas.

        Pete


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Calling an internal constructor

          Pete,

          I don't think I really did much here, but you are welcome!


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          "Pete Davis" <pdavis68@NOSPA M.hotmail.com> wrote in message
          news:uJ-dnU4s148J6MjfRV n-jg@giganews.com ...[color=blue]
          > It took a little trickery, but I managed to also create the
          > RelatedCurrency Manager.
          >
          > This is fantastic since this really solves my major problem.
          >
          > My problem is that I have a custom hierarchical grid control and unlike
          > the
          > MS DataGrid, this one is really hierarchical in the sense that you can
          > have
          > multiple parents expanded or collapsed at a given point.
          >
          > My problem was I needed to traverse the data hierarchy to figure out the
          > parents and children in the hierarchy. I could have written code to
          > duplicate what the CurrencyManager does, I suppose, but why re-invent the
          > wheel? So instead, I create these "shadow" currency managers that aren't
          > in
          > BindingContext and thus not bound to any controls, but still allow me to
          > navigate the records without affecting other controls that are bound to
          > the
          > same data source.
          >
          > This has been plagueing me for weeks. I kept coming up with different
          > ideas
          > but nothing was working. My last attempt was pretty extreme. Using
          > reflection, I got all the currency managers tied to the same data source,
          > then unbound all controls bound to those currency managers, navigated the
          > currency managers, then rebound the controls. The problem of course, is
          > that
          > the controls were still receiving event notifications and there was no way
          > that I could find to unsubscribe and resubscribe them.
          >
          > Fortunately, the current method is far cleaner and much faster.
          >
          > Thanks for the help Nicholas.
          >
          > Pete
          >
          >[/color]


          Comment

          • Pete Davis

            #6
            Re: Calling an internal constructor

            Oh well, I'm an idiot. I didn't have to jump through all those hoops with
            reflection. I could have simply created a new BindingContext object
            unrelated to the form and gotten my CurrencyManager s from that. Live and
            learn....

            Pete


            "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
            message news:uWJfxO6OFH A.2468@tk2msftn gp13.phx.gbl...[color=blue]
            > Pete,
            >
            > I don't think I really did much here, but you are welcome!
            >
            >
            > --
            > - Nicholas Paldino [.NET/C# MVP]
            > - mvp@spam.guard. caspershouse.co m
            >
            > "Pete Davis" <pdavis68@NOSPA M.hotmail.com> wrote in message
            > news:uJ-dnU4s148J6MjfRV n-jg@giganews.com ...[color=green]
            > > It took a little trickery, but I managed to also create the
            > > RelatedCurrency Manager.
            > >
            > > This is fantastic since this really solves my major problem.
            > >
            > > My problem is that I have a custom hierarchical grid control and unlike
            > > the
            > > MS DataGrid, this one is really hierarchical in the sense that you can
            > > have
            > > multiple parents expanded or collapsed at a given point.
            > >
            > > My problem was I needed to traverse the data hierarchy to figure out the
            > > parents and children in the hierarchy. I could have written code to
            > > duplicate what the CurrencyManager does, I suppose, but why re-invent[/color][/color]
            the[color=blue][color=green]
            > > wheel? So instead, I create these "shadow" currency managers that aren't
            > > in
            > > BindingContext and thus not bound to any controls, but still allow me to
            > > navigate the records without affecting other controls that are bound to
            > > the
            > > same data source.
            > >
            > > This has been plagueing me for weeks. I kept coming up with different
            > > ideas
            > > but nothing was working. My last attempt was pretty extreme. Using
            > > reflection, I got all the currency managers tied to the same data[/color][/color]
            source,[color=blue][color=green]
            > > then unbound all controls bound to those currency managers, navigated[/color][/color]
            the[color=blue][color=green]
            > > currency managers, then rebound the controls. The problem of course, is
            > > that
            > > the controls were still receiving event notifications and there was no[/color][/color]
            way[color=blue][color=green]
            > > that I could find to unsubscribe and resubscribe them.
            > >
            > > Fortunately, the current method is far cleaner and much faster.
            > >
            > > Thanks for the help Nicholas.
            > >
            > > Pete
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...