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

  • cosmicrocketman

    cosmicrocket

    17 NİSAN 2006
  • hotstrikegently

    hotstrikegen

    26 AĞUSTOS 2011
  • ThisWeekYT

    ThisWeekYT

    14 Mart 2013