472,791 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 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 2537
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.