Inheritance: System.CodeDom.CodeExpression
		public void Constructor1 ()
		{
			CodeEventReferenceExpression eventref = new CodeEventReferenceExpression ();
			CodeExpression listener = new CodeExpression ();

			CodeRemoveEventStatement caes = new CodeRemoveEventStatement (eventref, listener);
			Assert.AreSame (eventref, caes.Event, "#1");
			Assert.AreEqual (string.Empty, caes.Event.EventName, "#2");
			Assert.IsNull (caes.Event.TargetObject, "#3");
			Assert.AreSame (listener, caes.Listener, "#4");

			caes.Event = null;
			Assert.IsNotNull (caes.Event, "#5");
			Assert.AreEqual (string.Empty, caes.Event.EventName, "#6");
			Assert.IsNull (caes.Event.TargetObject, "#7");
			Assert.AreSame (listener, caes.Listener, "#8");

			caes.Listener = null;
			Assert.IsNull (caes.Listener, "#9");

			caes.Event = eventref;
			Assert.AreSame (eventref, caes.Event, "#10");

			caes.Listener = listener;
			Assert.AreSame (listener, caes.Listener, "#11");

			caes = new CodeRemoveEventStatement ((CodeEventReferenceExpression) null, (CodeExpression) null);
			Assert.IsNotNull (caes.Event, "#12");
			Assert.IsNull (caes.Listener, "#13");
			Assert.AreEqual (string.Empty, caes.Event.EventName, "#14");
			Assert.IsNull (caes.Event.TargetObject, "#15");
		}
 internal static void AddCallbackImplementation(CodeTypeDeclaration codeClass, string callbackName, string handlerName, string handlerArgs, bool methodHasOutParameters)
 {
     CodeFlags[] parameterFlags = new CodeFlags[1];
     CodeMemberMethod method = AddMethod(codeClass, callbackName, parameterFlags, new string[] { typeof(object).FullName }, new string[] { "arg" }, typeof(void).FullName, null, (CodeFlags) 0);
     CodeEventReferenceExpression left = new CodeEventReferenceExpression(new CodeThisReferenceExpression(), handlerName);
     CodeBinaryOperatorExpression condition = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null));
     CodeStatement[] trueStatements = new CodeStatement[2];
     trueStatements[0] = new CodeVariableDeclarationStatement(typeof(InvokeCompletedEventArgs), "invokeArgs", new CodeCastExpression(typeof(InvokeCompletedEventArgs), new CodeArgumentReferenceExpression("arg")));
     CodeVariableReferenceExpression targetObject = new CodeVariableReferenceExpression("invokeArgs");
     CodeObjectCreateExpression expression4 = new CodeObjectCreateExpression();
     if (methodHasOutParameters)
     {
         expression4.CreateType = new CodeTypeReference(handlerArgs);
         expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "Results"));
     }
     else
     {
         expression4.CreateType = new CodeTypeReference(typeof(AsyncCompletedEventArgs));
     }
     expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "Error"));
     expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "Cancelled"));
     expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "UserState"));
     trueStatements[1] = new CodeExpressionStatement(new CodeDelegateInvokeExpression(new CodeEventReferenceExpression(new CodeThisReferenceExpression(), handlerName), new CodeExpression[] { new CodeThisReferenceExpression(), expression4 }));
     method.Statements.Add(new CodeConditionStatement(condition, trueStatements, new CodeStatement[0]));
 }
		public override void Serialize (IDesignerSerializationManager manager, object value, MemberDescriptor descriptor, CodeStatementCollection statements)
		{
			if (statements == null)
				throw new ArgumentNullException ("statements");
			if (manager == null)
				throw new ArgumentNullException ("manager");
			if (value == null)
				throw new ArgumentNullException ("value");
			if (descriptor == null)
				throw new ArgumentNullException ("descriptor");

			IEventBindingService service = manager.GetService (typeof (IEventBindingService)) as IEventBindingService;
			if (service != null) {
				// In the propertygrid the events are represented by PropertyDescriptors and the value is a string
				// which contains the method name to bind to. The propertydescriptors are managed and created by the 
				// IEventBindingService
				// 
				EventDescriptor ev = (EventDescriptor) descriptor;
				string methodName = (string) service.GetEventProperty (ev).GetValue (value);
				CodeDelegateCreateExpression listener = new CodeDelegateCreateExpression (new CodeTypeReference (ev.EventType), _thisReference, methodName);
				CodeExpression targetObject = base.SerializeToExpression (manager, value);
				CodeEventReferenceExpression eventRef = new CodeEventReferenceExpression (targetObject, ev.Name);
				statements.Add (new CodeAttachEventStatement (eventRef, listener));
			}
		}
		public override void Serialize (IDesignerSerializationManager manager, object value, MemberDescriptor descriptor, 
						CodeStatementCollection statements)
		{
			if (statements == null)
				throw new ArgumentNullException ("statements");
			if (manager == null)
				throw new ArgumentNullException ("manager");
			if (value == null)
				throw new ArgumentNullException ("value");
			if (descriptor == null)
				throw new ArgumentNullException ("descriptor");

			IEventBindingService service = manager.GetService (typeof (IEventBindingService)) as IEventBindingService;
			if (service != null) {
				EventDescriptor eventDescriptor = (EventDescriptor) descriptor;
				string methodName = (string) service.GetEventProperty (eventDescriptor).GetValue (value);

				if (methodName != null) {
					CodeDelegateCreateExpression listener = new CodeDelegateCreateExpression (new CodeTypeReference (eventDescriptor.EventType),
																							   _thisReference, methodName);
					CodeExpression targetObject = base.SerializeToExpression (manager, value);
					CodeEventReferenceExpression eventRef = new CodeEventReferenceExpression (targetObject, eventDescriptor.Name);
					statements.Add (new CodeAttachEventStatement (eventRef, listener));
				}
			}
		}
		public void Constructor0_Deny_Unrestricted ()
		{
			CodeEventReferenceExpression cere = new CodeEventReferenceExpression ();
			Assert.AreEqual (String.Empty, cere.EventName, "EventName");
			cere.EventName = "mono";
			Assert.IsNull (cere.TargetObject, "TargetObject");
			cere.TargetObject = new CodeExpression ();
		}
		public CodeAttachEventStatement (CodeExpression targetObject,
						 string eventName, 
						 CodeExpression listener)
		{
			this.eventRef = new CodeEventReferenceExpression (targetObject,
																eventName);
			this.listener = listener;
		}
		public void Constructor1_Deny_Unrestricted ()
		{
			CodeExpression target = new CodeExpression ();
			CodeEventReferenceExpression cere = new CodeEventReferenceExpression (target, "mono");
			Assert.AreEqual ("mono", cere.EventName, "EventName");
			cere.EventName = String.Empty;
			Assert.AreSame (target, cere.TargetObject, "TargetObject");
			cere.TargetObject = new CodeExpression ();
		}
 public static CodeEventReferenceExpression Clone(this CodeEventReferenceExpression expression)
 {
     if (expression == null) return null;
     CodeEventReferenceExpression e = new CodeEventReferenceExpression();
     e.EventName = expression.EventName;
     e.TargetObject = expression.TargetObject.Clone();
     e.UserData.AddRange(expression.UserData);
     return e;
 }
		public void Constructor1_Deny_Unrestricted ()
		{
			CodeEventReferenceExpression eventref = new CodeEventReferenceExpression ();
			CodeExpression listener = new CodeExpression ();
			CodeAttachEventStatement caes = new CodeAttachEventStatement (eventref, listener);
			Assert.AreSame (eventref, caes.Event, "Event");
			caes.Event = new CodeEventReferenceExpression ();
			Assert.AreSame (listener, caes.Listener, "Listener");
			caes.Listener = new CodeExpression ();
		}
 public override void Serialize(IDesignerSerializationManager manager, object value, MemberDescriptor descriptor, CodeStatementCollection statements)
 {
     EventDescriptor e = descriptor as EventDescriptor;
     if (manager == null)
     {
         throw new ArgumentNullException("manager");
     }
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     if (e == null)
     {
         throw new ArgumentNullException("descriptor");
     }
     if (statements == null)
     {
         throw new ArgumentNullException("statements");
     }
     try
     {
         IEventBindingService service = (IEventBindingService) manager.GetService(typeof(IEventBindingService));
         if (service != null)
         {
             string methodName = (string) service.GetEventProperty(e).GetValue(value);
             if (methodName != null)
             {
                 CodeExpression targetObject = base.SerializeToExpression(manager, value);
                 if (targetObject != null)
                 {
                     CodeTypeReference delegateType = new CodeTypeReference(e.EventType);
                     CodeDelegateCreateExpression listener = new CodeDelegateCreateExpression(delegateType, _thisRef, methodName);
                     CodeEventReferenceExpression eventRef = new CodeEventReferenceExpression(targetObject, e.Name);
                     CodeAttachEventStatement statement = new CodeAttachEventStatement(eventRef, listener);
                     statement.UserData[typeof(Delegate)] = e.EventType;
                     statements.Add(statement);
                 }
             }
         }
     }
     catch (Exception innerException)
     {
         if (innerException is TargetInvocationException)
         {
             innerException = innerException.InnerException;
         }
         manager.ReportError(System.Design.SR.GetString("SerializerPropertyGenFailed", new object[] { e.Name, innerException.Message }));
     }
 }
		public void Constructor1 ()
		{
			CodeSnippetExpression expression = new CodeSnippetExpression("exp");

			CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
				expression, "mono");
			Assert.AreEqual ("mono", cere.EventName, "#1");
			Assert.IsNotNull (cere.TargetObject, "#2");
			Assert.AreSame (expression, cere.TargetObject, "#3");

			cere.EventName = null;
			Assert.IsNotNull (cere.EventName, "#4");
			Assert.AreEqual (string.Empty, cere.EventName, "#5");

			cere.TargetObject = null;
			Assert.IsNull (cere.TargetObject, "#6");

			cere = new CodeEventReferenceExpression ((CodeExpression) null,
				(string) null);
			Assert.IsNotNull (cere.EventName, "#7");
			Assert.AreEqual (string.Empty, cere.EventName, "#8");
			Assert.IsNull (cere.TargetObject, "#9");
		}
		public void Constructor0 ()
		{
			CodeEventReferenceExpression cere = new CodeEventReferenceExpression ();
			Assert.IsNotNull (cere.EventName, "#1");
			Assert.AreEqual (string.Empty, cere.EventName, "#2");
			Assert.IsNull (cere.TargetObject, "#3");

			string eventName = "mono";
			cere.EventName = eventName;
			Assert.IsNotNull (cere.EventName, "#4");
			Assert.AreSame (eventName, cere.EventName, "#5");

			cere.EventName = null;
			Assert.IsNotNull (cere.EventName, "#6");
			Assert.AreEqual (string.Empty, cere.EventName, "#7");

			CodeExpression expression = new CodeExpression ();
			cere.TargetObject = expression;
			Assert.IsNotNull (cere.TargetObject, "#8");
			Assert.AreSame (expression, cere.TargetObject, "#9");

			cere.TargetObject = null;
			Assert.IsNull (cere.TargetObject, "#10");
		}
 public override void EnterExpressionStmt([NotNull] XSharpParser.ExpressionStmtContext context)
 {
     if (initComponent != null)
     {
         CodeExpression expr = new CodeExpression();
         CodeStatement stmt = new CodeStatement();
         //
         if (context._expression is XSharpParser.AssignmentExpressionContext)
         {
             XSharpParser.AssignmentExpressionContext exp = (XSharpParser.AssignmentExpressionContext)context._expression;
             //
             //what is the left hand side ?
             //    Self  -> check if Right is in the member of CurrentClass --> FieldReference
             // else --> always Property
             //
             CodeExpression left = BuildExpression(exp.Left, false);
             CodeExpression right = BuildExpression(exp.Right, true);
             if (exp.ASSIGN_OP() != null)
             {
                 //expr = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.Assign, right);
                 stmt = new CodeAssignStatement(left, right);
             }
             else if (exp.ASSIGN_ADD() != null)
             {
                 // += Event Handler
                 // We will decode Left as CodeFieldReferenceExpression or CodePropertyReferenceExpression, but we need a CodeEventReferenceExpression
                 CodeEventReferenceExpression cere;
                 if (left is CodeFieldReferenceExpression)
                     cere = new CodeEventReferenceExpression(((CodeFieldReferenceExpression)left).TargetObject, ((CodeFieldReferenceExpression)left).FieldName);
                 else
                     cere = new CodeEventReferenceExpression(((CodePropertyReferenceExpression)left).TargetObject, ((CodePropertyReferenceExpression)left).PropertyName);
                 stmt = new CodeAttachEventStatement(cere, right);
                 //
             }
             else if (exp.ASSIGN_SUB() != null)
             {
                 // -= Event Handler
                 CodeEventReferenceExpression cere;
                 if (left is CodeFieldReferenceExpression)
                     cere = new CodeEventReferenceExpression(((CodeFieldReferenceExpression)left).TargetObject, ((CodeFieldReferenceExpression)left).FieldName);
                 else
                     cere = new CodeEventReferenceExpression(((CodePropertyReferenceExpression)left).TargetObject, ((CodePropertyReferenceExpression)left).PropertyName);
                 stmt = new CodeRemoveEventStatement(cere, right);
             }
         }
         else if (context._expression is XSharpParser.MethodCallContext)
         {
             XSharpParser.MethodCallContext exp = (XSharpParser.MethodCallContext)context._expression;
             expr = BuildExpression(exp,false);
             stmt = new CodeExpressionStatement(expr);
         }
         else
         {
             expr = new CodeSnippetExpression(context.GetText());
             stmt = new CodeExpressionStatement(expr);
         }
         //
         initComponent.Statements.Add(stmt);
     }
 }
