标签: reflection

arguments.callee的替代方案

我有一个EventListener,它监听整个文档并记录击键,但是我想在满足某些条件时删除这个监听器.

以下是我的代码片段:

document.addEventListener('keyup', function(e) {
    var letter_entered = String.fromCharCode(e.keyCode).toLowerCase();
    player.makeGuess(letter_entered);

    if(player.win_status === true || player.lose_status === true) {
        document.removeEventListener('keyup', arguments.callee, false);
    }
});
Run Code Online (Sandbox Code Playgroud)

这是有效的,但根据Mozilla Developer Docs,此方法已被弃用.

我知道我可以简单地命名这个函数,但有没有替代方法可以让我继续使用未命名的函数?

javascript reflection porting arguments deprecated

8
推荐指数
2
解决办法
7554
查看次数

加载包中的每个类

我正在使用反射从classes字段中获取所有已加载的类ClassLoader,然后检查是否getPackage等于我正在搜索的包.我遇到的一个问题是这些类没有被加载,ClassLoader但我无法自己加载它们,Class.forName因为我不知道类的名称,因为它们是动态加载的并且总是在变化.所有类的共同点是它们的扩展Module.我如何从包中加载所有类?

提前致谢.

java reflection dynamic

8
推荐指数
1
解决办法
667
查看次数

是否有通用类使用静态方法的解决方法?

我有一个相当简单的问题,但在C#中似乎没有解决方案.

我有大约100个Foo类,每个类都实现一个static FromBytes()方法.还有一些泛型类应该使用这些方法FromBytes().但是泛型类不能使用这些static FromBytes()方法,因为它们T.FromBytes(...)是非法的.

我是否遗漏了某些内容或者无法实现此功能?

public class Foo1
{
    public static Foo1 FromBytes(byte[] bytes, ref int index)
    {
        // build Foo1 instance
        return new Foo1()
        {
            Property1 = bytes[index++],
            Property2 = bytes[index++],
            // [...]
            Property10 = bytes[index++]
        };
    }

    public int Property1 { get; set; }
    public int Property2 { get; set; }
    // [...]
    public int Property10 { get; set; }
}

//public class Foo2 { ... }
// …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection

8
推荐指数
2
解决办法
476
查看次数

转到template/html迭代以从struct生成表

给定一个结构集合,如何使用"范围"模板迭代器打印出一个表,该表为每个结构分配一行,每个字段的列没有明确命名字段?

container := []Node

type Node struct {
    Contact_id        int
    Employer_id       int
    First_name        string
    Middle_name       string
    Last_name         string
}
Run Code Online (Sandbox Code Playgroud)

模板代码:

{{range .container}}

<tr>
<td>{{.Prefix}}</td>
<td>{{.First_name}}</td>
<td>{{.Middle_name}}</td>
<td>{{.Last_name}}</td>

<td>{{.Contact_id}}</td>
<td>{{.Employer_id}}</td>

</tr>
{{end}}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用迭代值时

{{range .container}}
 {{range .}}
 <td>{{.}}</td> 
{{end}}
{{end}}
Run Code Online (Sandbox Code Playgroud)

有人告诉我,我无法迭代价值观.有没有干净的方法来做到这一点?

html reflection templates iterator go

8
推荐指数
1
解决办法
8480
查看次数

BeanUtils不适用于链设置器

例如

class tester
{
    @Test
    public void testBeanUtils() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException
    {
        Stranger stranger = new Stranger();
        BeanUtils.setProperty(stranger,"name","wener");
        BeanUtils.setProperty(stranger,"xname","xwener");
        BeanUtils.setProperty(stranger,"yname","ywener");

        System.out.println(stranger);
    }
    @Data// lombok annotation generate all setter and getter
    public static class Stranger
    {
        @Accessors(chain = true)// generate chained setter
        String name;
        String xname;
        String yname;

