Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. using Reflection

using Reflection

Scheduled Pinned Locked Moved C#
questiondebugging
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goldli
    wrote on last edited by
    #1

    there is a dll. code below -------------------------- namespace CommonFunctionLibrary { public class ClsMain { public string setValue(string a,ref string b) { b = a; return a; } } } ------------------------------------------------------- and there is a Host -------------------------------------------------------------------- string filepath = @"E:\VS2005_work\CommonFunctionLibrary\CommonFunctionLibrary\bin\Debug\CommonFunctionLibrary.dll"; string s = string.Empty; string p = string.Empty; Assembly myDllAssembly = Assembly.LoadFrom(filepath); Type dllType = myDllAssembly.GetType("CommonFunctionLibrary.ClsMain"); MethodInfo dllMethod = dllType.GetMethod("setValue"); if (dllMethod != null) { Object dllObj = Activator.CreateInstance(dllType); s=(string)dllMethod.Invoke(dllObj, new object[] {"goldli",p }); MessageBox.Show(s+"\r\n"+p); } myDllAssembly = null; ------------------------------------------------------------------------------ question: 1、the Invoke method no need to use "ref" ,why? 2、the "p" parameter has no return value,why?

    L 1 Reply Last reply
    0
    • G goldli

      there is a dll. code below -------------------------- namespace CommonFunctionLibrary { public class ClsMain { public string setValue(string a,ref string b) { b = a; return a; } } } ------------------------------------------------------- and there is a Host -------------------------------------------------------------------- string filepath = @"E:\VS2005_work\CommonFunctionLibrary\CommonFunctionLibrary\bin\Debug\CommonFunctionLibrary.dll"; string s = string.Empty; string p = string.Empty; Assembly myDllAssembly = Assembly.LoadFrom(filepath); Type dllType = myDllAssembly.GetType("CommonFunctionLibrary.ClsMain"); MethodInfo dllMethod = dllType.GetMethod("setValue"); if (dllMethod != null) { Object dllObj = Activator.CreateInstance(dllType); s=(string)dllMethod.Invoke(dllObj, new object[] {"goldli",p }); MessageBox.Show(s+"\r\n"+p); } myDllAssembly = null; ------------------------------------------------------------------------------ question: 1、the Invoke method no need to use "ref" ,why? 2、the "p" parameter has no return value,why?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi.. I'm not sure but: 1. the ref is only valid in method calls and causes a parameter to be passed as pointer so it can be modified. 2. p does not contain the new value because this doesn't work either: string s1, s2; s1 = "Blah"; s2 = s1; s2 = "foo"; Console.WriteLine(string.Format("{0} {1}",s1,s2)); the output will be "Blah foo" because a new string is a new object. the way i understand it, you should do the following: string filepath = @"E:\VS2005_work\CommonFunctionLibrary\CommonFunctionLibrary\bin\Debug\CommonFunctionLibrary.dll"; string s = string.Empty; string p = string.Empty; Assembly myDllAssembly = Assembly.LoadFrom(filepath); Type dllType = myDllAssembly.GetType("CommonFunctionLibrary.ClsMain"); MethodInfo dllMethod = dllType.GetMethod("setValue"); if (dllMethod != null) { Object dllObj = Activator.CreateInstance(dllType); object[] parameter = new object[] {"goldli",p }; s=(string)dllMethod.Invoke(dllObj, parameter); p = parameter[1].ToString(); MessageBox.Show(s+"\r\n"+p); } myDllAssembly = null;

      G 1 Reply Last reply
      0
      • L Lost User

        Hi.. I'm not sure but: 1. the ref is only valid in method calls and causes a parameter to be passed as pointer so it can be modified. 2. p does not contain the new value because this doesn't work either: string s1, s2; s1 = "Blah"; s2 = s1; s2 = "foo"; Console.WriteLine(string.Format("{0} {1}",s1,s2)); the output will be "Blah foo" because a new string is a new object. the way i understand it, you should do the following: string filepath = @"E:\VS2005_work\CommonFunctionLibrary\CommonFunctionLibrary\bin\Debug\CommonFunctionLibrary.dll"; string s = string.Empty; string p = string.Empty; Assembly myDllAssembly = Assembly.LoadFrom(filepath); Type dllType = myDllAssembly.GetType("CommonFunctionLibrary.ClsMain"); MethodInfo dllMethod = dllType.GetMethod("setValue"); if (dllMethod != null) { Object dllObj = Activator.CreateInstance(dllType); object[] parameter = new object[] {"goldli",p }; s=(string)dllMethod.Invoke(dllObj, parameter); p = parameter[1].ToString(); MessageBox.Show(s+"\r\n"+p); } myDllAssembly = null;

        G Offline
        G Offline
        goldli
        wrote on last edited by
        #3

        thank you. it works.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups