How to get hte Assembly when...

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

    How to get hte Assembly when...

    Hi,

    I have a form (A) which has a collection as a property. All my forms
    inhert from this base form.

    I'm tring to put some code in form A where I can get the fieldInfo from
    the form that imnherits from A. This is the code:

    Type t = this.GetType();
    FieldInfo parameterInfo = t.GetField(fiel d,BindingFlags. Instance |
    BindingFlags.No nPublic | BindingFlags.Pu blic | BindingFlags.St atic |
    BindingFlags.Fl attenHierarchy) ;
    if(parameterInf o == null)
    {
    if(Assembly.Get EntryAssembly() != null )
    {
    MessageBox.Show ("Field nod found");
    fieldFound = false;
    }
    }

    The problem with this is that it gets the Assembly from Form A (base form)
    because its where the collection was called so then the field is never
    found. Form A (bas form) is in another assembly/solution. All this has to be
    done is design time and not runtime. How can I get the fileds or assembly of
    the current form and not the inhertided form.

    I hope I made my self clear..

    Regards
    Lucas



  • Jerry Negrelli

    #2
    How to get hte Assembly when...

    I've recreated the situation you've described and haven't
    encountered any errors. Perhaps I misunderstood your
    problem, so let me explain what I did:

    Assembly One
    Defines OriginClass with one method, "public string Test
    ()". "Test" simply uses the code from your example and
    returns either the parameterInfo.N ame or "Field Not Found"

    Assembly Two
    Defines DerivedClass which inherits from OriginClass

    Assembly Three
    Defines a third assembly that defines a DerivedClass
    object and calls its "Test" method (which is actually the
    method from OriginClass).

    Does this accurately model your situation?

    Jerry Negrelli
    Senior Software Engineer
    Data Scientific Corporation
    [color=blue]
    >-----Original Message-----
    >Hi,
    >
    > I have a form (A) which has a collection as a[/color]
    property. All my forms[color=blue]
    >inhert from this base form.
    >
    > I'm tring to put some code in form A where I can get[/color]
    the fieldInfo from[color=blue]
    >the form that imnherits from A. This is the code:
    >
    >Type t = this.GetType();
    >FieldInfo parameterInfo = t.GetField[/color]
    (field,BindingF lags.Instance |[color=blue]
    >BindingFlags.N onPublic | BindingFlags.Pu blic |[/color]
    BindingFlags.St atic |[color=blue]
    >BindingFlags.F lattenHierarchy );
    >if(parameterIn fo == null)
    >{
    > if(Assembly.Get EntryAssembly() != null )
    > {
    > MessageBox.Show ("Field nod found");
    > fieldFound = false;
    > }
    >}
    >
    >The problem with this is that it gets the Assembly from[/color]
    Form A (base form)[color=blue]
    >because its where the collection was called so then the[/color]
    field is never[color=blue]
    >found. Form A (bas form) is in another[/color]
    assembly/solution. All this has to be[color=blue]
    >done is design time and not runtime. How can I get the[/color]
    fileds or assembly of[color=blue]
    >the current form and not the inhertided form.
    >
    > I hope I made my self clear..
    >
    >Regards
    >Lucas
    >
    >
    >
    >.
    >[/color]

    Comment

    • Lucas Sain

      #3
      Re: How to get hte Assembly when...

      Jerry,

      Yes it does but I'm calling a collection in design time this is where
      the problem occurs. It is at design time I want to get the field info from
      the derived class. When I execute the code from the parameterInfo.. .. that
      is in the OriginClass... it is looking for the fields in Assembly One and
      not Assembly Three... al this at design time.

      Regards
      Lucas

      "Jerry Negrelli" <jerry.negrelli @nospam.datasci entific.com> wrote in message
      news:075a01c39f d6$6d6291e0$a00 1280a@phx.gbl.. .[color=blue]
      > I've recreated the situation you've described and haven't
      > encountered any errors. Perhaps I misunderstood your
      > problem, so let me explain what I did:
      >
      > Assembly One
      > Defines OriginClass with one method, "public string Test
      > ()". "Test" simply uses the code from your example and
      > returns either the parameterInfo.N ame or "Field Not Found"
      >
      > Assembly Two
      > Defines DerivedClass which inherits from OriginClass
      >
      > Assembly Three
      > Defines a third assembly that defines a DerivedClass
      > object and calls its "Test" method (which is actually the
      > method from OriginClass).
      >
      > Does this accurately model your situation?
      >
      > Jerry Negrelli
      > Senior Software Engineer
      > Data Scientific Corporation
      >[color=green]
      > >-----Original Message-----
      > >Hi,
      > >
      > > I have a form (A) which has a collection as a[/color]
      > property. All my forms[color=green]
      > >inhert from this base form.
      > >
      > > I'm tring to put some code in form A where I can get[/color]
      > the fieldInfo from[color=green]
      > >the form that imnherits from A. This is the code:
      > >
      > >Type t = this.GetType();
      > >FieldInfo parameterInfo = t.GetField[/color]
      > (field,BindingF lags.Instance |[color=green]
      > >BindingFlags.N onPublic | BindingFlags.Pu blic |[/color]
      > BindingFlags.St atic |[color=green]
      > >BindingFlags.F lattenHierarchy );
      > >if(parameterIn fo == null)
      > >{
      > > if(Assembly.Get EntryAssembly() != null )
      > > {
      > > MessageBox.Show ("Field nod found");
      > > fieldFound = false;
      > > }
      > >}
      > >
      > >The problem with this is that it gets the Assembly from[/color]
      > Form A (base form)[color=green]
      > >because its where the collection was called so then the[/color]
      > field is never[color=green]
      > >found. Form A (bas form) is in another[/color]
      > assembly/solution. All this has to be[color=green]
      > >done is design time and not runtime. How can I get the[/color]
      > fileds or assembly of[color=green]
      > >the current form and not the inhertided form.
      > >
      > > I hope I made my self clear..
      > >
      > >Regards
      > >Lucas
      > >
      > >
      > >
      > >.
      > >[/color][/color]


      Comment

      • Grant Richins [MS]

        #4
        Re: How to get hte Assembly when...

        Maybe I'm not uderstanding you correctly, but I think you're looking in the
        wrong place for the property. Does this code work for you:

        // Assembly1.cs
        public class BaseClass
        {
        int SomeField;
        void Test() {
        Type t = this.GetType(); // I think this is wrong
        t = typeof(BaseClas s); // Try this instead
        FieldInfo parameterInfo = t.GetField(fiel d,BindingFlags. Instance |
        BindingFlags.No nPublic | BindingFlags.Pu blic | BindingFlags.St atic |
        BindingFlags.Fl attenHierarchy) ;
        if(parameterInf o == null)
        {
        if(Assembly.Get EntryAssembly() != null )
        {
        MessageBox.Show ("Field not found");
        fieldFound = false;
        }
        }
        }
        }

        // Assembly2.cs
        public class DerivedClass : BaseClass
        {
        }

        --
        --Grant
        This posting is provided "AS IS" with no warranties, and confers no rights.


        "Lucas Sain" <lsain@lidersof t.com> wrote in message
        news:uLHc6d7nDH A.2488@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Hi,
        >
        > I have a form (A) which has a collection as a property. All my forms
        > inhert from this base form.
        >
        > I'm tring to put some code in form A where I can get the fieldInfo[/color]
        from[color=blue]
        > the form that imnherits from A. This is the code:
        >
        > Type t = this.GetType();
        > FieldInfo parameterInfo = t.GetField(fiel d,BindingFlags. Instance |
        > BindingFlags.No nPublic | BindingFlags.Pu blic | BindingFlags.St atic |
        > BindingFlags.Fl attenHierarchy) ;
        > if(parameterInf o == null)
        > {
        > if(Assembly.Get EntryAssembly() != null )
        > {
        > MessageBox.Show ("Field nod found");
        > fieldFound = false;
        > }
        > }
        >
        > The problem with this is that it gets the Assembly from Form A (base form)
        > because its where the collection was called so then the field is never
        > found. Form A (bas form) is in another assembly/solution. All this has to[/color]
        be[color=blue]
        > done is design time and not runtime. How can I get the fileds or assembly[/color]
        of[color=blue]
        > the current form and not the inhertided form.
        >
        > I hope I made my self clear..
        >
        > Regards
        > Lucas
        >
        >
        >[/color]


        Comment

        • Lucas Sain

          #5
          Re: How to get hte Assembly when...

          Grant,
          Thanks for your Reply.
          BaseClass has a method (BaseMehtod) that searches X control on a form
          (The code I wrote before).
          The problem is that in the DerivedClass I have added some Controls which
          I want the BaseMethod to be able to reflect at design time because all this
          validation should occure at design time, so if I use your sugestion I only
          get the type of the BaseClass so no fields are found. All the fields are in
          the DerivedClass.

          Am I doins something wrong or am I missing something....

          Regards
          Lucas Sain




          "Grant Richins [MS]" <grantri@online .microsoft.com> wrote in message
          news:%23XUcw0ko DHA.976@tk2msft ngp13.phx.gbl.. .[color=blue]
          > Maybe I'm not uderstanding you correctly, but I think you're looking in[/color]
          the[color=blue]
          > wrong place for the property. Does this code work for you:
          >
          > // Assembly1.cs
          > public class BaseClass
          > {
          > int SomeField;
          > void Test() {
          > Type t = this.GetType(); // I think this is wrong
          > t = typeof(BaseClas s); // Try this instead
          > FieldInfo parameterInfo = t.GetField(fiel d,BindingFlags. Instance |
          > BindingFlags.No nPublic | BindingFlags.Pu blic | BindingFlags.St atic[/color]
          |[color=blue]
          > BindingFlags.Fl attenHierarchy) ;
          > if(parameterInf o == null)
          > {
          > if(Assembly.Get EntryAssembly() != null )
          > {
          > MessageBox.Show ("Field not found");
          > fieldFound = false;
          > }
          > }
          > }
          > }
          >
          > // Assembly2.cs
          > public class DerivedClass : BaseClass
          > {
          > }
          >
          > --
          > --Grant
          > This posting is provided "AS IS" with no warranties, and confers no[/color]
          rights.[color=blue]
          >
          >
          > "Lucas Sain" <lsain@lidersof t.com> wrote in message
          > news:uLHc6d7nDH A.2488@TK2MSFTN GP12.phx.gbl...[color=green]
          > > Hi,
          > >
          > > I have a form (A) which has a collection as a property. All my forms
          > > inhert from this base form.
          > >
          > > I'm tring to put some code in form A where I can get the fieldInfo[/color]
          > from[color=green]
          > > the form that imnherits from A. This is the code:
          > >
          > > Type t = this.GetType();
          > > FieldInfo parameterInfo = t.GetField(fiel d,BindingFlags. Instance |
          > > BindingFlags.No nPublic | BindingFlags.Pu blic | BindingFlags.St atic |
          > > BindingFlags.Fl attenHierarchy) ;
          > > if(parameterInf o == null)
          > > {
          > > if(Assembly.Get EntryAssembly() != null )
          > > {
          > > MessageBox.Show ("Field nod found");
          > > fieldFound = false;
          > > }
          > > }
          > >
          > > The problem with this is that it gets the Assembly from Form A (base[/color][/color]
          form)[color=blue][color=green]
          > > because its where the collection was called so then the field is never
          > > found. Form A (bas form) is in another assembly/solution. All this has[/color][/color]
          to[color=blue]
          > be[color=green]
          > > done is design time and not runtime. How can I get the fileds or[/color][/color]
          assembly[color=blue]
          > of[color=green]
          > > the current form and not the inhertided form.
          > >
          > > I hope I made my self clear..
          > >
          > > Regards
          > > Lucas
          > >
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Grant Richins [MS]

            #6
            Re: How to get hte Assembly when...

            Ahhhh! Well, design time is an entirely different beast and well out of my
            league.

            So you have something like this:
            //assembly 1
            class BaseClass {
            void EnumDerivedFiel ds() {
            // code here to find "SomeField" ?
            }
            }

            //assembly 2
            class DerviedClass : BaseClass {
            int SomeField;
            }


            So basically you have a base class that somehow needs to change according to
            which specific derived class instance it is? It sounds like what you should
            really be doing is having more virtual methods or properties in the derived
            classes...
            --
            --Grant
            This posting is provided "AS IS" with no warranties, and confers no rights.


            "Lucas Sain" <lsain@lidersof t.com> wrote in message
            news:%23%23oRk0 HpDHA.2808@TK2M SFTNGP10.phx.gb l...[color=blue]
            > Grant,
            > Thanks for your Reply.
            > BaseClass has a method (BaseMehtod) that searches X control on a form
            > (The code I wrote before).
            > The problem is that in the DerivedClass I have added some Controls[/color]
            which[color=blue]
            > I want the BaseMethod to be able to reflect at design time because all[/color]
            this[color=blue]
            > validation should occure at design time, so if I use your sugestion I only
            > get the type of the BaseClass so no fields are found. All the fields are[/color]
            in[color=blue]
            > the DerivedClass.
            >
            > Am I doins something wrong or am I missing something....
            >
            > Regards
            > Lucas Sain
            >
            >
            >
            >
            > "Grant Richins [MS]" <grantri@online .microsoft.com> wrote in message
            > news:%23XUcw0ko DHA.976@tk2msft ngp13.phx.gbl.. .[color=green]
            > > Maybe I'm not uderstanding you correctly, but I think you're looking in[/color]
            > the[color=green]
            > > wrong place for the property. Does this code work for you:
            > >
            > > // Assembly1.cs
            > > public class BaseClass
            > > {
            > > int SomeField;
            > > void Test() {
            > > Type t = this.GetType(); // I think this is wrong
            > > t = typeof(BaseClas s); // Try this instead
            > > FieldInfo parameterInfo = t.GetField(fiel d,BindingFlags. Instance[/color][/color]
            |[color=blue][color=green]
            > > BindingFlags.No nPublic | BindingFlags.Pu blic |[/color][/color]
            BindingFlags.St atic[color=blue]
            > |[color=green]
            > > BindingFlags.Fl attenHierarchy) ;
            > > if(parameterInf o == null)
            > > {
            > > if(Assembly.Get EntryAssembly() != null )
            > > {
            > > MessageBox.Show ("Field not found");
            > > fieldFound = false;
            > > }
            > > }
            > > }
            > > }
            > >
            > > // Assembly2.cs
            > > public class DerivedClass : BaseClass
            > > {
            > > }
            > >
            > > --
            > > --Grant
            > > This posting is provided "AS IS" with no warranties, and confers no[/color]
            > rights.[color=green]
            > >
            > >
            > > "Lucas Sain" <lsain@lidersof t.com> wrote in message
            > > news:uLHc6d7nDH A.2488@TK2MSFTN GP12.phx.gbl...[color=darkred]
            > > > Hi,
            > > >
            > > > I have a form (A) which has a collection as a property. All my[/color][/color][/color]
            forms[color=blue][color=green][color=darkred]
            > > > inhert from this base form.
            > > >
            > > > I'm tring to put some code in form A where I can get the fieldInfo[/color]
            > > from[color=darkred]
            > > > the form that imnherits from A. This is the code:
            > > >
            > > > Type t = this.GetType();
            > > > FieldInfo parameterInfo = t.GetField(fiel d,BindingFlags. Instance |
            > > > BindingFlags.No nPublic | BindingFlags.Pu blic | BindingFlags.St atic |
            > > > BindingFlags.Fl attenHierarchy) ;
            > > > if(parameterInf o == null)
            > > > {
            > > > if(Assembly.Get EntryAssembly() != null )
            > > > {
            > > > MessageBox.Show ("Field nod found");
            > > > fieldFound = false;
            > > > }
            > > > }
            > > >
            > > > The problem with this is that it gets the Assembly from Form A (base[/color][/color]
            > form)[color=green][color=darkred]
            > > > because its where the collection was called so then the field is[/color][/color][/color]
            never[color=blue][color=green][color=darkred]
            > > > found. Form A (bas form) is in another assembly/solution. All this has[/color][/color]
            > to[color=green]
            > > be[color=darkred]
            > > > done is design time and not runtime. How can I get the fileds or[/color][/color]
            > assembly[color=green]
            > > of[color=darkred]
            > > > the current form and not the inhertided form.
            > > >
            > > > I hope I made my self clear..
            > > >
            > > > Regards
            > > > Lucas
            > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...