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

  • ChannelRichard

    ChannelRicha

    7 Kasım 2008
  • Christian Atlas

    Christian At

    26 Mart 2009
  • dope2111

    dope2111

    29 HAZİRAN 2009