        public Stranger setYname(String yname)// no lombok, still not work
        {
            this.yname = yname;
            return this;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的输出:

TestValues.Stranger(name=null, xname=xwener, yname=null)
Run Code Online (Sandbox Code Playgroud)

这有什么问题?连锁二传手是一件好事.有什么建议?

编辑

再次回到这个问题.这次我无法删除Accessors chain.现在,我commons-lang3用来实现.

// force …
Run Code Online (Sandbox Code Playgroud)

java reflection chaining lombok apache-commons-beanutils

8
推荐指数
2
解决办法
1735
查看次数

如何从Java 8中的getAnnotatedParameterTypes()获取泛型类型信息?

它似乎getAnnotatedParameterTypes()返回一个包含AnnotatedType原始类型而不是泛型类型的s 数组.例如:

public <T> void genericMethod(T t) {
}

@Test
public void testAnnotatedTypes() throws ReflectiveOperationException {
    Method method = getClass().getMethod("genericMethod", Object.class);

    Type type = method.getGenericParameterTypes()[0];
    assertTrue(type instanceof TypeVariable);

    AnnotatedType annotatedType = method.getAnnotatedParameterTypes()[0];

    // This fails; annotatedType implements only AnnotatedType
    assertTrue(annotatedType instanceof AnnotatedTypeVariable);

    // This fails too; type is a TypeVariable while annotatedType.getType() is
    // Object.class
    assertEquals(type, annotatedType.getType());
}
Run Code Online (Sandbox Code Playgroud)

分歧的原因是什么getGenericParameterTypes()?

java generics reflection java-8 type-annotation

8
推荐指数
1
解决办法
949
查看次数

在Scala中访问注释值

TL; DR:基本上,我正在寻找Java的Scala等价物:

(MyAnnotation) Thing.getClass().getAnnotations()[0]
Run Code Online (Sandbox Code Playgroud)

尽管我可以根据他们的类型愉快地发现注释和查询,但我似乎无法从scala.reflect.runtime.universe.Annotation我的实际类型中获取.

scala> // Declare an annotation (it seems StaticAnnotation means runtime
scala> // retention)
scala> case class MyAnnotation(x: Int, y: String) extends scala.annotation.StaticAnnotation
defined class MyAnnotation

scala> // Make a thing decorated with MyAnnotation
scala> @MyAnnotation(x=5, y="cool") case class Thing()
defined class Thing

scala> // Look at the annotation on the Thing...the runtime clearly
scala> // understands the values on it
scala> val annotation = scala.reflect.runtime.universe.typeOf[Thing].typeSymbol.asClass.annotations(0)
annotation: reflect.runtime.universe.Annotation = MyAnnotation(5, "cool")

scala> // …
Run Code Online (Sandbox Code Playgroud)

reflection scala

8
推荐指数
2
解决办法
3428
查看次数

PHP:ReflectionParameter,isOptional vs isDefaultValueAvailable

两者有什么区别.这两者都以完全相同的方式工作.

public static function getArgsArray($reflectionMethod,$argArray){
    $arr = array();
    foreach($reflectionMethod->getParameters() as $key => $val){
        $arr[$val->getName()] = isset($argArray[$val->getName()]) ?
        $argArray[$val->getName()] : (isset($_REQUEST[$val->getName()])
                ? $_REQUEST[$val->getName()] : ($val->*isDefaultValueAvailable()*  ? $val->getDefaultValue() : NULL));
    }
    return $arr;
}
Run Code Online (Sandbox Code Playgroud)

php reflection

8
推荐指数
2
解决办法
874
查看次数

有可能分辨出被覆盖和隐藏的方法吗?

考虑以下变体:

class A
{
    public virtual void Doit()
    {
    }
}

class B : A
{
    public new virtual void Doit()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

class B : A
{
    public override virtual void Doit()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法找到返回的调用结果的差异 typeof(B).GetMethod("Doit");

在这两种情况下MethodInfo.DecalringType,B类和其他属性看起来都是一样的.我错过了什么或者没有办法区分它们吗?



更新:

当我在LINQPAd中运行示例时,我发现Attributes属性略有不同:

对于new virtual值为- PrivateScope,公共,虚拟HideBySig,VtableLayoutMask
为override- PrivateScope,公共,虚拟HideBySig



更新2:

我用Google搜索VtableLayoutMask并回到StackOverflow

Udate 3:

结果代码:

public static class MethodInfoExtensions
{
    public static bool IsOverriden(this MethodInfo method)
    {
        Contract.Requires<ArgumentNullException>(method != null, …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection

8
推荐指数
1
解决办法
465
查看次数

如何在编译时获取通用参数类型名称?

我正在尝试实现泛型类.它应该有一个属性,该属性需要一个编译时常量,我想将其设置为参数类型的名称.像这样的东西:

namespace Example
{
    public class MyGeneric<T>
    {
        [SomeAttribute(CompileTimeConstant)]
        public int MyProperty { get; set; }

        private const string CompileTimeConstant = typeof(T).Name; // error CS0133:
        // The expression being assigned to `Example.MyGeneric<T>.CompileTimeConstant' must be constant
    }
}
Run Code Online (Sandbox Code Playgroud)

但是因为typeof(T).Name在运行时进行评估,所以它不起作用.可能吗?

c# generics reflection compile-time-constant compile-time

8
推荐指数
1
解决办法
667
查看次数