在.Net中使用反射,有什么区别:
if (foo.IsAssignableFrom(typeof(IBar)))
Run Code Online (Sandbox Code Playgroud)
和
if (foo.GetInterface(typeof(IBar).FullName) != null)
Run Code Online (Sandbox Code Playgroud)
哪个更合适,为什么?
什么时候一个或另一个失败?
我有一个自定义属性,在我的自定义属性的构造函数中我想将我的属性的属性值设置为我的属性应用于的属性的类型,是否有某种程度来访问该属性应用于的成员从我的属性类里面?
我需要调用Class.getMethod(java.lang.String,java.lang.Class ...)来获取一个方法,其中一个varargs参数是一个varargs.
目前我正在尝试:
// to get jdbcTemplate.queryForObject(RowMapper, Object...)
jdbcTemplate.getClass().getMethod("queryForObject", RowMapper.class, Object.class);
Run Code Online (Sandbox Code Playgroud)
哪个结果,不足为奇
Caused by: java.lang.NoSuchMethodException: org.springframework.jdbc.core.simple.SimpleJdbcTemplate.queryForObject(java.lang.String, org.springframework.jdbc.core.RowMapper, java.lang.Object)
at java.lang.Class.throwNoSuchMethodException(Class.java:283)
at java.lang.Class.getMethod(Class.java:825)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
这是使用反射更新属性的最快方法吗?假设该属性始终为int:
PropertyInfo counterPropertyInfo = GetProperty();
int value = (int)counterPropertyInfo.GetValue(this, null);
counterPropertyInfo.SetValue(this, value + 1, null);
Run Code Online (Sandbox Code Playgroud) 我的应用程序中有许多"作业",每个作业都有一个需要调用的方法列表及其参数.基本上称为包含以下对象的列表:
string Name;
List<object> Parameters;
Run Code Online (Sandbox Code Playgroud)
所以基本上,当一个作业运行时,我想通过这个列表枚举,并调用相关的方法.例如,如果我有如下方法:
TestMethod(string param1, int param2)
Run Code Online (Sandbox Code Playgroud)
我的方法对象是这样的:
Name = TestMethod
Parameters = "astring", 3
Run Code Online (Sandbox Code Playgroud)
是否有可能做到这一点?我想反射将成为关键.
在使用jhat分析堆转储的同时,我观察到了许多DelegatingClassLoader实例,虽然它们没有在代码中显式调用.我希望这是一种反射优化机制.有人知道细节吗?
有一个类似的
class MyClass {
static var1 = "a";
static var2 = "b";
}
Run Code Online (Sandbox Code Playgroud)
...我想在运行时检索静态成员及其值; 就像是
Array(
"var1" => "a",
"var2" => "b"
)
Run Code Online (Sandbox Code Playgroud)
有没有办法在PHP中这样做?
它似乎不起作用:
$ref = new ReflectionObject($obj);
if($ref->hasProperty('privateProperty')){
print_r($ref->getProperty('privateProperty'));
}
Run Code Online (Sandbox Code Playgroud)
它进入IF循环,然后抛出一个错误:
属性privateProperty不存在
:|
$ref = new ReflectionProperty($obj, 'privateProperty') 不起作用......
该文档页面列出了一些常量,包括IS_PRIVATE.如果我无法访问私有财产,我怎么能使用它?
在Java中解析类型意味着什么?
以下是我尝试理解它们的一些用法示例:
如果基础字段的类型是类型变量或参数化类型,则创建它.否则,它就解决了.
根据此类型表示的类型上下文解析给定类型.例如:
new TypeToken<List<String>>() {}.resolveType(
List.class.getMethod("get", int.class).getGenericReturnType())
=> String.class
Run Code Online (Sandbox Code Playgroud)
在运行时,我不知道变量v1是什么类型.出于这个原因,我写了很多if else陈述:
if (v1 is ShellProperty<int?>)
{
v2 = (v1 as ShellProperty<int?>).Value;
}
else if (v1 is ShellProperty<uint?>)
{
v2 = (v1 as ShellProperty<uint?>).Value;
}
else if (v1 is ShellProperty<string>)
{
v2 = (v1 as ShellProperty<string>).Value;
}
else if (v1 is ShellProperty<object>)
{
v2 = (v1 as ShellProperty<object>).Value;
}
Run Code Online (Sandbox Code Playgroud)
唯一的区别在于ShellProperty<AnyType>.
因此if else,我决定使用反射来在运行时获取属性类型,而不是使用大量语句编写它:
Type t1 = v1.GetType().GetProperty("Value").PropertyType;
dynamic v2 = (v1 as ShellProperty<t1>).Value;
Run Code Online (Sandbox Code Playgroud)
此代码获取PropertyType的v1,并将其分配给本地变量t1,但在那之后,我的编译器说:
t1是一个变量但是像一个类型一样使用
所以它不允许我在t1 …
reflection ×10
c# ×5
.net ×2
java ×2
php ×2
attributes ×1
class ×1
classloader ×1
jvm ×1
methods ×1
performance ×1
properties ×1
static ×1
types ×1
variables ×1