期待像代表一样...... [英] Looking to make something like a delegate...

查看:99
本文介绍了期待像代表一样......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在我的

项目中使用类似代表的东西,但我很难开始使用。


行为我最感兴趣的是委托如何更改它'

动态调用方法以匹配定义时提供给它的参数

。例如......

public delegate void MyDelegate(string myString,int myInt);


MyDelegate myDel = new MyDelegate(my.Target);

当我查看myDel的Invoke方法时,你会看到...


myDel.Invoke(string myString,int myInt);


我最终要做的是覆盖

委托的Invoke方法,但由于Delegate类被锁定,我不能只做

它很简单。


我的最终目标是能够将方法调用作为

参数传递而不调用它。作为一个例子...


...使用上面的代表...

MyAssembly.MyClass cls = new MyAssembly.MyClass( );

MyDelegate myDel = new MyDelegate(cls.targetMethod);

MyAssembly.MyClass2 cls2 = new MyAssembly.MyClass2();

cls2 .MyMethod(myDel(你多大了?,0));


当这个运行时,cls2上的MyMethod将使用反射确定

委托的来源,提取参数值,并调用

方法。这就是我想要做的。


即使理解/了解委托定义如何允许定义可变数量的参数,并且然后转身并且

在委托上创建一个匹配这些参数的Invoke方法

将会提供很多帮助。


非常感谢任何帮助,建议,替代方案。

I am looking to make something like a delegate that i can use in my
projects but am having a hard time getting started.

The behavior I am most interested in is how a delegate changes it''s
Invoke method dynamically to match the arguments supplied to it when it
is defined. For example...
public delegate void MyDelegate(string myString, int myInt);

MyDelegate myDel = new MyDelegate(my.Target);
When I look at the Invoke method for myDel, you see...

myDel.Invoke(string myString, int myInt);

What I am ultimately looking to do is override the Invoke method of a
delegate but being that the Delegate class is locked, I can''t just do
it the easy way.

My ultimate goal is to be able to pass a method invocation as a
parameter without it being invoked. As an example...

... using the delegate above...

MyAssembly.MyClass cls = new MyAssembly.MyClass();
MyDelegate myDel = new MyDelegate(cls.targetMethod);
MyAssembly.MyClass2 cls2 = new MyAssembly.MyClass2();
cls2.MyMethod(myDel("How old are you?", 0));

When this is run, the MyMethod on cls2 will use reflection to determine
the origin of the delegate, extract the parameter values, and invoke
the method. This is what I would like to do.

Even to understand/learn how a delegate definition allows for a
variable number of parameters to be defined, and then turn around and
create an Invoke method on the delegate that matches these parameters
would be a great deal of help.

Any help, suggestions, alternatives would be greatly appreciated.

推荐答案

ma ******* @ gmail.com 写道:
ma*******@gmail.com wrote:

我的最终目标是能够将方法调用作为

参数传递,而不调用它。作为一个例子...


...使用上面的代表...

MyAssembly.MyClass cls = new MyAssembly.MyClass( );

MyDelegate myDel = new MyDelegate(cls.targetMethod);

MyAssembly.MyClass2 cls2 = new MyAssembly.MyClass2();

cls2 .MyMethod(myDel(你多大了?,0));


当这个运行时,cls2上的MyMethod将使用反射确定

委托的来源,提取参数值,并调用

方法。这就是我想做的。
My ultimate goal is to be able to pass a method invocation as a
parameter without it being invoked. As an example...

... using the delegate above...

MyAssembly.MyClass cls = new MyAssembly.MyClass();
MyDelegate myDel = new MyDelegate(cls.targetMethod);
MyAssembly.MyClass2 cls2 = new MyAssembly.MyClass2();
cls2.MyMethod(myDel("How old are you?", 0));

When this is run, the MyMethod on cls2 will use reflection to determine
the origin of the delegate, extract the parameter values, and invoke
the method. This is what I would like to do.



也许我错过了什么 - 你为什么不写cls2.MyMethod

来获取MyDelegate参数?


-


..NET 2.0 for Delphi程序员
www.midnightbeach.com/.net





当你声明一个委托时,编译器实际上会生成一个新的类,即
继承自MulticastDelegate类(继承自Delegate)和

声明了Invoke带有所需签名的方法。


如果锁定你的意思是密封然后没有,委托和MulticastDelegate

或编译器生成的类都没有被密封。唯一的想法是你不能使用它们作为基类,因为编译器不允许你这样做。

编译器只允许你使用它的语法宣布代表。在

另一方面,对于更多可配置的代表,您可以使用泛型 -

通用代表。


有关详细信息,请查看:
http:// msdn2 .microsoft.com / zh-CN / lib ... w7(VS.80).aspx

-

HTH

Stoitcho Goutsev(100)

< ma ******* @ gmail.comwrote in message

news:11 **** ******************@s34g2000cwa.googlegr oups.com ...
Hi,

when you declare a delegate the compiler actually generates a new class that
inherits from the MulticastDelegate class (which inherits from Delegate) and
declares the Invoke method with desired signature.

If by locked you mean sealed then no, neither Delegate nor MulticastDelegate
or the compiler generated class is not sealed. The only think is that you
cannot use them as a base class because the compiler doesn''t allow you to.
The compiler allows you only to use its syntax for declaring delegates. On
the other hands for more configurable delegates you can use generics -
Generic Delegates.