Example #14
0
 protected override void GenerateEventReferenceExpression(CodeEventReferenceExpression e) {
     if (e.TargetObject != null) {
         GenerateExpression(e.TargetObject);
         Output.Write(".");
     }
     OutputIdentifier(e.EventName);
 }
Example #15
0
 private void ValidateEventReferenceExpression(CodeEventReferenceExpression e) {
    if (e.TargetObject != null) {
         ValidateExpression(e.TargetObject);
    }
    ValidateIdentifier(e,"EventName",e.EventName);
 }
Example #16
0
 protected override void GenerateEventReferenceExpression(CodeEventReferenceExpression e)
 {
     if (e.TargetObject != null)
     {
         bool flag = e.TargetObject is CodeThisReferenceExpression;
         base.GenerateExpression(e.TargetObject);
         base.Output.Write(".");
         if (flag)
         {
             base.Output.Write(e.EventName + "Event");
         }
         else
         {
             base.Output.Write(e.EventName);
         }
     }
     else
     {
         this.OutputIdentifier(e.EventName + "Event");
     }
 }
Example #17
0
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public CodeAttachEventStatement(CodeExpression targetObject, string eventName, CodeExpression listener)
 {
     this.eventRef = new CodeEventReferenceExpression(targetObject, eventName);
     this.listener = listener;
 }
