Reflection simple problem

N

Nightfall

Dear friends,
consider the following scenario, in Visual Studio 2005 and C#

- I create a ASP.NET web service ("server") with a class MyClass and a
WebMethod SomeMethod()
- I create a client application, using "Add web reference..." and
specifying some web reference name "MyName"

Normally I would do:

MyName.MyClass foo = new MyName.MyClass();
SomeType bar = foo.SomeMethod();

Now, instead, suppose that I have a STRING s = "MyName.MyClass" and
that I want to be able to do the same as above. How can I do?

Please, please provide some code. I know I need reflection and tried
many examples by Googling the Internet, but I can't succeed...
 
M

mpetrotta

Dear friends,
consider the following scenario, in Visual Studio 2005 and C#

- I create a ASP.NET web service ("server") with a class MyClass and a
WebMethod SomeMethod()
- I create a client application, using "Add web reference..." and
specifying some web reference name "MyName"

Normally I would do:

MyName.MyClass foo = new MyName.MyClass();
SomeType bar = foo.SomeMethod();

Now, instead, suppose that I have a STRING s = "MyName.MyClass" and
that I want to be able to do the same as above. How can I do?

Please, please provide some code. I know I need reflection and tried
many examples by Googling the Internet, but I can't succeed...

Here's my translation of your code above:

using System;
using System.Reflection;

namespace MyNamespace
{
class MyClass
{
public void SomeMethod()
{
Console.WriteLine("Hello World!");
}
}

class Test
{
static void Main()
{
Type myClassType = Type.GetType("MyNamespace.MyClass");
MethodInfo someMethodInfo = myClassType.GetMethod("SomeMethod");
Object instance = Activator.CreateInstance(myClassType); // create
an instance of MyNamespace.MyClass
someMethodInfo.Invoke(instance, null); // call the SomeMethod
member of the instance

Console.ReadLine();
}
}
}

Reflection takes some time to understand, and there are different ways
to use it (I could have, for instance, created a ConstructorInfo from
myClassType, and called Invoke on it, to create an instance of the
type). Most of the magic starts with the Type class; I'd suggest
reading through the MSDN documentation as a starting point.

Michael
 
N

Nightfall

Here's my translation of your code above:

using System;
using System.Reflection;

namespace MyNamespace
{
class MyClass
{
public void SomeMethod()
{
Console.WriteLine("Hello World!");
}
}

class Test
{
static void Main()
{
Type myClassType = Type.GetType("MyNamespace.MyClass");
MethodInfo someMethodInfo = myClassType.GetMethod("SomeMethod");
Object instance = Activator.CreateInstance(myClassType); // create
an instance of MyNamespace.MyClass
someMethodInfo.Invoke(instance, null); // call the SomeMethod
member of the instance

Console.ReadLine();
}
}

}

Reflection takes some time to understand, and there are different ways
to use it (I could have, for instance, created a ConstructorInfo from
myClassType, and called Invoke on it, to create an instance of the
type). Most of the magic starts with the Type class; I'd suggest
reading through the MSDN documentation as a starting point.

Michael- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -

Thanks a lot for your help. Sorry to say it doesn't work...:-(

This works:
MyNameSpace.MyClass foo = new MyNameSpace.MyClass();
string s = foo.GetString();

This doesn't work:
Type myClassType = Type.GetType("MyNameSpace.MyClass");
MethodInfo someMethodInfo =
myClassType.GetMethod("GetString");
Object instance = Activator.CreateInstance(myClassType);
string = (string)someMethodInfo.Invoke(instance, null);

I'm not sure about the cast in the last line (maybe I should
Convert.ToString...?) but anyway, I commented out one line per time
starting from the bottom, and I don't get runtime error only if I
leave just Type myClassType = ... (1st line).

The runtime error I get is Error 500 in the browser (internal server
error).

Any hint?

Thanx again, best regards
 
C

Chris Shepherd

Thanks a lot for your help. Sorry to say it doesn't work...:-(

It works fine here.
This works:
MyNameSpace.MyClass foo = new MyNameSpace.MyClass();
string s = foo.GetString();

This doesn't work:
Type myClassType = Type.GetType("MyNameSpace.MyClass");
MethodInfo someMethodInfo =
myClassType.GetMethod("GetString");
Object instance = Activator.CreateInstance(myClassType);
string = (string)someMethodInfo.Invoke(instance, null);

I'm not sure about the cast in the last line (maybe I should
Convert.ToString...?) but anyway, I commented out one line per time
starting from the bottom, and I don't get runtime error only if I
leave just Type myClassType = ... (1st line).

First of all, your string = (string) line is flawed, since you don't
have a variable you're assigning it to.

The MethodInfo line just above works for me using Object and ToString.

Here's an example:

Type myType = Type.GetType("System.Object");
MethodInfo someInfo = myType.GetMethod("ToString");
Object instance = Activator.CreateInstance(myType);
string s = someInfo.Invoke(instance, null).ToString();


Chris
 
M

mpetrotta

Thanks a lot for your help. Sorry to say it doesn't work...:-(
This works:
MyNameSpace.MyClass foo = new MyNameSpace.MyClass();
string s = foo.GetString();