For more info look at:
http://msdn2.microsoft.com/en-us/lib...w7(VS.80).aspx
--
HTH
Stoitcho Goutsev (100)

<ma*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...

>我期待做一个像我可以在我的

项目中使用的代表,但我很难开始。


我最感兴趣的行为是委托者更改它'

动态调用方法以匹配定义时提供给它的参数

。例如......


public delegate void MyDelegate(string myString,int myInt);


MyDelegate myDel = new MyDelegate(my.Target) );


当我查看myDel的Invoke方法时,你会看到...


myDel.Invoke(string myString,int myInt );


我最终要做的是覆盖

委托的Invoke方法但是由于Delegate类被锁定,我可以''这只是简单的方法。

简单的方法。


我的最终目标是能够将方法调用传递给

没有被调用的参数。作为一个例子...


...使用上面的代表...

MyAssembly.MyClass cls = new MyAssembly.MyClass( );

MyDelegate myDel = new MyDelegate(cls.targetMethod);

MyAssembly.MyClass2 cls2 = new MyAssembly.MyClass2();

cls2 .MyMethod(myDel(你多大了?,0));


当这个运行时,cls2上的MyMethod将使用反射确定

委托的来源,提取参数值,并调用

方法。这就是我想要做的。


即使理解/了解委托定义如何允许定义可变数量的参数,并且然后转身并且

在委托上创建一个匹配这些参数的Invoke方法

将会提供很多帮助。


非常感谢任何帮助,建议和替代方案。
>I am looking to make something like a delegate that i can use in my
projects but am having a hard time getting started.

The behavior I am most interested in is how a delegate changes it''s
Invoke method dynamically to match the arguments supplied to it when it
is defined. For example...
public delegate void MyDelegate(string myString, int myInt);

MyDelegate myDel = new MyDelegate(my.Target);
When I look at the Invoke method for myDel, you see...

myDel.Invoke(string myString, int myInt);

What I am ultimately looking to do is override the Invoke method of a
delegate but being that the Delegate class is locked, I can''t just do
it the easy way.

My ultimate goal is to be able to pass a method invocation as a
parameter without it being invoked. As an example...

... using the delegate above...

MyAssembly.MyClass cls = new MyAssembly.MyClass();
MyDelegate myDel = new MyDelegate(cls.targetMethod);
MyAssembly.MyClass2 cls2 = new MyAssembly.MyClass2();
cls2.MyMethod(myDel("How old are you?", 0));

When this is run, the MyMethod on cls2 will use reflection to determine
the origin of the delegate, extract the parameter values, and invoke
the method. This is what I would like to do.

Even to understand/learn how a delegate definition allows for a
variable number of parameters to be defined, and then turn around and
create an Invoke method on the delegate that matches these parameters
would be a great deal of help.

Any help, suggestions, alternatives would be greatly appreciated.



Jon,


如果我将委托作为参数传递,然后我无法设置委托'

参数值。我还必须传递一个对象数组,

包含所有参数,并希望它们是正确的

顺序,以及它们的正确数量。


喜欢......


cls2.MyMethod(myDel,new object [] {"你多大了?,0});


我的工作正常。但是,你失去了intellisense

的好处,并确保参数输入并正确传递。那是

为什么我希望能写...


cls2.MyMethod(myDel(你多大了?, 0));


我想要做的只是拥有委托功能但是当调用

调用方法时,它会按照我想要的方式执行做而不仅仅是

调用委托指向的方法。


有意义吗?

Jon Shemitz写道:
Jon,

If I pass the delegate as a parameter then I cannot set the delegate''s
parameter values. I would also have to pass an object array that
contains all the parameters and hope that they are in the correct
order, and the correct number of them.

Like...

cls2.MyMethod(myDel, new object[] { "How old are you?", 0 });

I have this working fine. But, you lose the benefit of intellisense
and making sure the parameters are typed and passed properly. That is
why i would like to be able to write...

cls2.MyMethod(myDel("How old are you?", 0));

What i want to do is just have the delegate functionality but when the
Invoke method is called, it does what I want it to do and not just
invoke the method the delegate is pointed to.

Make sense?
Jon Shemitz wrote:
ma*******@gmail.com 写道:
ma*******@gmail.com wrote:

我的最终目标是能够将方法调用作为

参数传递而不调用它。作为一个例子...


...使用上面的代表...

MyAssembly.MyClass cls = new MyAssembly.MyClass( );

MyDelegate myDel = new MyDelegate(cls.targetMethod);

MyAssembly.MyClass2 cls2 = new MyAssembly.MyClass2();

cls2 .MyMethod(myDel(你多大了?,0));


当这个运行时,cls2上的MyMethod将使用反射确定

委托的来源,提取参数值,并调用

方法。这就是我想做的。
My ultimate goal is to be able to pass a method invocation as a
parameter without it being invoked. As an example...

... using the delegate above...

MyAssembly.MyClass cls = new MyAssembly.MyClass();
MyDelegate myDel = new MyDelegate(cls.targetMethod);
MyAssembly.MyClass2 cls2 = new MyAssembly.MyClass2();
cls2.MyMethod(myDel("How old are you?", 0));

When this is run, the MyMethod on cls2 will use reflection to determine
the origin of the delegate, extract the parameter values, and invoke
the method. This is what I would like to do.



也许我错过了什么 - 你为什么不写cls2.MyMethod

来获取MyDelegate参数?


-


适用于Delphi程序员的.NET 2.0
www.midnightbeach.com/.net


这篇关于期待像代表一样......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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