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

  • Anthony Cumia

    Anthony Cumi

    5 EYLÜL 2006
  • Paste Magazine

    Paste Magazi

    28 AĞUSTOS 2008
  • TheScorpioTechno

    TheScorpioTe

    15 Aralık 2010