Exemplo n.º 1
0
 public CommandParameterDescriptor(ParameterInfo parameterInfo)
     : base(new CommandPropertyRequiredAttribute(), parameterInfo.Name)
 {
     this.value               = parameterInfo.DefaultValue;
     this.Summary             = parameterInfo.GetSummary();
     this.Description         = parameterInfo.GetDescription();
     this.DefaultValue        = parameterInfo.DefaultValue;
     this.MemberType          = parameterInfo.ParameterType;
     this.completionAttribute = parameterInfo.GetCustomAttribute <CommandCompletionAttribute>();
 }
        protected string[] GetCompletion(object instance, string find, CommandCompletionAttribute attribute)
        {
            if (attribute is null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }
            var methodName    = attribute.MethodName;
            var type          = attribute.Type ?? instance.GetType();
            var obj           = attribute.Type != null ? null : instance;
            var flag          = attribute.Type != null ? BindingFlags.Static : BindingFlags.Instance;
            var asyncWaitTime = attribute.AsyncWaitTime;
            var method        = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | flag, null, new Type[] { }, null);

            try
            {
                var arrayType = this.MemberType.MakeArrayType();
                var value     = method.Invoke(obj, null);
                if (value != null && value.GetType() == arrayType)
                {
                    var @array   = value as Array;
                    var itemList = new List <string>(@array.Length);
                    foreach (var item in @array)
                    {
                        itemList.Add($"{item}");
                    }
                    return(itemList.ToArray());
                }
                else if (value is string[] items)
                {
                    return(items);
                }
                else if (value is Task <string[]> task)
                {
                    if (task.Wait(asyncWaitTime) == false)
                    {
                        return(null);
                    }
                    return(task.Result);
                }
                throw new NotImplementedException();
            }
            catch (Exception e)
            {
                Trace.TraceError($"{e}");
                return(null);
            }
        }