Re: Reflection problem

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

From: Ananth RK (ananthrk_at_fifthgentech.com)
Date: 09/16/04


Date: Fri, 17 Sep 2004 00:08:21 +0530

to answer Richard's question...

yes, I'm pretty sure that there are no exceptions being thrown while
creating an object for assembly or type.

could it be a permission issue in Win 2003 ?? (becoz the same functionality
works well in Win2000)

Thanx,
Ananth

"Richard Blewett [DevelopMentor]" <richardb@develop.com> wrote in message
news:OwjRC3AnEHA.1248@TK2MSFTNGP09.phx.gbl...
> 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

  • Re: Configuration Error Not Helpful
    ... >What kind of error in the configuration section causes the exception to be ... schema validation, and any error that gets thrown from that gets this ... those exceptions never got displayed. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: sending over DO seems to make main process block?
    ... DO is one of the few places in Cocoa where exceptions are thrown that ... an exception may be thrown when the ... avoid deadlock. ... the sender expects to be called back from the receiver, ...
    (comp.sys.mac.programmer.help)
  • Re: How much processor resources do try-catch blocks in C# use
    ... You are right...if no exceptions are thrown, ... Marc Butenko ... > I was discussing with someone how much processor resources try-catch ...
    (microsoft.public.vsnet.general)
  • Re: throw in a finally
    ... > other well-documented texts available that point out this problem. ... > that the reasons behind it are the same as the reasons behind why ... Catch exceptions thrown in a finally even if the method declares they are ... Catch exceptions and just log them. ...
    (comp.lang.java.programmer)