public static string GetEffectiveDelegateType(this EventResponseSave ers, IElement containingElement)
        {
            if (ers.GetIsTunneling())
            {
                IElement          tunneledElement;
                EventResponseSave tunneledTo = ers.GetEventThisTunnelsTo(containingElement, out tunneledElement);

                if (tunneledTo != null)
                {
                    return(tunneledTo.GetEffectiveDelegateType(tunneledElement));
                }
            }

            EventSave eventSave = ers.GetEventSave();

            string delegateType;

            if (eventSave != null && !string.IsNullOrEmpty(eventSave.DelegateType))
            {
                delegateType = eventSave.DelegateType;
            }
            else if (!string.IsNullOrEmpty(ers.DelegateType))
            {
                delegateType = ers.DelegateType;
            }
            else
            {
                delegateType = "EventHandler";
            }

            if (delegateType == "Action")
            {
                delegateType = "System.Action";
            }

            return(delegateType);
        }
        private static void GenerateInitializeForEvent(ICodeBlock codeBlock, IElement element, EventResponseSave ers)
        {
            bool wasEventAdded = false;

            //We always want this to happen, even if it's 
            // emtpy
            //if (!string.IsNullOrEmpty(ers.Contents))
            //{
            NamedObjectSave sourceNos = null;
            bool shouldCloseIfStatementForNos = false;

            if (!string.IsNullOrEmpty(ers.SourceVariable))
            {
                // This is tied to a variable, so the name comes from the variable event rather than the
                // event name itself
                string eventName = ers.BeforeOrAfter.ToString() + ers.SourceVariable + "Set";
                codeBlock.Line("this." + eventName + " += On" + ers.EventName + ";");
                wasEventAdded = true;
            }

            else if (string.IsNullOrEmpty(ers.SourceObject))
            {

                string leftSide = null;
                EventSave eventSave = ers.GetEventSave();
                if (eventSave == null || string.IsNullOrEmpty(eventSave.ExternalEvent))
                {
                    leftSide = "this." + ers.EventName;
                }
                else
                {
                    leftSide = eventSave.ExternalEvent;
                }



                codeBlock.Line(leftSide + " += On" + ers.EventName + ";");
                wasEventAdded = true;
            }
            else if (!string.IsNullOrEmpty(ers.SourceObjectEvent))
            {
                // Only append this if the source NOS is fully-defined.  If not, we don't want to generate compile errors.
                sourceNos = element.GetNamedObject(ers.SourceObject);

                if (sourceNos != null && sourceNos.IsFullyDefined)
                {
                    NamedObjectSaveCodeGenerator.AddIfConditionalSymbolIfNecesssary(codeBlock, sourceNos);

                    string leftSide = null;

                    leftSide = ers.SourceObject + "." + ers.SourceObjectEvent;

                    codeBlock.Line(leftSide + " += On" + ers.EventName + ";");
                    wasEventAdded = true;
                    shouldCloseIfStatementForNos = true;
                }
            }

            if (!string.IsNullOrEmpty(ers.SourceObject) && !string.IsNullOrEmpty(ers.SourceObjectEvent) && wasEventAdded)
            {

                codeBlock.Line(ers.SourceObject + "." + ers.SourceObjectEvent + " += On" + ers.EventName + "Tunnel;");
                if (shouldCloseIfStatementForNos)
                {
                    NamedObjectSaveCodeGenerator.AddEndIfIfNecessary(codeBlock, sourceNos);
                }
            }
        }
        public static ICodeBlock FillWithGeneratedEventCode(ICodeBlock currentBlock, EventResponseSave ers, IElement element)
        {
            EventSave eventSave = ers.GetEventSave();

            string args = ers.GetArgsForMethod(element);

            if (!string.IsNullOrEmpty(ers.SourceObject) && !string.IsNullOrEmpty(ers.SourceObjectEvent))
            {
                currentBlock = currentBlock
                    .Function("void", "On" + ers.EventName + "Tunnel", args);

                string reducedArgs = StripTypesFromArguments(args);

                currentBlock.If("this." + ers.EventName + " != null")
                    .Line(ers.EventName + "(" + reducedArgs + ");")
                    .End();

                currentBlock = currentBlock.End();
            }
            return currentBlock;
        }
 public static bool GetIsExposing(this EventResponseSave instance)
 {
     return(instance.GetEventSave() != null && instance.GetEventSave().CreatesEventMember);
 }
        public static string GetArgsForMethod(this EventResponseSave ers, IElement containingElement)
        {
            EventSave eventSave = ers.GetEventSave();


            string args      = null;
            bool   foundArgs = false;

            if (eventSave != null)
            {
                args      = eventSave.Arguments;
                foundArgs = true;
            }
            else if (ers.GetIsTunneling())
            {
                IElement          tunneledContainingElement;
                EventResponseSave tunneled = ers.GetEventThisTunnelsTo(containingElement, out tunneledContainingElement);

                if (tunneled != null)
                {
                    return(tunneled.GetArgsForMethod(tunneledContainingElement));
                }
            }
            else if (!string.IsNullOrEmpty(ers.DelegateType))
            {
                if (ers.DelegateType.StartsWith("System.Action<"))
                {
                    string delegateType = ers.DelegateType;

                    int startOfGeneric  = delegateType.IndexOf("<") + 1;
                    int endofGeneric    = delegateType.LastIndexOf(">");
                    int lengthOfGeneric = endofGeneric - startOfGeneric;

                    string genericType = delegateType.Substring(startOfGeneric, lengthOfGeneric);

                    args      = genericType + " value";
                    foundArgs = true;
                }
                else
                {
                    Type type = TypeManager.GetTypeFromString(ers.DelegateType);

                    if (type != null)
                    {
                        MethodInfo      methodInfo = type.GetMethod("Invoke");
                        ParameterInfo[] parameters = methodInfo.GetParameters();
                        for (int i = 0; i < parameters.Length; i++)
                        {
                            if (i != 0)
                            {
                                args += ", ";
                            }

                            ParameterInfo param = parameters[i];

                            args += param.ParameterType.FullName + " " + param.Name;
                            Console.WriteLine("{0} {1}", param.ParameterType.Name, param.Name);
                        }

                        foundArgs = true;
                    }
                }
            }

            if (!foundArgs)
            {
                args = "object sender, EventArgs e";
            }

            return(args);
        }
 public static bool GetCreatesEvent(this EventResponseSave eventResponseSave)
 {
     return(eventResponseSave.GetEventSave() != null &&
            eventResponseSave.GetEventSave().CreatesEventMember);
 }