Re: Reflection problem

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Richard Blewett [DevelopMentor] (richardb_at_develop.com)
Date: 09/16/04


To: microsoft.public.dotnet.languages.csharp
Date: Thu, 16 Sep 2004 09:53:56 -0700

I was just wondering if you were loading the assembly from outside of the APPBASE and picking up the wrong version as the because the application had had a startup path configured on it.

 Looking more closely at your code change:

 objAssembly.GetName()

 ...

 Activator.CreateInstance

 to

 objAssembly.CreateInstance(className);

 and are you sure no exceptions are being thrown by the Invoke?

 Regards

 Richard Blewett - DevelopMentor

 http://staff.develop.com/richardb/weblog

   nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#MAPTwAnEHA.1304@TK2MSFTNGP09.phx.gbl>

 I really don't know how Application.ExecutablePath will help. Becoz, as I've
 mentioned I'm able to successfully create objects for
 the Assembly, type, etc. The problem is in the line in which I do t.Invoke()
 
 why do you think Application.ExecutablePath will help? Pls. let me know
 
 Thanx
 Ananth
 
 "Richard Blewett [DevelopMentor]" <richardb@develop.com> wrote in message
 news:ONGyMrAnEHA.3164@TK2MSFTNGP10.phx.gbl...
> Does changing System.Windows.Forms.Application.StartupPath to
 System.Windows.Forms.Application.ExecutablePath help?
>
> Regards
>
> Richard Blewett - DevelopMentor
>
> http://staff.develop.com/richardb/weblog
>
>
 nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/>
>
> I'm using Assembly.LoadFrom
>
> I've given the complete code in my first mail...you can have a look at
 it.
>
> I haven't heard abt the Fusion Log Viewer yet...will give it a shot
>
> "Richard Blewett [DevelopMentor]" <richardb@develop.com> wrote in message
> news:%230mZn3$mEHA.2948@TK2MSFTNGP11.phx.gbl...
> > How are you loading the assembly?
> >
> > Assembly.Load
> >
> > Assembly.LoadFrom
> >
> > Activator.CreateInstance
> >
> > something else?
> >
> > Have you checked out the Fusion Log Viewer (FUSLOGVW.EXE) to see if you
> are getting an assembly resolution failure?
> >
> > Regards
> >
> > Richard Blewett - DevelopMentor
> >
> >
http://staff.develop.com/richardb/weblog
> >
> >
>
 nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/>
> >
> > Hello Nicholas,
> >
> > Thanx for the quick reply
> >
> > Yes..i had a logging mechanism (i write to the eventviewer) in which i
> write
> > "Before calling method" before the "Invoke" call and
> > "After calling method" immediately after the line. In times of these
> hangs,
> > i only see the "Before calling method" and not the one which
> > gets printed after calling the method. So, i concluded that the control
> is
> > not passing from the line in which i call the invoke.
> >
> > FYI, this happens for any given method in the dll (not for any specific
> > method). So, i doubt whether the problem could be with the dll.
> > Also, i've tested the dll by other means and the code is working
 alright.
> >
> > Thanx,
> > Ananth
> >
> > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com>
 wrote
> in
> > message news:eMn8nY$mEHA.648@tk2msftngp13.phx.gbl...
> > > Ananth,
> > >
> > > Have you tried any logging code, both in this routine, and the
 routine
