本文整理汇总了C#中System.CodeDom.CodeTypeDeclaration.AddEvent方法的典型用法代码示例。如果您正苦于以下问题:C# CodeTypeDeclaration.AddEvent方法的具体用法?C# CodeTypeDeclaration.AddEvent怎么用?C# CodeTypeDeclaration.AddEvent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.CodeDom.CodeTypeDeclaration
的用法示例。
在下文中一共展示了CodeTypeDeclaration.AddEvent方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImplementINotifyPropertyChanged
/// <summary>
/// Adds an implementation of the INotifyPropertyChanged interface to the given type.
/// </summary>
/// <param name="type">The type declaration</param>
/// <returns>Type with INotifyPropertyChanged implemented.</returns>
protected CodeTypeDeclaration ImplementINotifyPropertyChanged(CodeTypeDeclaration type)
{
//// This method will implement the INotifyPropertyChanged interface
//// Here's an example :
//// public class Customer :INotifyPropertyChanged {
//// public PropertyChangedEventHandler PropertyChanged;
//// public void RaisePropertyChanged(string propertyName) {
//// if( this.PropertyChanged !=null ) {
//// this.PropertyChanged (this, new PropertyChangedEventArgs( propertyName ) );
//// }
//// }
//// }
CodeThisReferenceExpression thisReference = new CodeThisReferenceExpression();
CodeTypeReference notifyPropertyChanged = Code.TypeRef(typeof(INotifyPropertyChanged));
// Add the implements INotifyPropertyChanged statement
// public class Customer :INotifyPropertyChanged {
type.BaseTypes.Add(notifyPropertyChanged);
// Add the PropertyChanged event as a field to the type
// public PropertyChangedEventHandler PropertyChanged;
CodeMemberEvent propertyChangedEvent = type.AddEvent(Code.TypeRef(typeof(PropertyChangedEventHandler)), "PropertyChanged");
propertyChangedEvent.ImplementationTypes.Add(notifyPropertyChanged);
// this.PropertyChanged
CodeEventReferenceExpression eventFieldReference = new CodeEventReferenceExpression(thisReference, "PropertyChanged");
// Add the RaisePropertyChanged Method which will invoke the PropertyChanged handler whenever a property value changes
// if( this.PropertyChanged !=null )
CodeConditionStatement invokeEventHandlersIfAny = new CodeConditionStatement(new CodeBinaryOperatorExpression(eventFieldReference, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)));
// this.PropertyChanged (this, new PropertyChangedEventArgs( propertyName ) );
invokeEventHandlersIfAny.TrueStatements.Add(
new CodeDelegateInvokeExpression(eventFieldReference, new CodeExpression[] { thisReference, new CodeObjectCreateExpression("PropertyChangedEventArgs", new CodeArgumentReferenceExpression("propertyName")) }));
// public void RaisePropertyChanged
CodeMemberMethod method = type.AddMethod("RaisePropertyChanged", MemberAttributes.Public);
method.Parameters.Add(new CodeParameterDeclarationExpression("System.String", "propertyName"));
//// public void RaisePropertyChanged(string propertyName) {
//// if( this.PropertyChanged !=null ) {
//// this.PropertyChanged (this, new PropertyChangedEventArgs( propertyName ) );
//// }
//// }
method.Statements.Add(invokeEventHandlersIfAny);
return type;
}