T
Tim Werth
I am trying to use reflection to add an event handler for the RowUpdated
event of the OracleDataAdapter object (ODP.NET), but the same thing can be
said for SqlDataAdapter if you only use reflection. The code I supplied is
based on the SqlDataAdapter with reflection.
The error occurs when trying to create the delegate that will be passed in
to EventInfo.AddEventHandler. I get the following error:
An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Error binding to target method.
The binding problem is with the second parameter to the event handler. It
is of type SqlRowUpdatedEventArgs. Again, in reality, I am trying to use
ODP.NET via reflection. So, in this example, I am not referencing the
System.Data or System.Data.Client namespace directly.
Without referencing the namespace, how would one declare a delegate's method
that has a parameter type that exists in a namespace not being referenced
(does that make sense?)?
I have included some sample code to illustrate the problem. The first
CreateDelegate will succeed with the using System.Data* lines *not*
commented out because you can use the SqlRowUpdatedEventArgs parameter. If
you comment out the "using System.Data" and "using System.Data.SqlClient"
lines, how do you create the delegate?
Again, I am illustrating my problem here using Sql types, because not
everyone has ODP.NET installed.
Tim
using System;
using System.Reflection;
// TAKE THE FOLLOWING TWO LINES OUT TO DEPEND SOLELY ON REFLECTION
using System.Data;
using System.Data.SqlClient;
namespace ReflectionEventTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
CreateSqlDelegate();
}
static public void CreateSqlDelegate()
{
Module systemData = GetSystemDataModule();
Type t = systemData.GetType( "System.Data.SqlClient.SqlDataAdapter" );
object sqlDAObj = Activator.CreateInstance( t );
EventInfo ei = t.GetEvent( "RowUpdated" );
// This one works because the second parameter in the handler is of type
SqlRowUpdatedEventArgs
Delegate d1 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsProperSql" );
// These other three produce the following error:
//
// An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
//
// Additional information: Error binding to target method.
// Causes error
Delegate d2 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsObject" );
// Causes error
Delegate d3 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsBaseClass" );
// Causes error
Delegate d4 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsIntPtr" );
}
// THIS WON'T COMPILE WHEN YOU TAKE THE using System.Data* LINES OUT AT THE
TOP
static private void rowUpdated_ParamIsProperSql(object sender,
SqlRowUpdatedEventArgs e)
{
}
static private void rowUpdated_ParamIsObject(object sender, object
oraRowUpdEventArgs)
{
}
// SqlRowUpdatedEventArgs is derived from
System.Data.Common.RowUpdatedEventArgs
static private void rowUpdated_ParamIsBaseClass(object sender,
System.Data.Common.RowUpdatedEventArgs e)
{
}
static private void rowUpdated_ParamIsIntPtr(object sender, System.IntPtr e)
{
}
static public Module GetSystemDataModule()
{
Assembly assembly = null;
Module module = null;
string strongName = "System.Data, PublicKeyToken=b77a5c561934e089,
Culture=neutral, Version=1.0.5000.0";
// Load the assembly
assembly = Assembly.Load( strongName );
// Get the module
module = assembly.GetModule( "System.Data.dll" );
return module;
}
}
}
event of the OracleDataAdapter object (ODP.NET), but the same thing can be
said for SqlDataAdapter if you only use reflection. The code I supplied is
based on the SqlDataAdapter with reflection.
The error occurs when trying to create the delegate that will be passed in
to EventInfo.AddEventHandler. I get the following error:
An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Error binding to target method.
The binding problem is with the second parameter to the event handler. It
is of type SqlRowUpdatedEventArgs. Again, in reality, I am trying to use
ODP.NET via reflection. So, in this example, I am not referencing the
System.Data or System.Data.Client namespace directly.
Without referencing the namespace, how would one declare a delegate's method
that has a parameter type that exists in a namespace not being referenced
(does that make sense?)?
I have included some sample code to illustrate the problem. The first
CreateDelegate will succeed with the using System.Data* lines *not*
commented out because you can use the SqlRowUpdatedEventArgs parameter. If
you comment out the "using System.Data" and "using System.Data.SqlClient"
lines, how do you create the delegate?
Again, I am illustrating my problem here using Sql types, because not
everyone has ODP.NET installed.
Tim
using System;
using System.Reflection;
// TAKE THE FOLLOWING TWO LINES OUT TO DEPEND SOLELY ON REFLECTION
using System.Data;
using System.Data.SqlClient;
namespace ReflectionEventTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
CreateSqlDelegate();
}
static public void CreateSqlDelegate()
{
Module systemData = GetSystemDataModule();
Type t = systemData.GetType( "System.Data.SqlClient.SqlDataAdapter" );
object sqlDAObj = Activator.CreateInstance( t );
EventInfo ei = t.GetEvent( "RowUpdated" );
// This one works because the second parameter in the handler is of type
SqlRowUpdatedEventArgs
Delegate d1 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsProperSql" );
// These other three produce the following error:
//
// An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
//
// Additional information: Error binding to target method.
// Causes error
Delegate d2 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsObject" );
// Causes error
Delegate d3 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsBaseClass" );
// Causes error
Delegate d4 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsIntPtr" );
}
// THIS WON'T COMPILE WHEN YOU TAKE THE using System.Data* LINES OUT AT THE
TOP
static private void rowUpdated_ParamIsProperSql(object sender,
SqlRowUpdatedEventArgs e)
{
}
static private void rowUpdated_ParamIsObject(object sender, object
oraRowUpdEventArgs)
{
}
// SqlRowUpdatedEventArgs is derived from
System.Data.Common.RowUpdatedEventArgs
static private void rowUpdated_ParamIsBaseClass(object sender,
System.Data.Common.RowUpdatedEventArgs e)
{
}
static private void rowUpdated_ParamIsIntPtr(object sender, System.IntPtr e)
{
}
static public Module GetSystemDataModule()
{
Assembly assembly = null;
Module module = null;
string strongName = "System.Data, PublicKeyToken=b77a5c561934e089,
Culture=neutral, Version=1.0.5000.0";
// Load the assembly
assembly = Assembly.Load( strongName );
// Get the module
module = assembly.GetModule( "System.Data.dll" );
return module;
}
}
}