Example #1
0
        /// <summary>
        /// The view is asking for variable names for its intellisense.
        /// </summary>
        /// <param name="atype">Data type for which we want completion options.</param>
        /// <param name="properties">If true, property suggestions will be generated.</param>
        /// <param name="methods">If true, method suggestions will be generated.</param>
        /// <param name="publishedEvents">If true, published events will be returned.</param>
        /// <param name="subscribedEvents">If true, subscribed events will be returned.</param>
        /// <returns>List of completion options.</returns>
        public static List <ContextItem> ExamineTypeForContextItems(Type atype, bool properties, bool methods, bool publishedEvents, bool subscribedEvents)
        {
            List <ContextItem> allItems = new List <ContextItem>();

            // find the properties and methods
            if (atype != null)
            {
                if (properties)
                {
                    foreach (PropertyInfo property in atype.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                    {
                        VariableProperty var  = new VariableProperty(atype, property);
                        ContextItem      item = new ContextItem
                        {
                            Name        = var.Name,
                            IsProperty  = true,
                            IsEvent     = false,
                            IsWriteable = !var.IsReadOnly,
                            TypeName    = GetTypeName(var.DataType),
                            Descr       = GetDescription(property),
                            Units       = var.Units
                        };
                        allItems.Add(item);
                    }
                }

                if (methods)
                {
                    foreach (MethodInfo method in atype.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy))
                    {
                        if (!method.Name.StartsWith("get_") && !method.Name.StartsWith("set_") &&
                            !method.Name.StartsWith("add_") && !method.Name.StartsWith("remove_"))
                        {
                            ContextItem item = new ContextItem
                            {
                                Name        = method.Name,
                                IsProperty  = false,
                                IsEvent     = false,
                                IsMethod    = true,
                                IsWriteable = false,
                                TypeName    = method.ReturnType.Name,
                                Descr       = GetDescription(method),
                                Units       = string.Empty
                            };

                            // build a parameter string representation
                            ParameterInfo[] allparams = method.GetParameters();
                            StringBuilder   paramText = new StringBuilder("( ");
                            if (allparams.Count() > 0)
                            {
                                for (int p = 0; p < allparams.Count(); p++)
                                {
                                    ParameterInfo parameter = allparams[p];
                                    paramText.Append(parameter.ParameterType.Name + " " + parameter.Name);
                                    if (parameter.DefaultValue != DBNull.Value)
                                    {
                                        paramText.Append(" = " + parameter.DefaultValue?.ToString() ?? "null");
                                    }
                                    if (p < allparams.Count() - 1)
                                    {
                                        paramText.Append(", ");
                                    }
                                }
                            }
                            paramText.Append(" )");
                            item.ParamString = paramText.ToString();

                            allItems.Add(item);
                        }
                    }
                }

                if (publishedEvents)
                {
                    foreach (EventInfo evnt in atype.GetEvents(BindingFlags.Instance | BindingFlags.Public))
                    {
                        NeedContextItemsArgs.ContextItem item = new NeedContextItemsArgs.ContextItem();
                        item.Name        = evnt.Name;
                        item.IsProperty  = true;
                        item.IsEvent     = true;
                        item.IsMethod    = false;
                        item.IsWriteable = false;
                        item.TypeName    = evnt.ReflectedType.Name;
                        item.Descr       = GetDescription(evnt);
                        item.Units       = "";
                        allItems.Add(item);
                    }
                }

                if (subscribedEvents)
                {
                    foreach (MethodInfo method in atype.GetMethods(/*BindingFlags.Instance |*/ BindingFlags.NonPublic | BindingFlags.FlattenHierarchy))
                    {
                        EventSubscribeAttribute subscriberAttribute = (EventSubscribeAttribute)ReflectionUtilities.GetAttribute(method, typeof(EventSubscribeAttribute), false);
                        if (subscriberAttribute != null)
                        {
                            NeedContextItemsArgs.ContextItem item = new NeedContextItemsArgs.ContextItem();
                            item.Name        = subscriberAttribute.ToString();
                            item.IsProperty  = false;
                            item.IsEvent     = false;
                            item.IsMethod    = true;
                            item.IsWriteable = false;
                            item.TypeName    = atype.Name;
                            item.Descr       = GetDescription(atype);
                            item.Units       = "";
                            allItems.Add(item);
                        }
                    }
                }
            }

            allItems.Sort(delegate(ContextItem c1, ContextItem c2) { return(c1.Name.CompareTo(c2.Name)); });
            return(allItems);
        }
 private EventSubscribeOptions CreateEventSubscribeOptions(string fullName, EventSubscribeAttribute attribute)
 {
     return(new EventSubscribeOptions(attribute.MQProvider, attribute.Topic, fullName));
 }