English 中文(简体)
c) 部分 参考活动成员
原标题:c# Using a reference to an event member
  • 时间:2012-01-12 23:25:19
  •  标签:
  • c#
  • events

Anybody know why this is not possible? If the event is just a MulticastDelegate instance you should be able to reference it in the code. the compiler says EventA can only be on left side of -= or +=.

public delegate void MyDelegate();
public event MyDelegate EventA;

public void addHandlerToEvent(MulticastDelegate md,Delegate d){
 md+=d;

}

///
addHandlerToEvent(EventA,new MyDelegate(delegate(){}));
最佳回答

活动不是一个多位代表,就像一个财产不是一个领域一样。

C# s event syntax summarys multicast representatives to make life better by provide syntactic food for Plus and abolition of Handler representatives. 您的活动定义将汇编如下:

private MyDelegate _EventA;

public event MyDelegate EventA
{
    [MethodImpl(MethodImplOptions.Synchronized)]
    add 
    { 
        _EventA = (MyDelegate)Delegate.Combine(_EventA, value);
    }

    [MethodImpl(MethodImplOptions.Synchronized)]
    remove 
    { 
        _EventA = (MyDelegate)Delegate.Remove(_EventA, value);
    }
}

在扩大的活动定义范围内,在您使用<代码>operator +=和operator-=时,请上>>。

This is done in order to hide the internals of the multicast delegate, but expose a simple way to implement publishing/subscribing to events. Being able to get the underlying delegate (outside of the class where it s defined) would break that intentional encapsulation.

问题回答

一项活动是成员,对附加/移除方法(*)加以约束,每个成员都接受与事件签字相应的代表。 否则,C#汇编者将为每次活动自动确定一个多Cast代表领域,同时增加和删除将接任已决代表并增加或从该多广播代谢领域删除的成员。 声明myEvent += someMethod;,在界定活动类别之外进行,基本上是myEvent.AddHandler(一些Method)所需要的合成短程。 活动——=一些Method; for myEvent.RemoveHandler(某种Method),但C#中除了使用+=-=的外,没有其他办法使用添加/移法。

在使用<代码>+=和-=在界定活动类别内的符号时,情况很少见,因为由自动生成的活动代码界定的外地名称与活动名称相同。 IEvent += 方法;将因C#的不同版本而有所不同。

(*) 从技术上讲,由于某项活动还包括一种提款方法,但实际上这种方法基本上从未使用过。

活动得到多播种代表的支持,但活动的目的是提供observer patterns,在这种情况下,观察员(此处的代表)只能登记(+=)或未登记(-=)。 如果在班级本身之外能够正常接触后备代表,那么客户守则便有可能干扰在其他地方登记的无关联代表,而这种代表可能会掩盖事情。 观察other的观测方式,也在一定程度上偏离了观察有关事件的程度。

如果你需要对背书代表进行这种操纵,则必须在该类别内进行(当它作为正式代表而不是事件对待)。

您还可以明确执行支持代表,并为其登记/登记提供便利:

private EventHandler SomeEvent;

public event EventHandler
{
    add
    {
        SomeEvent += value;
    }
    remove
    {
        SomeEvent -= value;
    }
}

This way you can provide direct access to the delegate if you need to. It s still best to provide public access to it for the purposes of subscribing/unsubscribing as an event though (rather than a raw delegate), otherwise you can run into thread-safety problems with data races on the delegates.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签