标签: reflection

Roslyn从IMethodSymbol获取MethodInfo

如何从IMethodSymbol(Roslyn语法树)可靠地获取MethodInfo(反射).我可以从IMethodSymbol获取Type,并且该类型有许多方法,其中一个方法与IMethodSymbol实例匹配.

例如

int i = 0;
i.Equals(5);
Run Code Online (Sandbox Code Playgroud)

IMethodSymbol识别'等于'的地方

注意,[Int32]类型有2个[Equals]方法,一个采用[Object],另一个采用[Int32]参数.

我正在解析脚本,我没有任何工作区实例.

有任何想法吗?斯特沃

.net c# reflection methodinfo roslyn

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

为什么使用动态方法设置只读字段会导致此类错误?

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)

c# reflection f# il reflection.emit

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

不使用反射访问类'字段?

有没有办法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)

这是可能的还是我对编译器能力的方式?

java reflection

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

是否可以使用Boost Hana反省方法?

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...),我就会遇到很多编译错误.由于没有显示方法内省的例子,我想知道它是否得到支持.

c++ reflection generic-programming c++14 boost-hana

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

如何实现std :: experimental :: source_location?

库基础知识的C++扩展,版本2(N4564)介绍了该类型std::experimental::source_location.

§14.1.2[reflection.src_loc.creation]说:

static constexpr source_location current() noexcept;
Run Code Online (Sandbox Code Playgroud)

返回:当函数调用(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)

c++ reflection fundamentals-ts c++17 std-source-location

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

反思是否存在不连贯性?

该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或其他类似的帮助下.

reflection haskell typeclass

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

验证是否已调用所有getter方法

我有以下测试,我需要验证是否正在调用Person类的所有getter.到目前为止,我已经使用了mockito的verify()来确保调用每个getter.有没有办法通过反思来做到这一点?可能是这样的情况,新的getter被添加到Person类,但测试将错过.

public class GetterTest {
    class Person{

        private String firstname;
        private String lastname;

        public String getFirstname() {
            return firstname;
        }

        public String getLastname() {
            return lastname;
        }
    }

    @Test
    public void testAllGettersCalled() throws IntrospectionException{
        Person personMock = mock(Person.class);
        personMock.getFirstname();
        personMock.getLastname();

        for(PropertyDescriptor property : Introspector.getBeanInfo(Person.class).getPropertyDescriptors()) {
            verify(personMock, atLeast(1)).getFirstname();
            //**How to verify against any getter method and not just getFirstName()???**
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java reflection junit unit-testing mockito

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

从IL创建方法的副本

我试图在运行时使用反射创建方法的副本.

我有以下代码.

public static R CopyMethod<T, R>(Func<T, R> f, T t)
{
    AppDomain currentDom = Thread.GetDomain();
    AssemblyName asm = new AssemblyName();
    asm.Name = "DynamicAssembly";
    AssemblyBuilder abl = currentDom.DefineDynamicAssembly(asm, AssemblyBuilderAccess.Run);
    ModuleBuilder mbl = abl.DefineDynamicModule("Module");
    TypeBuilder tbl = mbl.DefineType("Type");
    var info = f.GetMethodInfo();
    MethodBuilder mtbl = tbl.DefineMethod(info.Name, info.Attributes, info.CallingConvention, info.ReturnType, info.GetParameters().Select(x => x.ParameterType).ToArray());

    byte[] il = f.Method.GetMethodBody().GetILAsByteArray();

    mtbl.CreateMethodBody(il, il.Length);
    Type type = tbl.CreateType();
    Func<T, R> method = type.GetMethod(info.Name).CreateDelegate(typeof(Func<T, R>)) as Func<T, R>;
    return method(t);
}
Run Code Online (Sandbox Code Playgroud)

最后一行抛出一条带有消息的异常:

Common Language Runtime检测到无效程序.

还有另一种方法吗?我希望能够获得方法的解析树而不是直接使用IL.

编辑1: …

c# reflection il reflection.emit .net-assembly

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

Java反射:为集合类型Object调用Setter和Getter方法

我有两个不同的用户定义对象包....

1) ws.lender.dto (all Objects exists in this package are source side).
2) copl.com.dto (all Objects exists in this package are destination side).
Run Code Online (Sandbox Code Playgroud)

对象层次结构和对象名称在两侧都不同.我想通过字段或通过使用Reflection的getter和setter将源端对象复制到目标端对象字段.

例如

源端对象

   package ws.lender.dto;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "CustomerAddresses", propOrder = {
        "previousAddresses"
    })
    public class CustomerAddresses {

        protected PreviousAddresses previousAddresses;

        public PreviousAddresses getPreviousAddresses() {
            return previousAddresses;
        }

        public void setPreviousAddresses(PreviousAddresses value) {
            this.previousAddresses = value;
        }

    }


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PreviousAddresses", propOrder = {
    "previousAddress"
})
public class PreviousAddresses {

    @XmlElement(name = "PreviousAddress", required = true) …
Run Code Online (Sandbox Code Playgroud)

java mapping reflection

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

在不使用反射的情况下检查Go中的接口值是否为零

我需要检查接口值是否为"nil".

但通过使用反射,它给了我一个错误:

反映:在struct Value上调用reflect.Value.Bool.

通过nil它没有给人一种错误的nil价值.

reflection null interface go

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