Example #18
0
		protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression expression)
		{
			if (expression.TargetObject != null) {
				GenerateExpression (expression.TargetObject);
				Output.Write ('.');
				if (expression.TargetObject is CodeThisReferenceExpression) {
					// We're actually creating a reference to a compiler-generated field here...
					Output.Write (expression.EventName + "Event");
				} else {
					Output.Write (CreateEscapedIdentifier (expression.EventName));
				}
			} else {
				Output.Write (CreateEscapedIdentifier (expression.EventName + "Event"));
			}
		}
Example #19
0
			public void Visit (CodeEventReferenceExpression o)
			{
				g.GenerateEventReferenceExpression (o);
			}
        public override object Serialize(IDesignerSerializationManager manager, object obj)
        {
            if (manager == null)
                throw new ArgumentNullException("manager");

            if (manager.Context == null)
                throw new ArgumentException("manager", SR.GetString(SR.Error_MissingContextProperty));

            if (obj == null)
                throw new ArgumentNullException("obj");

            DependencyObject dependencyObject = obj as DependencyObject;
            if (dependencyObject == null)
                throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(DependencyObject).FullName), "obj");

            Activity activity = obj as Activity;
            if (activity != null)
                manager.Context.Push(activity);

            CodeStatementCollection retVal = null;
            try
            {
                if (activity != null)
                {
                    CodeDomSerializer componentSerializer = manager.GetSerializer(typeof(Component), typeof(CodeDomSerializer)) as CodeDomSerializer;
                    if (componentSerializer == null)
                        throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(CodeDomSerializer).FullName));
                    retVal = componentSerializer.Serialize(manager, activity) as CodeStatementCollection;
                }
                else
                {
                    retVal = base.Serialize(manager, obj) as CodeStatementCollection;
                }

                if (retVal != null)
                {
                    CodeStatementCollection codeStatements = new CodeStatementCollection(retVal);
                    CodeExpression objectExpression = SerializeToExpression(manager, obj);
                    if (objectExpression != null)
                    {
                        ArrayList propertiesSerialized = new ArrayList();
                        List<DependencyProperty> dependencyProperties = new List<DependencyProperty>(dependencyObject.MetaDependencyProperties);
                        foreach (DependencyProperty dp in dependencyObject.DependencyPropertyValues.Keys)
                        {
                            if (dp.IsAttached)
                            {
                                if ((dp.IsEvent && dp.OwnerType.GetField(dp.Name + "Event", BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly) != null) ||
                                    (!dp.IsEvent && dp.OwnerType.GetField(dp.Name + "Property", BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly) != null))
                                    dependencyProperties.Add(dp);
                            }
                        }
                        foreach (DependencyProperty dependencyProperty in dependencyProperties)
                        {
                            object value = null;
                            if (dependencyObject.IsBindingSet(dependencyProperty))
                                value = dependencyObject.GetBinding(dependencyProperty);
                            else if (!dependencyProperty.IsEvent)
                                value = dependencyObject.GetValue(dependencyProperty);
                            else
                                value = dependencyObject.GetHandler(dependencyProperty);
                            // Attached property should always be set through SetValue, no matter if it's a meta property or if there is a data context.
                            // Other meta properties will be directly assigned.
                            // Other instance property will go through SetValue if there is a data context or if it's of type Bind.
                            if (value != null &&
                                (dependencyProperty.IsAttached || (!dependencyProperty.DefaultMetadata.IsMetaProperty && value is ActivityBind)))
                            {
                                object[] attributes = dependencyProperty.DefaultMetadata.GetAttributes(typeof(DesignerSerializationVisibilityAttribute));
                                if (attributes.Length > 0)
                                {
                                    DesignerSerializationVisibilityAttribute serializationVisibilityAttribute = attributes[0] as DesignerSerializationVisibilityAttribute;
                                    if (serializationVisibilityAttribute.Visibility == DesignerSerializationVisibility.Hidden)
                                        continue;
                                }

                                // Events of type Bind will go through here.  Regular events will go through IEventBindingService.
                                CodeExpression param1 = null;
                                string dependencyPropertyName = dependencyProperty.Name + ((dependencyProperty.IsEvent) ? "Event" : "Property");
                                FieldInfo fieldInfo = dependencyProperty.OwnerType.GetField(dependencyPropertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
                                if (fieldInfo != null && !fieldInfo.IsPublic)
                                    param1 = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(DependencyProperty)), "FromName", new CodePrimitiveExpression(dependencyProperty.Name), new CodeTypeOfExpression(dependencyProperty.OwnerType));
                                else
                                    param1 = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(dependencyProperty.OwnerType), dependencyPropertyName);

                                CodeExpression param2 = SerializeToExpression(manager, value);

                                //Fields property fails to serialize to expression due to reference not being created,
                                //the actual code for fields are generated in datacontext code generator so we do nothing here
                                if (param1 != null && param2 != null)
                                {
                                    CodeMethodInvokeExpression codeMethodInvokeExpr = null;
                                    if (value is ActivityBind)
                                        codeMethodInvokeExpr = new CodeMethodInvokeExpression(objectExpression, "SetBinding", new CodeExpression[] { param1, new CodeCastExpression(new CodeTypeReference(typeof(ActivityBind)), param2) });
                                    else
                                        codeMethodInvokeExpr = new CodeMethodInvokeExpression(objectExpression, (dependencyProperty.IsEvent) ? "AddHandler" : "SetValue", new CodeExpression[] { param1, param2 });
                                    retVal.Add(codeMethodInvokeExpr);

                                    // Remove the property set statement for the event which is replaced by the SetValue() expression.
                                    foreach (CodeStatement statement in codeStatements)
                                    {
                                        if (statement is CodeAssignStatement && ((CodeAssignStatement)statement).Left is CodePropertyReferenceExpression)
                                        {
                                            CodePropertyReferenceExpression prop = ((CodeAssignStatement)statement).Left as CodePropertyReferenceExpression;
                                            if (prop.PropertyName == dependencyProperty.Name && prop.TargetObject.Equals(objectExpression))
                                                retVal.Remove(statement);
                                        }
                                    }
                                }

                                propertiesSerialized.Add(dependencyProperty);
                            }
                        }

                        IEventBindingService eventBindingService = manager.GetService(typeof(IEventBindingService)) as IEventBindingService;
                        if (eventBindingService == null)
                        {
                            // At compile time, we don't have an event binding service.  We need to mannually emit the code to add
                            // event handlers.
                            foreach (EventDescriptor eventDesc in TypeDescriptor.GetEvents(dependencyObject))
                            {
                                string handler = WorkflowMarkupSerializationHelpers.GetEventHandlerName(dependencyObject, eventDesc.Name);
                                if (!string.IsNullOrEmpty(handler))
                                {
                                    CodeEventReferenceExpression eventRef = new CodeEventReferenceExpression(objectExpression, eventDesc.Name);
                                    CodeDelegateCreateExpression listener = new CodeDelegateCreateExpression(new CodeTypeReference(eventDesc.EventType), new CodeThisReferenceExpression(), handler);
                                    retVal.Add(new CodeAttachEventStatement(eventRef, listener));
                                }
                            }
                        }

                        // We also need to handle properties of type System.Type.  If the value is a design time type, xomlserializer 
                        // is not going to be able to deserialize the type.  We then store the type name in the user data and
                        // output a "typeof(xxx)" expression using the type name w/o validating the type.
                        if (dependencyObject.UserData.Contains(UserDataKeys.DesignTimeTypeNames))
                        {
                            Hashtable typeNames = dependencyObject.UserData[UserDataKeys.DesignTimeTypeNames] as Hashtable;
                            foreach (object key in typeNames.Keys)
                            {
                                string propName = null;
                                string ownerTypeName = null;
                                string typeName = typeNames[key] as string;
                                DependencyProperty dependencyProperty = key as DependencyProperty;
                                if (dependencyProperty != null)
                                {
                                    if (propertiesSerialized.Contains(dependencyProperty))
                                        continue;

                                    object[] attributes = dependencyProperty.DefaultMetadata.GetAttributes(typeof(DesignerSerializationVisibilityAttribute));
                                    if (attributes.Length > 0)
                                    {
                                        DesignerSerializationVisibilityAttribute serializationVisibilityAttribute = attributes[0] as DesignerSerializationVisibilityAttribute;
                                        if (serializationVisibilityAttribute.Visibility == DesignerSerializationVisibility.Hidden)
                                            continue;
                                    }

                                    propName = dependencyProperty.Name;
                                    ownerTypeName = dependencyProperty.OwnerType.FullName;
                                }
                                else if (key is string)
                                {
                                    int indexOfDot = ((string)key).LastIndexOf('.');
                                    Debug.Assert(indexOfDot != -1, "Wrong property name in DesignTimeTypeNames hashtable.");
                                    if (indexOfDot != -1)
                                    {
                                        ownerTypeName = ((string)key).Substring(0, indexOfDot);
                                        propName = ((string)key).Substring(indexOfDot + 1);
                                    }
                                }

                                if (!string.IsNullOrEmpty(typeName) && !string.IsNullOrEmpty(propName) && !string.IsNullOrEmpty(ownerTypeName))
                                {
                                    if (ownerTypeName == obj.GetType().FullName)
                                    {
                                        // Property is not an attached property.  Serialize using regular property set expression.
                                        CodePropertyReferenceExpression propertyRef = new CodePropertyReferenceExpression(objectExpression, propName);
                                        retVal.Add(new CodeAssignStatement(propertyRef, new CodeTypeOfExpression(typeName)));
                                    }
                                    else
                                    {
                                        // This is an attached property.  Serialize using SetValue() expression.
                                        CodeExpression param1 = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(ownerTypeName), propName + "Property");
                                        CodeExpression param2 = new CodeTypeOfExpression(typeName);
                                        retVal.Add(new CodeMethodInvokeExpression(objectExpression, "SetValue", new CodeExpression[] { param1, param2 }));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (activity != null)
                {
                    object pushedActivity = manager.Context.Pop();
                    System.Diagnostics.Debug.Assert(pushedActivity == activity);
                }
            }

            return retVal;
        }
Example #21
0
 protected override void GenerateEventReferenceExpression(System.CodeDom.CodeEventReferenceExpression e)
 {
     GenerateExpression(e.TargetObject);
     Output.Write(".");
     Output.Write(e.EventName);
 }
Example #22
0
		void AddEventAssign (CodeMemberMethod method, ControlBuilder builder, string name, Type type, string value)
		{
			//"__ctrl.{0} += new {1} (this.{2});"
			CodeEventReferenceExpression evtID = new CodeEventReferenceExpression (ctrlVar, name);

			CodeDelegateCreateExpression create;
			create = new CodeDelegateCreateExpression (new CodeTypeReference (type), thisRef, value);

			CodeAttachEventStatement attach = new CodeAttachEventStatement (evtID, create);
			method.Statements.Add (attach);
		}
        /// <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;
        }
