473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Beginner question about Reflection & code snippet.

I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I
can call that method again later using reflection.

I am experimenting a bit with what I need to do this and have the following code snippet. But first if I pass the assembly name and
type to Activator.CreateInstance() it always fails. However if I walk my assembly and get a type value, the call to
Activator.CreateInstance() will always succeed.

My next question, is why can't I cast the return value from the Invoke() call to a DataSet. I thought it returned an object. Am I
missing something? The test method returns a DataSet.

Finally, I got some advice to use Type.InvokeMember() in this forum, but I had to unwrap the return and then needed to use typed
values to complete the call and I want to be able to build this entirely from strings from a database.

If my approach is way off, could someone please point me in the right direction?

Thanks. My code snippet is below.

MethodInfo myMethodEX = null;
Type myTypeEX = null;
Assembly assembly1 = Assembly.Load( "Ripple.Home.BusinessLayer" );
Type[] types = assembly1.GetTypes();
foreach( Type definedType in types )
{
sClass = definedType.Name;
if ( sClass == "Role" )
{
myTypeEX = definedType;
MethodInfo[] myMethod = definedType.GetMethods();
foreach( MethodInfo nextMethod in myMethod )
{
sMethod = nextMethod.Name;
if ( sMethod == "ListAll" )
{
myMethodEX = nextMethod;
break;
}
}
}
if ( sClass == "Role" )
{
break;
}
}

// This always succeeds
Object obj = Activator.CreateInstance( myTypeEX );

// This ALWAYS Fails
// Object obj = Activator.CreateInstance( "Ripple.Home.BusinessLayer", "Role" );

// The cast fails. How do I get my return value?
DataSet ds = (DataSet) myMethodEX.Invoke( obj, null );

Nov 15 '05 #1
1 2584
Jeffrey,

Thanks for your insight. My problem with Activator.CreateInstance() was that I was not using the full name in the second parameter.

I have an additional question that I could use your insight into. My approach now involves walking an entire assembly to get a
MethodInfo variable to do an Invoke() on. Is there a better, or more efficient way to use reflection to call a specific method in
an assembly if you already know the assembly name, the type and the method? I will be storing these values as strings in a database
log. Someone told me to use Type.InvokeMember(). How does that figure in to all of this?

Finally, if this is the best way to do this, can you give me a small example of how to format the parameter[] for the second
parameter of Invoke()? I will be wanting to call methods with parameters eventually.

Thanks.

Mike Malter
"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message news:GE**************@cpmsftngxa06.phx.gbl...

Hi Mike,

The Activator.CreateInstance(assemblyname,typename) function return
ObjectHandle type which wrapped the
object type.
So you should use UnWrap() method to get the object that was wrapped.
Also, I did not get any error on casting object to dataset.

Sample code listed below:

using System;
using System.Reflection;
using System.Runtime.Remoting ;
using System.Data ;

namespace reflectiontest
{
public class Role
{
public DataSet ListAll()
{
Console.WriteLine ("Role.ListAll()");
return new DataSet ();
}
}
class Class1
{

[STAThread]
static void Main(string[] args)
{
try
{
string sClass;
string sMethod;
MethodInfo myMethodEX = null;
Type myTypeEX = null;
Assembly assembly1 = Assembly.Load( "reflectiontest" );
Type[] types = assembly1.GetTypes();
foreach( Type definedType in types )
{
sClass = definedType.Name;
if ( sClass == "Role" )
{
myTypeEX = definedType;
MethodInfo[] myMethod = definedType.GetMethods();
foreach( MethodInfo nextMethod in myMethod )
{
sMethod = nextMethod.Name;
if ( sMethod == "ListAll" )
{
myMethodEX = nextMethod;
break;
}
}
}
if ( sClass == "Role" )
{
break;
}
}

//Object obj1= Activator.CreateInstance( myTypeEX );
ObjectHandle obj2 = Activator.CreateInstance(
"reflectiontest","reflectiontest.Role" );
Object obj3=obj2.Unwrap();

DataSet ds=(DataSet)myMethodEX.Invoke( obj3, null );//no error generate

}
catch(Exception e)
{
Console.WriteLine (e.Message );
}
Console.Read ();
}
}
}

