Inheritance: System.Attribute
Ejemplo n.º 1
0
        void ProcessAssembly(Assembly assembly)
        {
            foreach (var type in assembly.DefinedTypes)
            {
                foreach (var method in type.DeclaredMethods)
                {
                    CommandAttribute attr = null;
                    var attrs             = method.GetCustomAttributes(typeof(CommandAttribute), false)
                                            .Where(a => a is CommandAttribute);
                    if (IEnumerableExt.Count(attrs) > 0)
                    {
                        attr = attrs.First() as CommandAttribute;
                    }

                    if (attr != null)
                    {
                        ProcessMethod(method, attr);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        void ProcessMethod(MethodInfo method, CommandAttribute attr)
        {
            if (!method.IsStatic)
            {
                throw new Exception(method.DeclaringType.Name + "." + method.Name +
                                    " is marked as a command, but is not static");
            }
            else
            {
                var info = new CommandInfo();
                info.Help = attr.Help;

                var parameters = method.GetParameters();
                var defaults   = new object[parameters.Length];
                var usage      = new string[parameters.Length];

                for (var i = 0; i < parameters.Length; i++)
                {
                    var p = parameters[i];
                    usage[i] = p.Name + ":";

                    if (p.ParameterType == typeof(string))
                    {
                        usage[i] += "string";
                    }
                    else if (p.ParameterType == typeof(int))
                    {
                        usage[i] += "int";
                    }
                    else if (p.ParameterType == typeof(float))
                    {
                        usage[i] += "float";
                    }
                    else if (p.ParameterType == typeof(bool))
                    {
                        usage[i] += "bool";
                    }
                    else
                    {
                        throw new Exception(method.DeclaringType.Name + "." + method.Name +
                                            " is marked as a command, but has an invalid parameter type. Allowed types are: string, int, float, and bool");
                    }

                    // no System.DBNull in PCL so we fake it
                    if (p.DefaultValue.GetType().FullName == "System.DBNull")
                    {
                        defaults[i] = null;
                    }
                    else if (p.DefaultValue != null)
                    {
                        defaults[i] = p.DefaultValue;
                        if (p.ParameterType == typeof(string))
                        {
                            usage[i] += "=\"" + p.DefaultValue.ToString() + "\"";
                        }
                        else
                        {
                            usage[i] += "=" + p.DefaultValue.ToString();
                        }
                    }
                    else
                    {
                        defaults[i] = null;
                    }
                }

                if (usage.Length == 0)
                {
                    info.Usage = "";
                }
                else
                {
                    info.Usage = "[" + string.Join(" ", usage) + "]";
                }

                info.Action = args =>
                {
                    if (parameters.Length == 0)
                    {
                        method.Invoke(null, null);
                    }
                    else
                    {
                        var param = (object[])defaults.Clone();

                        for (var i = 0; i < param.Length && i < args.Length; i++)
                        {
                            if (parameters[i].ParameterType == typeof(string))
                            {
                                param[i] = ArgString(args[i]);
                            }
                            else if (parameters[i].ParameterType == typeof(int))
                            {
                                param[i] = ArgInt(args[i]);
                            }
                            else if (parameters[i].ParameterType == typeof(float))
                            {
                                param[i] = ArgFloat(args[i]);
                            }
                            else if (parameters[i].ParameterType == typeof(bool))
                            {
                                param[i] = ArgBool(args[i]);
                            }
                        }

                        try
                        {
                            method.Invoke(null, param);
                        }
                        catch (Exception e)
                        {
                            Log(e);
                        }
                    }
                };

                _commands[attr.Name] = info;
            }
        }
Ejemplo n.º 3
0
		void processMethod( MethodInfo method, CommandAttribute attr )
		{
			if( !method.IsStatic )
			{
				throw new Exception( method.DeclaringType.Name + "." + method.Name + " is marked as a command, but is not static" );
			}
			else
			{
				var info = new CommandInfo();
				info.help = attr.help;  

				var parameters = method.GetParameters();
				var defaults = new object[parameters.Length];                 
				var usage = new string[parameters.Length];

				for( var i = 0; i < parameters.Length; i++ )
				{                       
					var p = parameters[i];
					usage[i] = p.Name + ":";

					if( p.ParameterType == typeof( string ) )
						usage[i] += "string";
					else if( p.ParameterType == typeof( int ) )
						usage[i] += "int";
					else if( p.ParameterType == typeof( float ) )
						usage[i] += "float";
					else if( p.ParameterType == typeof( bool ) )
						usage[i] += "bool";
					else
						throw new Exception( method.DeclaringType.Name + "." + method.Name + " is marked as a command, but has an invalid parameter type. Allowed types are: string, int, float, and bool" );

					// no System.DBNull in PCL so we fake it
					if( p.DefaultValue.GetType().FullName == "System.DBNull" )
					{
						defaults[i] = null;
					}
					else if( p.DefaultValue != null )
					{
						defaults[i] = p.DefaultValue;
						if( p.ParameterType == typeof( string ) )
							usage[i] += "=\"" + p.DefaultValue.ToString() + "\"";
						else
							usage[i] += "=" + p.DefaultValue.ToString();
					}
					else
					{
						defaults[i] = null;
					}
				}

				if( usage.Length == 0 )
					info.usage = "";
				else
					info.usage = "[" + string.Join( " ", usage ) + "]";

				info.action = args =>
				{
					if( parameters.Length == 0 )
					{
						method.Invoke( null, null );
					}
					else
					{
						var param = (object[])defaults.Clone();

						for( var i = 0; i < param.Length && i < args.Length; i++ )
						{
							if( parameters[i].ParameterType == typeof( string ) )
								param[i] = argString( args[i] );
							else if( parameters[i].ParameterType == typeof( int ) )
								param[i] = argInt( args[i] );
							else if( parameters[i].ParameterType == typeof( float ) )
								param[i] = argFloat( args[i] );
							else if( parameters[i].ParameterType == typeof( bool ) )
								param[i] = argBool( args[i] );
						}

						try
						{
							method.Invoke( null, param );
						}
						catch( Exception e )
						{
							log( e );
						}
					}
				};

				_commands[attr.name] = info;
			}
		}