Invoking a static method using reflection
// 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");
In case the method is private use getDeclaredMethod()
instead of getMethod()
. And call setAccessible(true)
on the method object.
Invoke static method using reflection
Problem solved:)
ModelA and ModelB implement the same interface, for example Model (not important) ,model.getClass()
is an important part of his
public static void insert(Context context, Model model) {
Class<?> clazz = Class.forName(mClass); //Controller A or B
Method method = clazz.getMethod("insert", Context.class, model.getClass());
method.invoke(null, new Object[]{context, model}));
}
Now i can use this :
Controller.insert(this, myModelA);
Controller.insert(this, myModelB);
Thanks all.
Call static method with reflection
As the documentation for MethodInfo.Invoke states, the first argument is ignored for static methods so you can just pass null.
foreach (var tempClass in macroClasses)
{
// using reflection I will be able to run the method as:
tempClass.GetMethod("Run").Invoke(null, null);
}
As the comment points out, you may want to ensure the method is static when calling GetMethod
:
tempClass.GetMethod("Run", BindingFlags.Public | BindingFlags.Static).Invoke(null, null);
Calling a static method of a class using reflection
Your main problem is you need to specify the full namespace of SA010 when using GetType.
Type type = Type.GetType("SomeNamespace.SA010");
However if you are not dynamicly generating the name a easier solution is use typeof
, this does not require you to entire the namespace in if the type is already within scope.
Type type = typeof(SA010);
2nd issue you will run in to once you fix the type, if a method is static you don't create a instance of it, you just pass null
in for the instance for the Invoke
call.
private SA GetData()
{
Type type = typeof(SA010);
MethodInfo methodInfo = type.GetMethod("GetSA");
return (SA)methodInfo.Invoke(null, null);
}
Using reflection to call static method for return value
You don't want null
as your argument or parameter list. Instead you can do
Method method = classLoader.loadClass("testClass").getMethod("getInstance", new Class[0]);
Object object = method.invoke(null, new Object[0]);
or the following as they are varargs methods.
Method method = classLoader.loadClass("testClass").getMethod("getInstance");
Object object = method.invoke(null);
// or works but is perhaps confusing.
Object object = method.invoke(null, null);
In Java, how to use reflection to get a static method and execute it?
directly from the interwebz
Class<?> class1;
try {
class1 = Class.forName(CLASS);
Method method = class1.getMethod(METHOD, String.class);
Object o = method.invoke(null, NAME);
System.out.println(o);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Kotlin - Get static functions with reflection
Actually, this code works with Kotlin classes, but usually Kotlin does not have any static members. You can acquire companion members with the following code:
fun companionMembersFromClass(clazz: KClass<*>): List<Something> {
val members = clazz.companionObject?.members ?: emptyList()
...
}
Similarly, you can acquire the instance of the companion with: clazz.companionObjectInstance
.
Alternatively, you can reference a companion class directly with e.g.: MyClass.Companion::class
and then list its non-static members.
Related Topics
Unzip Files Programmatically in .Net
How to Get All Classes Within a Namespace
C# "Parameter Is Not Valid." Creating New Bitmap
Swap Two Variables Without Using a Temporary Variable
How to Programmatically Fill in a Form and 'Post' a Web Page
Uwp Binding in Style Setter Not Working
How to Run a C# Console Application with the Console Hidden
Getting the Current Tab's Url from Google Chrome Using C#
Large Object Heap Fragmentation
Why Does Boolean.Tostring Output "True" and Not "True"
How to Handle Key Press Event in Console Application
How to Make Databinding Type Safe and Support Refactoring
Select Multiple Items from a Datagrid in an Mvvm Wpf Project
Is Task.Run Considered Bad Practice in an Asp .Net MVC Web Application