标签: reflection

在非泛型方法中使用反射等待Task <TDerived>的结果

考虑以下情况:

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>方法只返回始终从' …

c# generics reflection async-await

9
推荐指数
1
解决办法
2347
查看次数

如何使用Reflection API在TypeScript中获取数组项类型?

我在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实例.

arrays reflection decorator typescript

9
推荐指数
1
解决办法
2625
查看次数

为什么不reflect.getSubTypesOf(Object.class)找到枚举?

如果我有

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)

java reflection enums reflections

9
推荐指数
1
解决办法
5188
查看次数

如何在Rust中获取struct字段名?

对于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)

reflection introspection rust

9
推荐指数
1
解决办法
4566
查看次数

Kotlin反射 - 获取类的所有字段名称

我怎样才能获得Kotlin数据类的列表declaredFields呢?像Java一样getDeclaredFields()

如果这是可能的,还可以过滤public和private字段?(像Java一样Modifier.isPrivate(field.getModifiers()))

java reflection kotlin

9
推荐指数
1
解决办法
7532
查看次数

从界面获取所有字段

我如何知道我可以从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)

reflection struct go reply

9
推荐指数
1
解决办法
1万
查看次数

Java Security Manager完全禁用反射

关于这个问题,我一直在阅读关于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)

java reflection securitymanager policies

9
推荐指数
2
解决办法
2977
查看次数

是否可以获取泛型类型的类型名称?

我有一个方法签名 execute<TResult>(): Observable<TResult>

我如何获得TResult类型的名称?

例:

execute<ViewModel> - >"ViewModel"是我需要的结果.

generics reflection typename typescript typescript2.0

9
推荐指数
1
解决办法
5238
查看次数

可以使用typescript属性装饰器为类设置元数据吗?

在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)

reflection metadata decorator typescript

9
推荐指数
1
解决办法
5536
查看次数

在golang/reflect中访问未导出的字段?

有没有办法使用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 go private-members

9
推荐指数
3
解决办法
7547
查看次数