using Reflection
-
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?
-
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?
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;
-
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;