This doesn't work:
Type myClassType = Type.GetType("MyNameSpace.MyClass");
MethodInfo someMethodInfo =
myClassType.GetMethod("GetString");
Object instance = Activator.CreateInstance(myClassType);
string = (string)someMethodInfo.Invoke(instance, null);

I'm not sure about the cast in the last line (maybe I should
Convert.ToString...?) but anyway, I commented out one line per time
starting from the bottom, and I don't get runtime error only if I
leave just Type myClassType = ... (1st line).

The runtime error I get is Error 500 in the browser (internal server
error).

Any hint?

You're making things much, much too hard on yourself, if that's how
you're debugging. I suggest setting ASP.NET aside for the moment, and
concentrate on reflection. When you've got that working, then you can
integrate it into your web app.

Take my original code, which runs just fine, and create a console
application with it. Run it, then adapt it for your classes. Get it
to work as a console app. Then, adapt it for your web app.

You also need to get ASP.NET to give you better error reporting. I
don't do much web development, but the ASP.NET apps I've put together
will report the actual exception, with a stack trace, when I get an
error. If you're not connecting from the same machine that IIS is
running on, then you'll need to modify your web.config's customErrors
line:<customErrors mode="Off" />

If you run into a problem at some point, post a complete program that
reproduces the problem (I should be able to copy and paste into a new
project, and run it). This takes some effort, but often in the
process of simplifying your code for posting, you'll have a Eureka
moment and realize what the problem is.

Good luck,

Michael
 
A

almir

Hi,

I had a similar problem with asp.net, so to awoid this problem try
your code first in a simple windows or comand-prompt application, it
will take you only few lines of code, if it works there, which should
be easy if you follow the steps abowe try it in asp.net. there it will
probably not work the problem is in asp.net compiler which compiles
each file or directory in asp.net in a differen assembly, thus whan
you start searching for class with a given name you get null as return
as the class you are looking for is in a different assembly, to avoid
this problem get a reference to a clas with
typeof(MyClassFromWebReference).GetAssembly(); than with the reference
to this assembly try finding class with given name, this should work
than, if not drop me a mail with your code i ll try to send you a
working reply.

eventually try activator.getinstance -or somthing like that maybe this
will give you better results


good luck
almir
 
N

Nightfall

Hi,

I had a similar problem with asp.net, so to awoid this problem try
your code first in a simple windows or comand-prompt application, it
will take you only few lines of code, if it works there, which should
be easy if you follow the steps abowe try it in asp.net. there it will
probably not work the problem is in asp.net compiler which compiles
each file or directory in asp.net in a differen assembly, thus whan
you start searching for class with a given name you get null as return
as the class you are looking for is in a different assembly, to avoid
this problem get a reference to a clas with
typeof(MyClassFromWebReference).GetAssembly(); than with the reference
to this assembly try finding class with given name, this should work
than, if not drop me a mail with your code i ll try to send you a
working reply.

eventually try activator.getinstance -or somthing like that maybe this
will give you better results

good luck
almir










- Mostra testo tra virgolette -

So... almir you got there...
I wrote an email to you, but I explain what's happening here, too.

In my CLIENT code I wrote

Type myClassType = Type.GetType("SomeName.WSServer");
MethodInfo someMethodInfo =
myClassType.GetMethod("HelloWorld");
Object instance = Activator.CreateInstance(myClassType);
string s = someMethodInfo.Invoke(instance,
null).ToString();

SomeName.WSServer is the NameSpace.ClassName for a class provided via
"Add web reference..." to my client.
In this case, I get a null reference back, and the application hangs.

If I write instead
Type myClassType = typeof(SomeName.WSServer);
everything works fine.

Everything works fine (obviously, I suppose) even if SomeName.WSServer
is NOT a Web Reference, but a "local" class, that is, a class inside
the same project as the client.

So I guess the problem is just in invoking Type.GetType with a Web
reference as argument, and this is really too hard to understand for
me :-(((

Bye bye
 
N

Nightfall

So... almir you got there...
I wrote an email to you, but I explain what's happening here, too.

In my CLIENT code I wrote

Type myClassType = Type.GetType("SomeName.WSServer");
MethodInfo someMethodInfo =
myClassType.GetMethod("HelloWorld");
Object instance = Activator.CreateInstance(myClassType);
string s = someMethodInfo.Invoke(instance,
null).ToString();

SomeName.WSServer is the NameSpace.ClassName for a class provided via
"Add web reference..." to my client.
In this case, I get a null reference back, and the application hangs.

If I write instead
Type myClassType = typeof(SomeName.WSServer);
everything works fine.

Everything works fine (obviously, I suppose) even if SomeName.WSServer
is NOT a Web Reference, but a "local" class, that is, a class inside
the same project as the client.

So I guess the problem is just in invoking Type.GetType with a Web
reference as argument, and this is really too hard to understand for
me :-(((

Bye bye- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -

Well... it seems I found the solution:

instead of Type myClassType = Type.GetType...
I'm using

Type myClassType =
System.Web.Compilation.BuildManager.GetType("SomeName.WSServer",
true);

and everything is working fine :)))

Thanx a lot to everybody anyway.

Bye!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top