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

  • Matthew Smith

    Matthew Smit

    24 Mayıs 2010
  • picster

    picster

    20 NİSAN 2006
  • steven johns

    steven johns

    11 Mart 2011