考虑以下情况:
class A
{
public int Id;
}
class B : A
{
}
class Main
{
public async Task<int> Create(Type type)
{
MethodInfo method = this.GetType().GetMethod("Create", new Type[] { typeof(string) }).MakeGenericMethod(new Type[] { type });
A a = await (Task<A>)method.Invoke(this, new object[] { "humpf" });
return a.Id;
}
public async Task<T> Create<T>(string name) where T : A
{
T t = await Foo.Bar<T>(name);
return t;
}
}
Run Code Online (Sandbox Code Playgroud)
呼叫new Main().Create(typeof(B))将失败
无法将'
System.Threading.Tasks.Task[B]'类型的对象强制转换为'System.Threading.Tasks.Task[A]'
我不太明白,因为在这种情况下,Generic Create<T>方法只返回始终从' …
我在TypeScript中有以下小类,其中一些公共字段被装饰:
class Company {
@dataMember
public name: string;
@dataMember
public people: Person[];
}
class Person {
// ...
}
Run Code Online (Sandbox Code Playgroud)
通过使用反射元数据,我可以确定公司属性名称和人员的类型:它们分别是构造函数String和Array,它们是预期的和逻辑的.
我的属性装饰函数:
function decorate(target: Object, propertyKey: string | symbol): void {
var reflectType = Reflect.getMetadata("design:type", target, propertyKey);
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是我如何确定数组元素的类型(构造函数)?它甚至可能吗?在上面的例子中,它应该是(引用)Person.
注意:我在实例化之前需要类型引用,因此,使用数组项动态确定类型是不可能的:没有数组项,甚至没有Array实例.
如果我有
Reflections reflections = new Reflections("my.package", classLoader, new SubTypesScanner(false));
Run Code Online (Sandbox Code Playgroud)
然后这会找到我的枚举类
Set<Class<? extends Enum>> enums = reflections.getSubTypesOf(Enum.class);
Run Code Online (Sandbox Code Playgroud)
但事实并非如此
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
Run Code Online (Sandbox Code Playgroud)
是否有一个原因?
可重复的例子:
package cupawntae;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
public class Reflector {
public static void main(String[] args) {
Reflections reflections = new Reflections("cupawntae", Reflector.class.getClassLoader(), new SubTypesScanner(false));
System.out.println("Objects: " + reflections.getSubTypesOf(Object.class));
System.out.println("Enums: " + reflections.getSubTypesOf(Enum.class));
System.out.println("Enum's superclass: " + Enum.class.getSuperclass());
}
}
Run Code Online (Sandbox Code Playgroud)
枚举类:
package cupawntae;
public enum MyEnum {
}
Run Code Online (Sandbox Code Playgroud)
输出:
Objects: [class cupawntae.Reflector]
Enums: [class cupawntae.MyEnum]
Enum's superclass: class …Run Code Online (Sandbox Code Playgroud) 对于Rust的结构,是否有一些等效的JS的Object.keys()?
我需要从结构字段名称生成CSV标题(我使用rust-csv).
struct Export {
first_name: String,
last_name: String,
gender: String,
date_of_birth: String,
address: String
}
//... some code
let mut wrtr = Writer::from_file("/home/me/export.csv").unwrap().delimiter(b'\t');
wrtr.encode(/* WHAT TO WRITE HERE TO GET STRUCT NAMES as tuple of strings or somethings */).is_ok()
Run Code Online (Sandbox Code Playgroud) 我怎样才能获得Kotlin数据类的列表declaredFields呢?像Java一样getDeclaredFields()
如果这是可能的,还可以过滤public和private字段?(像Java一样Modifier.isPrivate(field.getModifiers()))
我如何知道我可以从reply对象/接口访问的字段?我尝试过反射,但似乎你必须首先知道字段名称.如果我需要知道可用的所有字段怎么办?
// Do sends a command to the server and returns the received reply.
Do(commandName string, args ...interface{}) (reply interface{}, err error)
Run Code Online (Sandbox Code Playgroud) 关于这个问题,我一直在阅读关于Stackoverflow的很多问题,但无法找到解决方案或解决我的问题.如果已经有人,如果有人提示,我将不胜感激......
我的问题/问题是,是否可以完全禁用不值得信赖的代码的反射?功能如getDeclaredMethods()(见test.java).我已经有了一个Java安全管理器,如果代码试图写/读/等,它会抛出安全例外....
如果有可能,有人可以告诉我怎么样?
布鲁诺
test.java
TestClass cls = new TestClass();
Class c = cls.getClass();
// returns the array of Method objects
Method[] m = c.getDeclaredMethods();
for(int i = 0; i < m.length; i++) {
System.out.println("method = " + m[i].toString());
}
Run Code Online (Sandbox Code Playgroud) 我有一个方法签名 execute<TResult>(): Observable<TResult>
我如何获得TResult类型的名称?
例:
execute<ViewModel> - >"ViewModel"是我需要的结果.
在typescript中,是否可以使用属性装饰器为类设置元数据?请考虑以下代码.类装饰器的"目标"显然与属性装饰器的"目标"不同.我可以从另一个中获得一个吗?
import 'reflect-metadata';
const MY_CLASS_DECORATOR_KEY = 'MyClassDecoratorKey';
const MY_PROPERTY_DECORATOR_KEY = 'MyPropertyDecoratorKey';
export const MyClassDecorator = options => {
return function (target) {
console.log('class target: ' , target);
Reflect.defineMetadata(MY_CLASS_DECORATOR_KEY, options, target);
};
};
export const MyPropertyDecorator = (options): PropertyDecorator => {
return (target, property) => {
console.log('property target: ' , target);
const metadata = Reflect.getMetadata(MY_PROPERTY_DECORATOR_KEY, target) || {};
metadata[property] = options;
Reflect.defineMetadata(MY_PROPERTY_DECORATOR_KEY, metadata, target);
};
};
@MyClassDecorator('my class decorator value')
class MyClass {
@MyPropertyDecorator('first my property decorator value')
myFirstProperty: any;
@MyPropertyDecorator('second …Run Code Online (Sandbox Code Playgroud) 有没有办法使用Reflect访问go 1.8中未导出的字段?这似乎不再起作用:https://stackoverflow.com/a/17982725/555493
请注意,reflect.DeepEqual工作正常(也就是说,它可以访问未导出的字段),但我无法做出该功能的正面或反面.这是一个go playarea,它显示了它的实际效果:https://play.golang.org/p/vyEvay6eVG.src代码如下
import (
"fmt"
"reflect"
)
type Foo struct {
private string
}
func main() {
x := Foo{"hello"}
y := Foo{"goodbye"}
z := Foo{"hello"}
fmt.Println(reflect.DeepEqual(x,y)) //false
fmt.Println(reflect.DeepEqual(x,z)) //true
}
Run Code Online (Sandbox Code Playgroud) reflection ×10
java ×3
typescript ×3
decorator ×2
generics ×2
go ×2
arrays ×1
async-await ×1
c# ×1
enums ×1
kotlin ×1
metadata ×1
policies ×1
reflections ×1
reply ×1
rust ×1
struct ×1
typename ×1