> > > you are trying to dynamically invoke? Also, why not just debug it? If
> > you
> > > have the project loaded, and the debug build in the same directory
 (you
> > can
> > > copy it yourself), you should be able to step through it and see what
> is
> > > going on. It looks like the problem is in the code being invoked
> > > dynamically, but without seeing that, it's hard to tell.
> > >
> > > Hope this helps.
> > >
> > >
> > > --
> > > - Nicholas Paldino [.NET/C# MVP]
> > > - mvp@spam.guard.caspershouse.com
> > >
> > > "Ananth" <Ananth@discussions.microsoft.com> wrote in message
> > > news:9E75C976-174D-4902-80CC-87F121235557@microsoft.com...
> > > > Hello group,
> > > >
> > > > Here is the scenario...
> > > >
> > > > There is a .dll (written in C#) available in the same folder as my
> > > > application.I'm using reflection to dynamically load the assembly
 and
> > call
> > > > the methods available in that dll.
> > > >
> > > > However, the system hangs when it is trying to call the methods in
> that
> > > > dll.
> > > > FYI, i'm able to successfully create an object for the dll's class.
> The
> > > > problem is in the line i call the "Invoke" method. That is, the
> control
> > is
> > > > NOT passing to the next line after "Invoke" and it remains in the
> same
> > > > line
> > > > for an infinite amount of time. I've attached the method (at the
 end
> of
> > > > this
> > > > post) for your reference.
> > > >
> > > > Any suggestions would be greatly appreciated.
> > > >
> > > > Thanx,
> > > > Ananth
> > > >
> > >
> >
>
>
 
> --------------------------------------------------------------------------
> > ----
> > > > //Method starts here
> > > > private string CallFunction(string assemblyPath, string className,
> > string
> > > > methodName, string[] param)
> > > > {
> > > > try
> > > > {
> > > > if (!
> System.IO.File.Exists(System.Windows.Forms.Application.StartupPath
> > +
> > > > "\\" + assemblyPath))
> > > > {
> > > > return "The dll is not found in the installation directory.";
> > > > }
> > > > else
> > > > {
> > > > assemblyPath = System.Windows.Forms.Application.StartupPath + "\\"
 +
> > > > assemblyPath;
> > > > }
> > > >
> > > > Assembly objAssembly = Assembly.LoadFrom(assemblyPath);
> > > >
> > > > if (objAssembly == null)
> > > > return "";
> > > >
> > > > AssemblyName objAssemblyName = objAssembly.GetName();
> > > > string strAssemblyShortName = "" + objAssemblyName.Name;
> > > > objAssemblyName = null;
> > > > Type objType = objAssembly.GetType(strAssemblyShortName + "." +
> > > > className);
> > > >
> > > > object o = Activator.CreateInstance(objType);
> > > >
> > > > int ct = param.Length;
> > > > object[] paramObj = new object[ct];
> > > >
> > > > for (int i=0;i<ct;i++)
> > > > {
> > > > paramObj[i] = (object)param[i];
> > > > }//for i
> > > >
> > > > string sResult = string.Empty;
> > > >
> > > > MethodInfo[] arrMethods = objType.GetMethods();
> > > >
> > > > foreach (MethodInfo objMethod in arrMethods)
> > > > {
> > > > if (objMethod.Name == methodName)
> > > > {
> > > > MethodInfo t = objType.GetMethod(methodName);
> > > > sResult = "" + t.Invoke(o, paramObj).ToString();
> > > > return sResult;
> > > > }
> > > > }
> > > > }
> > > > catch(TargetInvocationException tex)
> > > > {
> > > > string strMsg = "Error occured while calling " + className + "." +
> > > > methodName;
> > > > }
> > > > catch(Exception ex)
> > > > {
> > > > string strMsg = "Error occured while calling " + className + "." +
> > > > methodName;
> > > > }
> > > >
> > > > return "";
> > > > }
> > > > //Method ends here
> > >
> >
>
>
 
> --------------------------------------------------------------------------
> > ---
> > > >
> > >
> > >
> >
> >
> >
> > ---
> > Incoming mail is certified Virus Free.
> > Checked by AVG anti-virus system (
http://www.grisoft.com).
> > Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004
> >
> >
> >
> > [microsoft.public.dotnet.languages.csharp]
>
>
>
> ---
> Incoming mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004
>
>
>
> [microsoft.public.dotnet.languages.csharp]
 
 
 
 ---
 Incoming mail is certified Virus Free.
 Checked by AVG anti-virus system (http://www.grisoft.com).
 Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

 
 [microsoft.public.dotnet.languages.csharp]



Relevant Pages