SORU
18 Mart 2010, PERŞEMBE


Statik bir yöntem kullanarak yansıma çağırma

Statik olan main yöntemini çağırmak istiyorum. Tür nesne Class, ama bu sınıfın bir örneğini oluşturmak mümkün değilim ve ayrıca static yöntemi çağırmak için mümkün main aldım.

CEVAP
18 Mart 2010, PERŞEMBE


// String.class here is the parameter type, that might not be the case with you
Method method = clazz.getMethod("methodName", String.class);
Object o = method.invoke(null, "whatever");

Davada yöntemi getMethod() yerine özel kullanım getDeclaredMethod(). Ve yöntemi setAccessible(true) nesne arayın.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Elliot Davin

    Elliot Davin

    28 Kasım 2008
  • Friday NightFort

    Friday Night

    15 EYLÜL 2011
  • magnum33563

    magnum33563

    8 NİSAN 2011