反射執行類的方法

public static void excuteMethod(String className, String methodName, Object[] args)
    {
        try
        {
            Class<?> c = Class.forName(className);
            if (args == null || args.length < 1)
            {
                Method m = c.getMethod("show", new Class[] {});
                m.invoke(c, new Object[] {});
            }
            else
            {
                Class<?>[] classes = new Class[args.length];
                for (int i = 0; i < args.length; i++)
                {
                    classes[i] = args[i].getClass();
                }
                Method m = c.getMethod("show", classes);
                m.invoke(c, args);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章