如何通过Reflec将EventHandler或Delegate挂钩到UserControl [英] How to hook up EventHandler or Delegate to UserControl thru Reflec

查看:50
本文介绍了如何通过Reflec将EventHandler或Delegate挂钩到UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个具有各种用户控件的程序集,所有实现都是

一个接口


界面


公共界面ISettings

{

bool SaveSettings();

}

然后我有一个测试pgm将循环通过asssmbly并找到实现ISettings接口的所有类

。然后我在表格上显示所有UserControls



程序集asm = Assembly.LoadFrom(@C:\ myUserContro.dll");

//遍历程序集中的每个类型,查找我们的



foreach(类型类型为asm.GetTypes())

{

if(type.IsClass == true)

{

bClass = false;

foreach(在type.GetInterfaces()中键入interfaceType)

{

if(interfaceType.FullName.Contains(" ISettings"))

{

bClass = true;

休息;

}

}

if(bClass)

{

控制控制= Activator.CreateInstance(类型)

作为控制;

xtraTabControl1.TabPages.Add();


xtraTabControl1.TabPages [xtraTabControl1.TabPages.Count - 1] .Text =

type.FullName ;


xtraTabControl1.TabPages [xtraTabControl1.TabPages.Count-1] .Controls.Add(control);

}

}

这一切都很好

问题出在我创建UserControls的表格上我有一个SAVE

按钮。当我点击SAVE按钮时,我想触发所有界面方法

SaveSettings来自所有UserControls。


最好的方法是什么?


谢谢

Hi

I have an Assembly that has various usercontrols that all implemnet
an Interface

The Interface

public interface ISettings
{
bool SaveSettings();
}
I then have a test pgm that will loop thru the asssmbly and find all classes
that implement the ISettings interface. I then display all the UserControls
on a Form

Assembly asm = Assembly.LoadFrom(@"C:\myUserContro.dll");
// Walk through each type in the assembly looking for our
class
foreach (Type type in asm.GetTypes())
{
if (type.IsClass == true )
{
bClass = false;
foreach (Type interfaceType in type.GetInterfaces())
{
if (interfaceType.FullName.Contains("ISettings"))
{
bClass = true;
break;
}
}

if(bClass)
{
Control control = Activator.CreateInstance(type)
as Control;
xtraTabControl1.TabPages.Add();

xtraTabControl1.TabPages[xtraTabControl1.TabPages.Count - 1].Text =
type.FullName;

xtraTabControl1.TabPages[xtraTabControl1.TabPages.Count-1].Controls.Add(control);
}
}
This is all fine
The question is on the form where I create the UserControls I have a "SAVE"
button. When I hit the SAVE button I want to fire all the interface method
"SaveSettings" from all the UserControls.

What is the best way to do that???

Thanks

推荐答案

2008年9月4日星期四14:21:01 -0700,sippyuconn

< si ******** @ newsgroup.nospamwrote :
On Thu, 04 Sep 2008 14:21:01 -0700, sippyuconn
<si********@newsgroup.nospamwrote:

[...]

问题出在我创建UserControls的表格上我有一个

" SAVE"

按钮。当我点击SAVE按钮时,我想点开所有界面

方法

" SaveSettings"来自所有UserControls。


最好的方法是什么?
[...]
The question is on the form where I create the UserControls I have a
"SAVE"
button. When I hit the SAVE button I want to fire all the interface
method
"SaveSettings" from all the UserControls.

What is the best way to do that???



我不认为反思与问题有关。至于

什么是最好的方式,这不是我们要说的。但是一个选项可能是

来创建委托并为委托添加每个

实现者的SaveSettings()方法。例如:


private Func< boolSaveSettingsHandlers;


//添加UserControl时:

UserControl control = ...;

ISettings设置=(ISettings)控制;


SaveSettingsHandlers + = settings.SaveSettings;


//当你想要保存所有设置时,调用委托:

SaveSettingsHandlers();


当然,它引出的问题是为什么该方法返回一个bool。如果你确实想以某种方式使用它,那么你会想要改变

调用。例如:


foreach(Func< boolhandler in

SaveSettingsHandlers.GetInvocationList())

{

if(!handler())

{

//返回/显示错误,无论

}

}


Pete

I don''t think reflection as anything to do with the question. As far as
what''s the "best way", that''s not for us to say. But one option might be
to create a delegate and add the SaveSettings() method for each
implementor to the delegate. For example:

private Func<boolSaveSettingsHandlers;

// when adding a UserControl:
UserControl control = ...;
ISettings settings = (ISettings)control;

SaveSettingsHandlers += settings.SaveSettings;

// when you want all the settings to be saved, invoke the delegate:
SaveSettingsHandlers();

Of course, it begs the question as to why the method returns a bool. If
you actually want to use that in some way, you''ll want to change the
invocation. For example:

foreach (Func<boolhandler in
SaveSettingsHandlers.GetInvocationList())
{
if (!handler())
{
// return/display error, whatever
}
}

Pete


9月4日,2:41 * pm,Peter Duniho < NpOeStPe ... @nnowslpianmk.com>

写道:
On Sep 4, 2:41*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

2008年9月4日星期四14:21:01 -0700 ,sippyuconn *

我不认为反思与问题有关。 *至于*

什么是最佳方式,这不是我们要说的。 *但是一个选项可能是*

来创建委托并为委托添加每个*

实现者的SaveSettings()方法。 *例如:
On Thu, 04 Sep 2008 14:21:01 -0700, sippyuconn *
I don''t think reflection as anything to do with the question. *As far as *
what''s the "best way", that''s not for us to say. *But one option might be *
to create a delegate and add the SaveSettings() method for each *
implementor to the delegate. *For example:


当然,它引出了为什么该方法返回bool的问题。 *如果*

你实际上想以某种方式使用它,你会想要改变*

调用。 *例如:
Of course, it begs the question as to why the method returns a bool. *If *
you actually want to use that in some way, you''ll want to change the *
invocation. *For example:



是的,我猜我是第二个彼得。我发现代表就像是一个超级GOTO的代表。声明,你可以抛出来调用另一个函数

,它甚至不知道抛出的代码中发生了什么。一个

全球GOTO。


另一种选择,同样糟糕但更容易写,当然是使用

旧全局布尔值的待机(对于命名空间,因为C#没有

真正的全局变量)。更改全局布尔值时,活动代码中某处的if

语句将接管。当然还有

相关的使用枚举和case语句的解决方案,这就是经典Windows的工作方式。


我想但问题是在C#Winforms中代码和数据以及

没有分开。我期待在几个月内学习WPF

并通过分离代码

和数据看看他们如何解决问题。


RL

yeah I guess I second Peter on this. I find that a delegate is like a
super "GOTO" statement, that you can ''throw'' to call another function
that''s not even aware of what''s going on in the code that throws. A
global GOTO.

The other option, equally bad but easier to write, is of course to use
the old standby of a ''global'' boolean (to the namespace, as C# has no
true global variables). When the global boolean is changed, an if
statement somewhere in your active code takes over. And of course the
related solution of using enums and case statements, which is how
classic Windows works.

I think the problem though is that in C# Winforms code and data and
not separated. I''m looking forward to studying WPF in a few months
and seeing how they supposedly solved the problem by separating code
and data.

RL


我提到反射,因为界面是在第三方组装的

我无法访问代码所以我需要使用反射来获取

界面和方法


但是我被困 - 如何使用relection绑定类或类方法to

extract - 然后绑定到Delegate

public delegate bool CustomSaveHandler();

Assembly asm = Assembly.LoadFrom(@" C :\ myassembly.dll");

//遍历程序集中的每个类型,查找我们的



foreach(类型类型) in asm.GetTypes())

{

if(type.IsClass == true)

{

bClass = false;

foreach(在type.GetInterfaces()中键入interfaceType

{

if(interfaceType.FullName.Contains(" ISettings"))

{

bClass = true;

break;

}

}


if(bClass)

{

控制控制=激活器.CreateInstance(类型)

作为控制;

xtraTabControl1.TabPages.Add();


xtraTabControl1.TabPages [xtraTabControl1。 TabPages.Count - 1] .Text =

type.FullName;


xtraTabControl1.TabPages [xtraTabControl1.TabPages.Count-1] .Controls.Add(控制);


MethodInfo typemethod =

type.GetMethod(" SaveSettings");


?? ???


" raylopez99"写道:
I mention reflection because the interface is in a third party assemble that
I don''t have access to code so I needed to use reflection to get at the
interface and Method

But I am stuck - how to tie the Class or Methods of Class using relection to
extract - to then tie to Delegate
public delegate bool CustomSaveHandler();
Assembly asm = Assembly.LoadFrom(@"C:\myassembly.dll");
// Walk through each type in the assembly looking for our
class
foreach (Type type in asm.GetTypes())
{
if (type.IsClass == true )
{
bClass = false;
foreach (Type interfaceType in type.GetInterfaces())
{
if (interfaceType.FullName.Contains("ISettings"))
{
bClass = true;
break;
}
}

if(bClass)
{
Control control = Activator.CreateInstance(type)
as Control;
xtraTabControl1.TabPages.Add();

xtraTabControl1.TabPages[xtraTabControl1.TabPages.Count - 1].Text =
type.FullName;

xtraTabControl1.TabPages[xtraTabControl1.TabPages.Count-1].Controls.Add(control);

MethodInfo typemethod =
type.GetMethod("SaveSettings");

?????

"raylopez99" wrote:

9月4日下午2:41,Peter Duniho < NpOeStPe ... @nnowslpianmk.com>

写道:
On Sep 4, 2:41 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

2008年9月4日星期四14:21:01 -0700 ,sippyuconn

我不认为反思与问题有关。至于

什么是最好的方式,这不是我们要说的。但是一个选项可能是

来创建委托并为委托添加每个

实现者的SaveSettings()方法。例如:
On Thu, 04 Sep 2008 14:21:01 -0700, sippyuconn
I don''t think reflection as anything to do with the question. As far as
what''s the "best way", that''s not for us to say. But one option might be
to create a delegate and add the SaveSettings() method for each
implementor to the delegate. For example:


当然,它引出了为什么该方法返回bool的问题。如果你确实想以某种方式使用它,那么你会想要改变

调用。例如:
Of course, it begs the question as to why the method returns a bool. If
you actually want to use that in some way, you''ll want to change the
invocation. For example:



是的,我想我是第二个彼得。我发现代表就像是一个超级GOTO的代表。声明,你可以抛出来调用另一个函数

,它甚至不知道抛出的代码中发生了什么。一个

全球GOTO。


另一种选择,同样糟糕但更容易写,当然是使用

旧全局布尔值的待机(对于命名空间,因为C#没有

真正的全局变量)。更改全局布尔值时,活动代码中某处的if

语句将接管。当然还有

相关的使用枚举和case语句的解决方案,这就是经典Windows的工作方式。


我想但问题是在C#Winforms中代码和数据以及

没有分开。我期待在几个月内学习WPF

并通过分离代码

和数据看看他们如何解决问题。


RL


yeah I guess I second Peter on this. I find that a delegate is like a
super "GOTO" statement, that you can ''throw'' to call another function
that''s not even aware of what''s going on in the code that throws. A
global GOTO.

The other option, equally bad but easier to write, is of course to use
the old standby of a ''global'' boolean (to the namespace, as C# has no
true global variables). When the global boolean is changed, an if
statement somewhere in your active code takes over. And of course the
related solution of using enums and case statements, which is how
classic Windows works.

I think the problem though is that in C# Winforms code and data and
not separated. I''m looking forward to studying WPF in a few months
and seeing how they supposedly solved the problem by separating code
and data.

RL


这篇关于如何通过Reflec将EventHandler或Delegate挂钩到UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