Example #24
0
 /// <devdoc>
 ///    <para>
 ///       Initializes a new instance of the <see cref='System.CodeDom.CodeAttachEventStatement'/> class using the specified arguments.
 ///    </para>
 /// </devdoc>
 public CodeAttachEventStatement(CodeEventReferenceExpression eventRef, CodeExpression listener)
 {
     this.eventRef = eventRef;
     this.listener = listener;
 }
 /// <devdoc>
 ///    <para>
 ///       Initializes a new instance of the <see cref='System.CodeDom.CodeAttachEventStatement'/> class using the specified arguments.
 ///    </para>
 /// </devdoc>
 public CodeAttachEventStatement(CodeEventReferenceExpression eventRef, CodeExpression listener) {
     this.eventRef = eventRef;
     this.listener = listener;
 }
Example #26
0
 /// <summary>
 /// Generates code for the specified event reference expression.
 /// </summary>
 /// <remarks><c>TARGETOBJECT-&gt;EVENTNAME</c> or <c>TARGETOBJECT::EVENTNAME</c></remarks>
 protected override void GenerateEventReferenceExpression(CodeEventReferenceExpression e)
 {
     OutputMemberReference(e.TargetObject, e.EventName, true, null);
 }
Example #27
0
		protected abstract void GenerateEventReferenceExpression (CodeEventReferenceExpression e);
		public void CodeAttachEventStatementTest ()
		{
			CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
				new CodeSnippetExpression ("A"), "class");
			CodeSnippetExpression handler = new CodeSnippetExpression ("EventHandler");

			CodeAttachEventStatement attachEventStatement = new CodeAttachEventStatement ();
			statement = attachEventStatement;

			try {
				Generate ();
				Assert.Fail ("#1");
			} catch (ArgumentNullException) {
			}

			attachEventStatement.Event = cere;
			try {
				Generate ();
				Assert.Fail ("#2");
			} catch (ArgumentNullException) {
			}

			attachEventStatement.Event = null;
			attachEventStatement.Listener = handler;
			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				" += EventHandler;{0}", NewLine), Generate (), "#3");

			attachEventStatement.Event = cere;
			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				"A.@class += EventHandler;{0}", NewLine), Generate (), "#4");

			attachEventStatement.Event = new CodeEventReferenceExpression (
				new CodeSnippetExpression ((string) null), "");
			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				". += EventHandler;{0}", NewLine), Generate (), "#5");

			attachEventStatement.Listener = new CodeSnippetExpression ("");
			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				". += ;{0}", NewLine), Generate (), "#6");
		}
