我正在重组 opur 代码以使用泛型。我们确实使用(出于必要,此处无法更改)自己的代码来附加/分离/迭代/调用委托(delegate)。在有 X、Y、Z 类声明它们自己之前:
public delegate void Event_x_Delegate(ref ComParam pVal, out bool result);
目前我正在将这个委托(delegate)传递给泛型类:
public sealed class Event_X_Handling : BasicEvent<Event_X_Handling.Event_x_Delegate>
BasicEvent 正在运行
public abstract class BasicEvent<DELEGATE> : Loggable
where DELEGATE: class { ...
这工作得很好,所以我通用了附加/分离功能。
但现在我也想概括迭代/调用。由于 X、Y、Z 仅在“ref ComParam pVal”中有所不同,这样的东西很好用:
public abstract class BasicEventListener<EVENTPARAM1> :
BasicEvent<BasicEventListener<EVENTPARAM1>.BasicDelegate<EVENTPARAM1>> {
#region TYPES
public delegate void BasicDelegate<PARAM1>(ref EVENTPARAM1 pVal, out bool result);
#endregion
X、Y、Z 变为:
public sealed class Event_X_Handling : BasicEventListener<ComParam>
但是潜伏在角落的问题来了:附加/分离现在与 BasicEventListener.BasicDelegate 一起工作。但是代码很多。指的是 Event_X_Handling.Event_x_Delegate,因为他们不幸地使用 NET1.1 语法来创建委托(delegate)(蜂鸣类型 Event += new Delegate_X(_listen))。
简而言之:有没有办法将 Event_x_Delegate 别名为 BasicDelegate?我真的看不到任何其他可能性。
PS:当然,我看到通过将基类分解为迭代/调用中的动态调用,我可以在不引入 BasicDelegate 的情况下实现它。但这不是很优雅恕我直言。
最佳答案
也许 using
别名适合您?
using System;
using IntFunc = System.Func<int>;
namespace DelegateAlias
{
class Program
{
static int foo() { return 314; }
static void Main(string[] args)
{
IntFunc f = foo;
}
}
}
关于c# - 是否可以从委托(delegate)中别名/引用委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/654987/