Partial classes and delegates - probably a very straightforward question

Partial classes and delegates - probably a very straightforward question

Old forum URL: forums.lhotka.net/forums/t/624.aspx


skagen00 posted on Friday, July 14, 2006

I'm exploring the partial class generation approach with the C# code smith templates, and it is not entirely clear to me how to hook up my "AddCustomRules" method in my handcrafted part of the partial class.

The generated code is below. How do I add my method to the addCustomRulesDelegate without changing the generated code? 

protected override void AddBusinessRules()

{

AddCommonRules();

onAddCustomRules();

}

private delegate void rulesAction();

//

// Business Rules

//

[NonSerialized()]

private rulesAction addCustomRulesDelegate = null;

private void onAddCustomRules()

{

if (addCustomRulesDelegate != null)

addCustomRulesDelegate();

}

xal replied on Friday, July 14, 2006

You are supposed to create the handlers inside Initialize() and  OnDeserialized()


like (vb example, but you'll get the point):

Protected Overrides Sub Initialize()
        MyBase.Initialize()
        AddHandler Me.DelegateName, AddressOf HandlerName
End Sub

Protected Overrides Sub OnDeserialized(ByVal context As System.Runtime.Serialization.StreamingContext)
        MyBase.OnDeserializedObject(context)
        AddHandler Me.DelegateName, AddressOf HandlerName
End Sub

Andrés

skagen00 replied on Friday, July 14, 2006

Thanks for the quick answer, Xal - it worked, now I know!

 

rasupit replied on Friday, July 14, 2006

Thanks xal, you beat me to it.

Since this is a delagate, you could also do as follow:

addCustomRulesDelegate = addCustomRules;

which is a shortcut version of:

addCustomRulesDelegate = new rulesAction(addCustomRules);

in Vb I believe it would be:

addCustomRulesDelegate = AddressOf addCustomRules

or

addCustomRulesDelegate = new rulesAction(AddressOf addCustomRules)

HTH,

Ricky

Copyright (c) Marimer LLC