If you have anything unclear, please feel free to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Mike Malter" <mi********@nospam.com>
| Subject: Beginner question about Reflection & code snippet.
| Date: Mon, 25 Aug 2003 17:30:06 -0700
| Lines: 55
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <O0**************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: adsl-64-175-22-129.dsl.snfc21.pacbell.net 64.175.22.129
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:179292
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I am just starting to work with reflection and I want to create a log
that saves relevant information if a method call fails so I
| can call that method again later using reflection.
|
| I am experimenting a bit with what I need to do this and have the
following code snippet. But first if I pass the assembly name and
| type to Activator.CreateInstance() it always fails. However if I walk my
assembly and get a type value, the call to
| Activator.CreateInstance() will always succeed.
|
| My next question, is why can't I cast the return value from the Invoke()
call to a DataSet. I thought it returned an object. Am I
| missing something? The test method returns a DataSet.
|
| Finally, I got some advice to use Type.InvokeMember() in this forum, but
I had to unwrap the return and then needed to use typed
| values to complete the call and I want to be able to build this entirely
from strings from a database.
|
| If my approach is way off, could someone please point me in the right
direction?
|
| Thanks. My code snippet is below.
|
| MethodInfo myMethodEX = null;
| Type myTypeEX = null;
| Assembly assembly1 = Assembly.Load( "Ripple.Home.BusinessLayer" );
| Type[] types = assembly1.GetTypes();
| foreach( Type definedType in types )
| {
| sClass = definedType.Name;
| if ( sClass == "Role" )
| {
| myTypeEX = definedType;
| MethodInfo[] myMethod = definedType.GetMethods();
| foreach( MethodInfo nextMethod in myMethod )
| {
| sMethod = nextMethod.Name;
| if ( sMethod == "ListAll" )
| {
| myMethodEX = nextMethod;
| break;
| }
| }
| }
| if ( sClass == "Role" )
| {
| break;
| }
| }
|
| // This always succeeds
| Object obj = Activator.CreateInstance( myTypeEX );
|
| // This ALWAYS Fails
| // Object obj = Activator.CreateInstance( "Ripple.Home.BusinessLayer",
"Role" );
|
| // The cast fails. How do I get my return value?
| DataSet ds = (DataSet) myMethodEX.Invoke( obj, null );
|
|
|
|

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
6
by: Laser Lu | last post by:
HI, all, I just want to invoke an internal method named 'ResolveClientUrl', which is defined in class System.Web.UI.Control, using an instance object of a type that derives from Control. Let's...
2
by: Carl Rosenberger | last post by:
Hi all, I would like to create an instance of a struct with reflection. The way I am used to doing this with classes: - get a ConstructorInfo from the Type - call Invoke() However, if a...
2
by: Jim Bancroft | last post by:
Hi all, I'm writing an exception handler for one of my VB.Net methods and wondered how best to dynamically put the class and method name in my message string. My code looks like this...
2
by: Miro | last post by:
I am a pure beginner and reading 3 books at the same time trying to learn vb. I have created 2 forms in my "Solution" No where can I see in any of the books how you load / call one form from the...
7
by: Paul Hadfield | last post by:
Hi, I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system...
1
by: =?Utf-8?B?VGltIFBhY2w=?= | last post by:
I am new to Reflection. I am building a windows app that navigates the control tree of a Windows form and creates XAML representing the form. I am using reflection to get the properties of each...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
2
by: nicholas08 | last post by:
I am new to programming and trying to work on a simple console app. I am making a simple Library Management system where the user/admin can manage members and item. Attaching the menu so it's clear...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.