错误绑定到C#3.0中的目标方法 [英] Error binding to target method in C#3.0
问题描述
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Xml;
使用System.Data;
使用System.Threading;
使用System.IO;
使用System.Reflection;
使用System.Windows;
命名空间ChartHelper
{
public class ICChartHelper
{
public void RefreshChart()
{
try
{
Assembly myobj = Assembly.LoadFrom(@C:\sample.dll);
foreach(在myobj.GetTypes()中输入mytype)
{
if(mytype.IsClass == true)
{
if(mytype.FullName.EndsWith(。+ICAutomationProxy))
{
//创建一个对象的实例
object ClassObj = Activator.CreateInstance(mytype);
// var eventTypes = mytype.GetEvents();
EventInfo evClick = mytype.GetEvent(OnRefreshCompleted);
类型tDelegate = evClick.EventHandlerType;
MethodInfo miHandler =
typeof(ChartHelper.ICChartHelper)
.GetMethod(RefreshApplication,
BindingFlags.NonPublic | BindingFlags.Instance);
委托d = Delegate.CreateDelegate(tDelegate,typeof(ChartHelper.ICChartHelper),miHandler);
MethodInfo addHandler = evClick.GetAddMethod();
Object [] addHandlerArgs = {d};
addHandler.Invoke(ClassObj,addHandlerArgs);
}
}
}
}
catch(Exception ex)
{
throw ex;
}
}
private void RefreshApplication(Object sender,EventArgs e)
{
MessageBox.Show(Bingo);
}
但在
委托d = Delegate.CreateDelegate(tDelegate,typeof(ChartHelper.ICChartHelper),miHandler);
行,我遇到错误绑定到目标方法的错误
我还发现了这里,并尝试解决相同但没有运气。
我需要帮助来了解我在做什么错?
谢谢
您的方法是一种实例方法,因此您需要使用 CreateDelegate
的重载,它承载委托的目标,并传递一个实例的声明类型。例如:
委托d = Delegate.CreateDelegate(tDelegate,new ICChartHelper(),miHandler);
请注意,您不需要调用 GetAddMethod
在 EventInfo
并调用使用反射 - 你可以使用 EventInfo.AddEventHandler
。
I am trying to hook Up a Delegate Using Reflection. This is what I have done so far
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
using System.Threading;
using System.IO;
using System.Reflection;
using System.Windows;
namespace ChartHelper
{
public class ICChartHelper
{
public void RefreshChart()
{
try
{
Assembly myobj = Assembly.LoadFrom(@"C:\sample.dll");
foreach (Type mytype in myobj.GetTypes())
{
if (mytype.IsClass == true)
{
if (mytype.FullName.EndsWith("." + "ICAutomationProxy"))
{
// create an instance of the object
object ClassObj = Activator.CreateInstance(mytype);
// var eventTypes = mytype.GetEvents();
EventInfo evClick = mytype.GetEvent("OnRefreshCompleted");
Type tDelegate = evClick.EventHandlerType;
MethodInfo miHandler =
typeof(ChartHelper.ICChartHelper)
.GetMethod("RefreshApplication",
BindingFlags.NonPublic | BindingFlags.Instance);
Delegate d = Delegate.CreateDelegate(tDelegate,typeof(ChartHelper.ICChartHelper), miHandler);
MethodInfo addHandler = evClick.GetAddMethod();
Object[] addHandlerArgs = { d };
addHandler.Invoke(ClassObj, addHandlerArgs);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private void RefreshApplication(Object sender, EventArgs e)
{
MessageBox.Show("Bingo");
}
But in the
Delegate d = Delegate.CreateDelegate(tDelegate,typeof(ChartHelper.ICChartHelper), miHandler);
line, I am encountering the error Error binding to target method
I have also found the discusion here and tried to solve the same but with no luck.
I need help to understand what wrong I am doing?
Thanks
Your method is an instance method, so you need to use an overload of CreateDelegate
which takes the target of the delegate, and pass in an instance of the declaring type. For example:
Delegate d = Delegate.CreateDelegate(tDelegate, new ICChartHelper(), miHandler);
Note that you don't need to call GetAddMethod
on the EventInfo
and invoke that using reflection - you can just use EventInfo.AddEventHandler
.
这篇关于错误绑定到C#3.0中的目标方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!