By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,864 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,864 IT Pros & Developers. It's quick & easy.

Reflection simple problem

P: n/a
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...

Sep 11 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Sep 11 '07 #2

P: n/a
On Sep 11, 8:04 am, Nightfall <chesbo...@gmail.comwrote:
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
Sep 11 '07 #3

P: n/a
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

On Sep 11, 6:06 pm, "mpetro...@gmail.com" <mpetro...@gmail.comwrote:
On Sep 11, 8:04 am, Nightfall <chesbo...@gmail.comwrote:


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- Hide quoted text -

- Show quoted text -

Sep 11 '07 #4

P: n/a
On 12 Set, 00:24, almir <kaza...@gmail.comwrote:
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

On Sep 11, 6:06 pm, "mpetro...@gmail.com" <mpetro...@gmail.comwrote:
On Sep 11, 8:04 am, Nightfall <chesbo...@gmail.comwrote:
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- Hide quoted text -
- Show quoted text -- Nascondi testo tra virgolette -

- 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

Sep 12 '07 #5

P: n/a
On 12 Set, 11:07, Nightfall <chesbo...@gmail.comwrote:
On 12 Set, 00:24, almir <kaza...@gmail.comwrote:


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
On Sep 11, 6:06 pm, "mpetro...@gmail.com" <mpetro...@gmail.comwrote:
On Sep 11, 8:04 am, Nightfall <chesbo...@gmail.comwrote:
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- Hide quoted text -
- Show quoted text -- Nascondi testo tra virgolette -
- 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- 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("SomeN ame.WSServer",
true);

and everything is working fine :-)))

Thanx a lot to everybody anyway.

Bye!

Sep 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.