Example #29
0
		protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression e)
		{
		}
        public bool ValidateCodeEventReferenceExpression (CodeEventReferenceExpression exp) {
            bool result = false;
            PushLocation (exp);

            if (IsSimpleTarget (exp.TargetObject))
                result = true;

            PopLocation();
            return result;
        }
Example #31
0
 private void GenerateFormalEventReferenceExpression(CodeEventReferenceExpression e)
 {
     if ((e.TargetObject != null) && !(e.TargetObject is CodeThisReferenceExpression))
     {
         base.GenerateExpression(e.TargetObject);
         base.Output.Write(".");
     }
     this.OutputIdentifier(e.EventName);
 }
Example #32
0
		protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression expression)
		{
			if (expression.TargetObject != null) {
				GenerateExpression (expression.TargetObject);
				Output.Write ('.');
			}
			Output.Write (GetSafeName (expression.EventName));
		}
		protected override void GenerateEventReferenceExpression(CodeEventReferenceExpression e)
		{
			Output.Write("[CodeEventReferenceExpression: Name={0}, Target=", e.EventName);
			this.GenerateExpression(e.TargetObject);
			Output.Write("]");
		}
Example #34
0
 public CodeAttachEventStatement(CodeEventReferenceExpression eventRef, CodeExpression listener)
 {
     throw new NotImplementedException();
 }