M
Mike Malter
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 );
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 );