如果您有一个通过反射访问的枚举,您将如何将它的值传递给 method.invoke 调用。
会不会是这样的(为简单起见,显示为静态方法)
Class enumClazz = Class.forName("mypkg.MyEnum",true,MyClassLoader);
Class myReflectedClazz = Class.forName("mypkg.MyClass",true,MyClassLoader);
Field f = enumClazz.getField("MyEnumValue");
Method m = myReflectedClazz.getMethod("myMethod",enumClazz);
m.invoke(null,f.get(null));
Run Code Online (Sandbox Code Playgroud) 我有类似这样的课程
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
A a = new C();
a.method();
Console.ReadLine();
}
}
public class A
{
public virtual void method()
{
Console.WriteLine("METHOD FROM A");
}
}
public class B : A { }
public class C : B
{
public override void method()
{
Console.WriteLine("METHOD FROM C");
}
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,打印"METHOD FROM C"
但
如果我有这样的情况
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
A a = new C(); …Run Code Online (Sandbox Code Playgroud) 我使用 Java 中的 ExecutorService 来调用带有invokeAll(). 之后,我得到了结果集future.get()。以与创建线程相同的顺序接收结果非常重要。
这是一个片段:
try {
final List threads = new ArrayList();
// create threads
for (String name : collection)
{
final CallObject object = new CallObject(name);
threads.add(object);
}
// start all Threads
results = pool.invokeAll(threads, 3, TimeUnit.SECONDS);
for (Future<String> future : results)
{
try
{
// this method blocks until it receives the result, unless there is a
// timeout set.
final String rs = future.get();
if (future.isDone())
{
// if future.isDone() …Run Code Online (Sandbox Code Playgroud) 我有一个域对象,为了这个问题的目的,我将使用以下私有变量调用Person:
String name
int age
Run Code Online (Sandbox Code Playgroud)
每个人都有吸气剂和二传手.现在我还有Map<String, String>以下条目:
name, phil
age, 35
Run Code Online (Sandbox Code Playgroud)
我想在Person类中填充所有setter方法的列表,然后循环遍历此列表并使用map中的值调用每个方法.
这是否可能,因为我在网上看不到任何接近这个的例子.非常感谢例子.
当从 Method.invoke 方法调用该方法时,我似乎无法在我的代码中捕获异常。如何从方法本身内部捕获它?
void function() {
try {
// code that throws exception
}
catch( Exception e ) {
// it never gets here!! It goes straight to the try catch near the invoke
}
}
try {
return method.invoke(callTarget, args);
}
catch( InvocationTargetException e ) {
// exception thrown in code that throws exception get here!
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
有什么方法可以避免MyMethodDelegate在这样的场景中明确声明?
bool MyMethod(string x)
{
//...
}
BeginInvoke(new MyMethodDelegate(MyMethod), x);
Run Code Online (Sandbox Code Playgroud)
我知道 lambdas a-la ()=>MyMethod(x),但是有时我想避免它们,因为它们会破坏编辑并继续。
编辑:只是BeginInvoke(MyMethod, x)不起作用:
The best overloaded method match for 'System.Windows.Forms.Control.BeginInvoke(System.Delegate, params object[])' has some invalid arguments
Argument 1: cannot convert from 'method group' to 'System.Delegate'
Argument 2: cannot convert from 'string' to 'object[]'
Run Code Online (Sandbox Code Playgroud)
BeginInvoke 定义如下:
public IAsyncResult BeginInvoke(Delegate method, params object[] args);
Run Code Online (Sandbox Code Playgroud)
它没有指定任何特定的委托类型,因此编译器无法检测从哪个委托类型实例化 BeginInvoke(MyMethod. x)
我有一个"调用"问题,我无法解决.我会尽量在我的描述中尽可能彻底,但我是新手,所以请耐心等待,如果您需要更多信息,请告诉我.
我有一个运行后台线程,当提示时将禁用主线程上创建的表单上的一堆复选框.为了做到这一点,我需要使用invoke和委托安全地交叉线程,但我必须做错了.最重要的是,当我在调试器中检查这个时,我看到它会在代码的ACTION部分运行两次InvokeRequired.我可以通过用一个包围ACTION来解决这个问题else,虽然它不会运行else两次,但仍然会尝试再次通过该方法.
delegate void ManualCurtainShuttoffHandler();
public void ManualCurtainShutoff()
{
if (InvokeRequired)
{
Invoke(new ManualCurtainShuttoffHandler(ManualCurtainShutoff));
}
// ACTION: Disable check boxes
}
Run Code Online (Sandbox Code Playgroud)
我只想知道为什么它会两次运行该方法.如果您需要更多信息,请告诉我,我很乐意与您分享.
我有关于在以下示例jOOQ语句中调用方法count()的问题:
create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
.from(AUTHOR)
.join(BOOK).on(AUTHOR.ID.equal(BOOK.AUTHOR_ID))
.where(BOOK.LANGUAGE.eq("DE"))
.and(BOOK.PUBLISHED.gt(date("2008-01-01")))
.groupBy(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
.having(count().gt(5))
.orderBy(AUTHOR.LAST_NAME.asc().nullsFirst())
.limit(2)
.offset(1)
.forUpdate()
.of(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
Run Code Online (Sandbox Code Playgroud)
我试图创建这样的机制来调用方法而不使用对象/类引用,但我放弃了.它真的有可能实现吗?
感谢帮助.
Wicia
我有以下代码:
using System;
using System.Reflection;
namespace TestInvoke
{
class Program
{
static void Main( string[] args )
{
Method1();
Console.WriteLine();
Method2();
}
static void Method1()
{
try
{
MyClass m = new MyClass();
m.MyMethod( -3 );
}
catch ( Exception e )
{
Console.WriteLine( e.Message );
}
}
static void Method2()
{
try
{
string className = "TestInvoke.MyClass";
string methodName = "MyMethod";
Assembly assembly = Assembly.GetEntryAssembly();
object myObject = assembly.CreateInstance( className );
MethodInfo methodInfo = myObject.GetType().GetMethod( methodName );
methodInfo.Invoke( myObject, …Run Code Online (Sandbox Code Playgroud) 我想收到结果动态调用另一个jar中的类.
例如,
名为A.jar的文件中的"A"目录.
名为B.jar的文件中的"B"目录.
我想动态调用一个A.jar文件类到B.jar文件.
这是A.jar文件的主要类.
因为消息交换技术,不考虑套接字和RMI.
主类(B.jar)
public class main {
public static void main(String[] args) {
//It dynamically creates an object of a Message Class in A.jar.
//And it invoke the getMessage function.
//And Save the return value.
}}
Run Code Online (Sandbox Code Playgroud)
消息类(A.jar)
public class message{
public String getMessage(){
return "Hello!";
}
}
Run Code Online (Sandbox Code Playgroud) invoke ×10
java ×6
c# ×4
methods ×4
reflection ×4
base-class ×1
callable ×1
delegates ×1
enums ×1
exception ×1
jar ×1
jooq ×1
new-operator ×1
sql ×1
try-catch ×1