using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace MethodInfoInvokeDemo
{
public class ReflectTest
{
public void MethodWithNoParaNoReturn()
{
Console.WriteLine("不带参数且不返回值的方法");
}
public string MethodWithNoPara()
{
Console.WriteLine("不带参数且有返回值的方法");
return "MethodWithNoPara";
}
public string Method1(string str)
{
Console.WriteLine("带参数且有返回值的方法");
return str;
}
public string Method2(string str, int index)
{
Console.WriteLine("带参数且有返回值的方法");
return str + index.ToString();
}
public string Method3(string str, out string outStr)
{
outStr = "bbbb";
Console.WriteLine("带参数且有返回值的方法");
return str;
}
public static string StaticMethod()
{
Console.WriteLine("静态方法");
return "cccc";
}
}
class Program
{
static void Main(string[] args)
{
Type type = typeof(ReflectTest);
object reflectTest = Activator.CreateInstance(type);
//不带参数且不返回值的方法的调用
MethodInfo methodInfo = type.GetMethod("MethodWithNoParaNoReturn");
methodInfo.Invoke(reflectTest, null);
Console.WriteLine();
//不带参数且有返回值的方法的调用
methodInfo = type.GetMethod("MethodWithNoPara");
Console.WriteLine(methodInfo.Invoke(reflectTest, null).ToString());
Console.WriteLine();
//带参数且有返回值的方法的调用
methodInfo = type.GetMethod("Method1", new Type[]{typeof(string)});
Console.WriteLine(methodInfo.Invoke(reflectTest, new object[]{"测试"}).ToString());
Console.WriteLine();
//带多个参数且有返回值的方法的调用
methodInfo = type.GetMethod("Method2", new Type[] { typeof(string), typeof(int) });
Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试", 100 }).ToString());
//Console.WriteLine();
//methodInfo = type.GetMethod("Method3", new Type[] { typeof(string), typeof(string) });
//string outStr = "";
//Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试", outStr }).ToString());
Console.WriteLine();
//静态方法的调用
methodInfo = type.GetMethod("StaticMethod");
Console.WriteLine(methodInfo.Invoke(null, null).ToString());
Console.ReadKey();
}
}
}
分享到:
相关推荐
标题中的问题“scrcpy投屏 AssertionError: java.lang.reflect.InvocationTargetException”是用户在尝试使用Scrcpy时遇到的一个常见错误。这个错误通常意味着在执行某个方法时,Java运行时环境遇到了未预期的情况。...
标题中的“解决axis2-CodegenWizardPluginBUG- java.lang.reflect.InvocationTargetException”指的是在使用Apache Axis2的CodegenWizardPlugin工具时遇到的一个错误。这个工具是Axis2框架的一部分,用于自动生成...
java.lang.reflect.Proxy 学习资料 讲解 例子 源码 java.lang.reflect.Proxy 学习资料 讲解 例子 源码 java.lang.reflect.Proxy 学习资料 讲解 例子 源码
通过上述步骤,可以有效地在 MyEclipse 6.0 中安装 Axis2 插件,并解决在使用过程中可能出现的 `java.lang.reflect.InvocationTargetException` 错误。这将极大地提高使用 MyEclipse 开发 Web 服务的效率和便利性。
kotlin-reflect.jar
MyEclipse axis2 wsdl java.lang.reflect.invocationtargetexception code gen 大家要注意一定要仔细,这个问题基本上缺少包引起的,而且一定要clean 如果需要axis2插件 以及这个plugins中的包在我的其他资源里面有
在Java编程语言中,`java.lang.reflect`包是核心库的一部分,它提供了运行时访问类、接口、字段和方法的能力。这个包对于理解和操作对象的动态特性至关重要,尤其是在实现反射机制时。反射允许我们在程序运行期间...
Could not download kotlin-reflect.jar 这个问题 是墙的原因 没有下载下来kotlin-reflect.jar的jar包资源。 把下载的kotlin-reflect-1.3.70.jar,放在版本文件的子目录中,重新启动电脑就可以。网上都有对应的操作...
在JDK 1.8中,这个包的扩展可能涉及到对反射机制的优化或者增加了新的API,使得开发者能更高效地使用反射。 `unsafe`通常指的是`sun.misc.Unsafe`类,这是一个强大的内部类,它提供了一些直接内存操作和低级资源...
1. 动态代理:使用`java.lang.reflect.Proxy`和`java.lang.reflect.InvocationHandler`可以创建动态代理对象,实现AOP(面向切面编程)。 2. 数据库操作框架:如MyBatis,通过反射动态生成SQL语句并执行。 3. XML...
赠送jar包:scala-reflect-2.12.10.jar; 赠送原API文档:scala-reflect-2.12.10-javadoc.jar; 赠送源代码:scala-reflect-2.12.10-...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
// import static org.fest.reflect.core.Reflection.*; // Loads the class 'org.republic.Jedi' Class jediType = type("org.republic.Jedi").load(); // Loads the class 'org.republic.Jedi' as 'org.repu
scala-reflect.jar
免费共享,很详细的介绍了反射机制的原理,适合追根究底的java学习者
使用 `reflect.Value.NumMethod()` 和 `reflect.Value.Method(i)` 来遍历一个结构体的所有方法,并将它们注册到 `Handler` 结构体中。 ```go func main() { handlers := make(map[string]*Handler) v := reflect....
`reflect-metadata`库在此基础上扩展了几个新的方法,如`Reflect.metadata`、`Reflect.getMetadata`、`Reflect.hasMetadata`等。 `Reflect.metadata`方法用于定义元数据键和值。它接受两个参数:元数据键(通常是...
ES2015最符合规范的Reflect.getPrototypeOf sham / Reflect.getPrototypeOf / Reflect.getPrototypeOf ,可在尽可能多的引擎中使用-特别是具有__proto__支持或ES6的任何引擎。 内置类型也可以在较旧的引擎中正常...
在ES5中,如果想要传递动态数量的参数,必须使用`new`操作符结合`Function.prototype.apply`,但`Reflect.construct`在ES5中提供了一种解决方案,即使不支持扩展运算符,也可以实现类似的功能。 5. 掌控访问器或...
从给定的文件信息中,我们可以总结出以下知识点: wireless耳机技术 * Bluetooth版本:5.0 * Bluetooth frequency:2.402 GHz – 2.480 GHz * Bluetooth传输功率: * Bluetooth调制方式:GFSK, π/4 DQPSK, 8DPSK ...
使用Reflect Metadata API,我们需要两个主要的方法:`Reflect.metadata` 和 `Reflect.getMetadata`。`Reflect.metadata` 用于定义或更新元数据,而 `Reflect.getMetadata` 用于检索元数据。 例如,我们可以在类...