Refelection - Dynamic programming

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

    #1

    Refelection - Dynamic programming

    Hello,

    I have to explain my problem with an example :

    public class DecisionTables
    {
    public ObjectName ObjectName; // ObjectName defined in
    other .cs
    public DecisionTables( )
    {
    functX();
    }
    public functX()
    {
    // Here I read from a database a name of an object and
    put it in a string (normally in a loop)
    string whichobject = "ObjectName ";

    // Create Instance of whichobject
    Type collectieType = Type.GetType(wh ichobject );
    Object objCollectie = System.Activato r.CreateInstanc e
    (collectieType) ;

    // My problem : I will dynamically set the reference
    of this new object to this.ObjectName .

    // Not dynamically I do the following
    this.ObjectName = (ObjectName)obj Collectie;

    // I don't want this, I think first I have to search for
    a member in DecisionTables of the type ObjectName and then
    I will give it a reference. But also the parsing must by
    dynamically (I have to do the parsing).

    }

    Thanks,
    Nicky
  • Mattias Sjögren

    #2
    Re: Refelection - Dynamic programming

    [color=blue]
    > // I don't want this, I think first I have to search for
    >a member in DecisionTables of the type ObjectName and then
    >I will give it a reference. But also the parsing must by
    >dynamically (I have to do the parsing).[/color]

    You can get a FieldInfo reference representing the
    DecisionTables. ObjectName field with typeof(Decision Tables).GetFiel d()
    or .GetFields(). You can then assign objCollectie to it with
    FieldInfo.SetVa lue().



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Nicky

      #3
      Re: Refelection - Dynamic programming

      Hello,

      Thanks for the answer, but I still have a little problem.
      I did the following and it works :

      FieldInfo fld = typeof(Decision Tables).GetFiel d
      ("ObjectName ");
      fld.SetValue(th is.ObjectName ,objCollectie);


      Only this.ObjectName is not dynamical.

      Do I have to make an instance of it like this :
      newobj= System.Activato r.CreateInstanc e(Type.GetType
      (strobjectname) );

      and then

      fld.SetValue(ne wobj ,objCollectie);

      Why instantiate a new object of it? In my not dynamically
      example I did not instantiate the object:
      this.ObjectName = (ObjectName)obj Collectie;

      Do i mis somethin essential here?

      Thanks for help,
      Nicky

      [color=blue]
      >-----Original Message-----
      >[color=green]
      >> // I don't want this, I think first I have to search [/color][/color]
      for [color=blue][color=green]
      >>a member in DecisionTables of the type ObjectName and [/color][/color]
      then [color=blue][color=green]
      >>I will give it a reference. But also the parsing must by
      >>dynamically (I have to do the parsing).[/color]
      >
      >You can get a FieldInfo reference representing the
      >DecisionTables .ObjectName field with typeof[/color]
      (DecisionTables ).GetField()[color=blue]
      >or .GetFields(). You can then assign objCollectie to it [/color]
      with[color=blue]
      >FieldInfo.SetV alue().
      >
      >
      >
      >Mattias
      >
      >--
      >Mattias Sjögren [MVP] mattias @ mvps.org
      >http://www.msjogren.net/dotnet/
      >Please reply only to the newsgroup.
      >.
      >[/color]

      Comment

      • Jon Skeet

        #4
        Re: Refelection - Dynamic programming

        Nicky <nicky.haezebae rt@pwc.be> wrote:[color=blue]
        > Thanks for the answer, but I still have a little problem.
        > I did the following and it works :
        >
        > FieldInfo fld = typeof(Decision Tables).GetFiel d
        > ("ObjectName ");
        > fld.SetValue(th is.ObjectName ,objCollectie);[/color]

        That's very strange - you *should* be doing:

        fld.SetValue (this, objCollectie);

        You're setting the field within *this* object, not within the one
        referred to by this.ObjectName . I'm very surprised that worked at all.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Nicky

          #5
          Re: Refelection - Dynamic programming

          What I just wrote, is not good. In this case I have to
          standalone objects and thats not what I want.

          Only in
          fld.SetValue(th is.ObjectName,o bjCollectie);

          this.ObjectName must be dynamically, because somtimes it
          can be this.ObjectName 2 or this.ObjectName 3 ....

          like objcollectie depends of the string whichobject. alse
          this.ObjectName depends if a string lest say Whichobject2.

          Thanks in advance[color=blue]
          >-----Original Message-----
          >Hello,
          >
          >Thanks for the answer, but I still have a little problem.
          >I did the following and it works :
          >
          >FieldInfo fld = typeof(Decision Tables).GetFiel d
          >("ObjectName") ;
          >fld.SetValue(t his.ObjectName ,objCollectie);
          >
          >
          >Only this.ObjectName is not dynamical.
          >
          >Do I have to make an instance of it like this :
          >newobj= System.Activato r.CreateInstanc e(Type.GetType
          >(strobjectname ));
          >
          >and then
          >
          >fld.SetValue(n ewobj ,objCollectie);
          >
          >Why instantiate a new object of it? In my not dynamically
          >example I did not instantiate the object:
          > this.ObjectName = (ObjectName)obj Collectie;
          >
          >Do i mis somethin essential here?
          >
          >Thanks for help,
          >Nicky
          >
          >[color=green]
          >>-----Original Message-----
          >>[color=darkred]
          >>> // I don't want this, I think first I have to search [/color][/color]
          >for [color=green][color=darkred]
          >>>a member in DecisionTables of the type ObjectName and [/color][/color]
          >then [color=green][color=darkred]
          >>>I will give it a reference. But also the parsing must [/color][/color][/color]
          by [color=blue][color=green][color=darkred]
          >>>dynamicall y (I have to do the parsing).[/color]
          >>
          >>You can get a FieldInfo reference representing the
          >>DecisionTable s.ObjectName field with typeof[/color]
          >(DecisionTable s).GetField()[color=green]
          >>or .GetFields(). You can then assign objCollectie to it [/color]
          >with[color=green]
          >>FieldInfo.Set Value().
          >>
          >>
          >>
          >>Mattias
          >>
          >>--
          >>Mattias Sjögren [MVP] mattias @ mvps.org
          >>http://www.msjogren.net/dotnet/
          >>Please reply only to the newsgroup.
          >>.
          >>[/color]
          >.
          >[/color]

          Comment

          Working...