Reflection

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

    #1

    Reflection

    I've used reflection quite a bit to launch forms, it works great,
    requires little coding, and allows me to launch forms from a dynamic
    menu.

    Now I have a need to instantiate any one of several business classes
    dynamically, so my natural inclination was to use reflection. The
    problem I'm running into is that my business classes require arguments
    to be passed in where as the forms did not. Here is an example of
    launching a form:

    Dim ExternalAssembl y As System.Reflecti on.Assembly = _
    System.Reflecti on.Assembly.Loa dFrom(Applicati on.StartupPath &
    "\" & Container)
    Dim CalledForm As BravoBaseForm = _
    ExternalAssembl y.CreateInstanc e(FullName, True)
    CalledForm.Show ()

    Container, in my example, is an assembly containing the form,
    typically an Exe or a Dll. Fullname refers to the class name
    representing the form I want to launch. This code works fine for
    launching a form. Now contrast that with the following code:

    ExternalAssembl y = System.Reflecti on.Assembly.Loa dFrom(TargetHos t)
    BizClass = Me.ExternalAsse mbly.CreateInst ance(ClassName, _
    True, BindingFlags.Cr eateInstance, Nothing, args, _
    System.Globaliz ation.CultureIn fo.CurrentCultu re, Nothing)

    To pass in arguments, I have to use another overloaded CreateInstance
    call. I'll be honest, I'm not sure what all of the arguments are, I'm
    making somewhat educated guesses on them. The main ones I do
    understand is the first argument which is the name of the class I want
    to instantiate, and the fifth one which is an array of objects
    representing the parameters required to instantiate the class.

    The code in my second example executes without any errors, but
    BizClass is equal to nothing afterwards. Any thoughts or comments are
    appreciated.

  • aaron.kempf@gmail.com

    #2
    Re: Reflection

    classes are a waste of time; they slow your programs down

    80% of vb6 developers didn't need COM components

    Comment

    • Brian Gideon

      #3
      Re: Reflection

      On Feb 7, 8:56 am, "BK" <bkunn...@hotma il.comwrote:
      I've used reflection quite a bit to launch forms, it works great,
      requires little coding, and allows me to launch forms from a dynamic
      menu.
      >
      Now I have a need to instantiate any one of several business classes
      dynamically, so my natural inclination was to use reflection. The
      problem I'm running into is that my business classes require arguments
      to be passed in where as the forms did not. Here is an example of
      launching a form:
      >
      Dim ExternalAssembl y As System.Reflecti on.Assembly = _
      System.Reflecti on.Assembly.Loa dFrom(Applicati on.StartupPath &
      "\" & Container)
      Dim CalledForm As BravoBaseForm = _
      ExternalAssembl y.CreateInstanc e(FullName, True)
      CalledForm.Show ()
      >
      Container, in my example, is an assembly containing the form,
      typically an Exe or a Dll. Fullname refers to the class name
      representing the form I want to launch. This code works fine for
      launching a form. Now contrast that with the following code:
      >
      ExternalAssembl y = System.Reflecti on.Assembly.Loa dFrom(TargetHos t)
      BizClass = Me.ExternalAsse mbly.CreateInst ance(ClassName, _
      True, BindingFlags.Cr eateInstance, Nothing, args, _
      System.Globaliz ation.CultureIn fo.CurrentCultu re, Nothing)
      >
      To pass in arguments, I have to use another overloaded CreateInstance
      call. I'll be honest, I'm not sure what all of the arguments are, I'm
      making somewhat educated guesses on them. The main ones I do
      understand is the first argument which is the name of the class I want
      to instantiate, and the fifth one which is an array of objects
      representing the parameters required to instantiate the class.
      >
      The code in my second example executes without any errors, but
      BizClass is equal to nothing afterwards. Any thoughts or comments are
      appreciated.
      Hi,

      Take a look at what I just posted in the following thread. I
      presented two methods for creating a String object via reflection.
      Each method required passing parameters to the constructor.



      Brian

      Comment

      • BK

        #4
        Re: Reflection

        On Feb 7, 11:45 am, "Brian Gideon" <briangid...@ya hoo.comwrote:
        On Feb 7, 8:56 am, "BK" <bkunn...@hotma il.comwrote:
        >
        >
        >
        I've used reflection quite a bit to launch forms, it works great,
        requires little coding, and allows me to launch forms from a dynamic
        menu.
        >
        Now I have a need to instantiate any one of several business classes
        dynamically, so my natural inclination was to use reflection. The
        problem I'm running into is that my business classes require arguments
        to be passed in where as the forms did not. Here is an example of
        launching a form:
        >
        Dim ExternalAssembl y As System.Reflecti on.Assembly = _
        System.Reflecti on.Assembly.Loa dFrom(Applicati on.StartupPath &
        "\" & Container)
        Dim CalledForm As BravoBaseForm = _
        ExternalAssembl y.CreateInstanc e(FullName, True)
        CalledForm.Show ()
        >
        Container, in my example, is an assembly containing the form,
        typically an Exe or a Dll. Fullname refers to the class name
        representing the form I want to launch. This code works fine for
        launching a form. Now contrast that with the following code:
        >
        ExternalAssembl y = System.Reflecti on.Assembly.Loa dFrom(TargetHos t)
        BizClass = Me.ExternalAsse mbly.CreateInst ance(ClassName, _
        True, BindingFlags.Cr eateInstance, Nothing, args, _
        System.Globaliz ation.CultureIn fo.CurrentCultu re, Nothing)
        >
        To pass in arguments, I have to use another overloaded CreateInstance
        call. I'll be honest, I'm not sure what all of the arguments are, I'm
        making somewhat educated guesses on them. The main ones I do
        understand is the first argument which is the name of the class I want
        to instantiate, and the fifth one which is an array of objects
        representing the parameters required to instantiate the class.
        >
        The code in my second example executes without any errors, but
        BizClass is equal to nothing afterwards. Any thoughts or comments are
        appreciated.
        >
        Hi,
        >
        Take a look at what I just posted in the following thread. I
        presented two methods for creating a String object via reflection.
        Each method required passing parameters to the constructor.
        >
        http://groups.google.com/group/micro...languages.vb/b...
        >
        Brian
        Thanks for the reply, however neither of your examples will work. The
        line:

        Dim typ As Type = GetType(String)

        wouldn't work for me since my type is unknown at design time. Think
        of it this way. Lets say I have 5 assemblies, all dll's, that hold
        some unknown number of classes in them. Lets also assume for
        simplicity sake that all of the classes in any of the dlls conform to
        a known interface called iBusinessObject . Programmaticall y, I want to
        instantiate any one of these classes but I won't know ahead of time
        which one. Lets also say that the name of the class is known, but it
        is a string variable. How would I get the type so I can instantiate
        it?

        Comment

        • Brian Gideon

          #5
          Re: Reflection

          On Feb 7, 2:39 pm, "BK" <bkunn...@hotma il.comwrote:
          Thanks for the reply, however neither of your examples will work. The
          line:
          >
          Dim typ As Type = GetType(String)
          >
          wouldn't work for me since my type is unknown at design time. Think
          of it this way. Lets say I have 5 assemblies, all dll's, that hold
          some unknown number of classes in them. Lets also assume for
          simplicity sake that all of the classes in any of the dlls conform to
          a known interface called iBusinessObject . Programmaticall y, I want to
          instantiate any one of these classes but I won't know ahead of time
          which one. Lets also say that the name of the class is known, but it
          is a string variable. How would I get the type so I can instantiate
          it?
          Hi,

          No problem. You can do this instead to load a type at runtime.

          Dim typ as Type = Type.GetType("Y ourNamespace.Yo urType")

          Or if the assembly your type is in isn't loaded yet do this.

          Dim asm As Assembly = Assembly.Load(" YourAssembly")
          Dim typ As Type = asm.GetType("Yo urNamespace.You rType")

          Brian

          Comment

          • BK

            #6
            Re: Reflection

            Thanks, but now I try this:

            Dim typ As Type = ExternalAssembl y.GetType(FullC lassName)
            Me.BizClass = DirectCast(Me.B izClass, typ)

            and I get an error telling me "Type typ is not defined". Mind you if
            I comment the second line of code out, it runs just fine.

            ?????

            Comment

            • Lloyd Sheen

              #7
              Re: Reflection

              When you use DirectCast the second parameter is the object type, not an
              instance Type. If you are trying to cast to various type names you may have
              to come up with a better way since this way is for specific types. I use
              DirectCast only when I know what type to cast to. I don't think it works
              dynamically.

              Lloyd Sheen


              "BK" <bkunneke@hotma il.comwrote in message
              news:1170883493 .910479.215960@ a75g2000cwd.goo glegroups.com.. .
              Thanks, but now I try this:
              >
              Dim typ As Type = ExternalAssembl y.GetType(FullC lassName)
              Me.BizClass = DirectCast(Me.B izClass, typ)
              >
              and I get an error telling me "Type typ is not defined". Mind you if
              I comment the second line of code out, it runs just fine.
              >
              ?????
              >

              Comment

              • Armin Zingler

                #8
                Re: Reflection

                "BK" <bkunneke@hotma il.comschrieb
                Thanks, but now I try this:
                >
                Dim typ As Type = ExternalAssembl y.GetType(FullC lassName)
                Me.BizClass = DirectCast(Me.B izClass, typ)
                >
                and I get an error telling me "Type typ is not defined". Mind you
                if I comment the second line of code out, it runs just fine.
                >
                ?????
                >
                Directcast requires a type name, not a type object. What you're trying is
                contradictive:

                If you specify a variable type, you already know the type of object that you
                want to assign to the variable. Consequently you can write the same type
                name when you use Directcast.

                If you have a System.Type object, you do not know the type of the variable
                when writing the code. As a consequence, you can not know how to declare the
                variable - well, it's always "Object" at least.


                Armin

                Comment

                Working...