它似乎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()?
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) 两者有什么区别.这两者都以完全相同的方式工作.
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) 如何从IMethodSymbol(Roslyn语法树)可靠地获取MethodInfo(反射).我可以从IMethodSymbol获取Type,并且该类型有许多方法,其中一个方法与IMethodSymbol实例匹配.
例如
int i = 0;
i.Equals(5);
Run Code Online (Sandbox Code Playgroud)
IMethodSymbol识别'等于'的地方
注意,[Int32]类型有2个[Equals]方法,一个采用[Object],另一个采用[Int32]参数.
我正在解析脚本,我没有任何工作区实例.
有任何想法吗?斯特沃
strfld如果将其所有者设置为该类并且关闭JIT检查,则可以使用动态方法中的操作码存储在类的只读字段中.一个例子是在这里.然而,这种方法不适用于来自F#的类,即FSharpOption.请分析以下示例:
using Microsoft.FSharp.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
#if true
using MyType = Microsoft.FSharp.Core.FSharpOption<string>;
#else
using MyType = System.Tuple<string>;
#endif
namespace ConsoleApplication27
{
class Program
{
static void Main(string[] args)
{
var something = new MyType("test");
var dynMethod = new DynamicMethod("ChangeField", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new [] { typeof(MyType) }, typeof(MyType), true);
var generator = dynMethod.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldstr, "success");
generator.Emit(OpCodes.Stfld, typeof(MyType).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)[0]);
generator.Emit(OpCodes.Ret);
var …Run Code Online (Sandbox Code Playgroud) 有没有办法Field在不使用反射的情况下访问类的特定内容?
考虑这个课程:
class MyType {
public int theNumber;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够访问theNumber的地方 java.lang.reflect.Field.
这肯定有效:
Field f = MyType.class.getDeclaredField("theNumber");
Run Code Online (Sandbox Code Playgroud)
但是,我想编译检查字段名称,所以理想情况下这样的东西(但当然我的例子不编译):
Field f = MyType.class::theNumber;
Run Code Online (Sandbox Code Playgroud)
这是可能的还是我对编译器能力的方式?
Boost Hana提供了以简单而美观的方式对类成员字段进行内省的能力:
// define:
struct Person {
std::string name;
int age;
};
// below could be done inline, but I prefer not polluting the
// declaration of the struct
BOOST_HANA_ADAPT_STRUCT(not_my_namespace::Person, name, age);
// then:
Person john{"John", 30};
hana::for_each(john, [](auto pair) {
std::cout << hana::to<char const*>(hana::first(pair)) << ": "
<< hana::second(pair) << std::endl;
});
Run Code Online (Sandbox Code Playgroud)
但是,文档仅提及成员字段.我也想对方法进行反思.我试图用一种方法天真地扩展示例:
struct Foo {
std::string get_name() const { return "louis"; }
};
BOOST_HANA_ADAPT_STRUCT(::Foo, get_name);
Run Code Online (Sandbox Code Playgroud)
这编译.但是,只要我尝试使用它,使用类似于上面的代码(for_each...),我就会遇到很多编译错误.由于没有显示方法内省的例子,我想知道它是否得到支持.
我正在使用一个期望的API Expression<Func<T, object>>,并使用它来创建不同对象之间的映射:
Map(x => x.Id).To("Id__c"); // The expression is "x => x.Id"
Run Code Online (Sandbox Code Playgroud)
如何从PropertyInfo?创建必要的表达式?这个想法是:
var properties = typeof(T).GetProperties();
foreach (var propInfo in properties)
{
var exp = // How to create expression "x => x.Id" ???
Map(exp).To(name);
}
Run Code Online (Sandbox Code Playgroud) 库基础知识的C++扩展,版本2(N4564)介绍了该类型std::experimental::source_location.
§14.1.2[reflection.src_loc.creation]说:
Run Code Online (Sandbox Code Playgroud)static constexpr source_location current() noexcept;返回:当函数调用(C++14§5.2.2)调用其后缀表达式(可能是带括号的)id-expression命名时
current,返回source_location带有实现定义值的a.该值应受#line(C++14§16.4)影响,其方式与__LINE__和__FILE__.如果以其他方式调用,则返回的值未指定.备注:当使用大括号或等于初始化程序来初始化非静态数据成员时,任何调用都
current应该对应于构造函数的位置或初始化成员的聚合初始化.[ 注意:当用作默认参数(C++14§8.3.6)时,该值
source_location将是current呼叫站点呼叫的位置.- 结束说明 ]
如果我理解正确,那么该功能就像这样使用.
#include <experimental/source_location> // I don't actually have this header
#include <iostream>
#include <string>
#include <utility>
struct my_exception
{
std::string message {};
std::experimental::source_location location {};
my_exception(std::string msg,
std::experimental::source_location loc = std::experimental::source_location::current()) :
message {std::move(msg)},
location {std::move(loc)}
{ …Run Code Online (Sandbox Code Playgroud) 该reflection软件包提供了一个类
class Reifies s a | s -> a where
reflect :: proxy s -> a
Run Code Online (Sandbox Code Playgroud)
和一个功能
reify :: a -> (forall s . Reifies s a => Proxy s -> r) -> r
Run Code Online (Sandbox Code Playgroud)
只有这些,例如,通过给出实例,可能会使事情变得非常糟糕
instance Reifies s Int where
reflect _ = 0
Run Code Online (Sandbox Code Playgroud)
这可能很糟糕,例如,
reify (1 :: Int) $ \p -> reflect p
Run Code Online (Sandbox Code Playgroud)
可以合法地生成1(通过通常的反射过程)或0(通过在应用之前专门化传递的函数reify).
实际上,这个特定的漏洞似乎被包含在中的一些Reifies实例所阻止Data.Reflection.我描述的邪恶实例将被重复拒绝.如果启用了重叠实例,我相信重复带来的不确定性可能会阻碍专业化.
不过,我想知道是否有一些方法可以在一个阴暗的实例中暴露这个,也许是在GADT或其他类似的帮助下.