标签: reflection

我如何获得MemberInfo的价值?

如何获取MemberInfo对象的值?.Name返回变量的名称,但我需要该值.

我认为你可以这样做,FieldInfo但我没有一个片段,如果你知道如何做到这一点你能提供一个片段吗?

谢谢!

c# reflection

32
推荐指数
3
解决办法
4万
查看次数

从MethodInfo构建委托?

谷歌搜索和登陆后,阅读了另一个问题

如果您在编译时不知道参数的数量或类型,是否可以从MethodInfo构建正确的委托?

更多关于此:如果不使用Reflection.Emit或类型构建器,这可以优雅地完成吗?

这对我来说有点令人失望,因为Delegate.CreateDelegate要求我指定正确的Delegate类型作为第一个参数,否则它会抛出异常或调用不正确的方法.

我正在建造一些忍者齿轮,这会有很大的帮助......谢谢!


这是一个通用的解决方案:

/// <summary>
/// Builds a Delegate instance from the supplied MethodInfo object and a target to invoke against.
/// </summary>
public static Delegate ToDelegate(MethodInfo mi, object target)
{
    if (mi == null) throw new ArgumentNullException("mi");

    Type delegateType;

    var typeArgs = mi.GetParameters()
        .Select(p => p.ParameterType)
        .ToList();

    // builds a delegate type
    if (mi.ReturnType == typeof(void)) {
        delegateType = Expression.GetActionType(typeArgs.ToArray());

    } else {
        typeArgs.Add(mi.ReturnType);
        delegateType = Expression.GetFuncType(typeArgs.ToArray());
    }

    // creates a binded …
Run Code Online (Sandbox Code Playgroud)

.net reflection delegates methodinfo

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

我如何迭代班级成员?

我正在使用Java版本的Google App Engine.

我想创建一个函数,可以接收许多类型的对象作为参数.我想打印出对象的成员变量.每个对象可能不同,并且该功能必须适用于所有对象.我必须使用反射吗?如果是这样,我需要编写什么样的代码?

public class dataOrganization {
  private String name;
  private String contact;
  private PostalAddress address;

  public dataOrganization(){}
}

public int getObject(Object obj){
  // This function prints out the name of every 
  // member of the object, the type and the value
  // In this example, it would print out "name - String - null", 
  // "contact - String - null" and "address - PostalAddress - null"
}
Run Code Online (Sandbox Code Playgroud)

我该如何编写函数getObject?

java reflection

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

为什么在使用反射调用方法时,"对象不是声明类的实例"?

public class Shared {

    public static void main(String arg[]) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        Shared s1 = new Shared();

        Object obj[] = new Object[2];
        obj[0] = "object1";
        obj[1] = "object2";
        s1.testParam(null, obj);

        Class param[] = new Class[2];
        param[0] = String.class;
        param[1] = Object[].class; //// how to define the second parameter as array
        Method testParamMethod = s1.getClass().getDeclaredMethod("testParam", param);
        testParamMethod.invoke("", obj); ///// here getting error
    }

    public void testParam(String query,Object ... params){
        System.out.println("in the testparam method");
    }

}
Run Code Online (Sandbox Code Playgroud)

这是输出:

in …
Run Code Online (Sandbox Code Playgroud)

java reflection illegalargumentexception

32
推荐指数
2
解决办法
7万
查看次数

如何以null为参数反射调用方法?

我试图用Java反射调用这个方法:

public void setFoo(ArrayList<String> foo) { this.foo = foo; }
Run Code Online (Sandbox Code Playgroud)

问题是我想传递null null,因此foo变为null.

但是,在以下方法中,它假定没有参数,并且我得到IllegalArgumentException(wrong number of arguments):

method.invoke(new FooHolder(), null);
// -----------------------------^ - I want null to be passed to the method...
Run Code Online (Sandbox Code Playgroud)

这是如何完成的?

java reflection

32
推荐指数
4
解决办法
2万
查看次数

C#获取调用程序集的父程序集名称

我有一个我正在研究的C#单元测试应用程序.涉及三个程序集 - C#app本身的程序集,应用程序使用的第二个程序集,以及第二个程序集使用的第三个程序集.

所以这些电话是这样的:

First Assembly ------> Second Assembly---------> Third Assembly.
Run Code Online (Sandbox Code Playgroud)

我在第三个程序集中需要做的是获取调用第二个程序集的Fist Assembly的名称.

Assembly.GetExecutingAssembly().ManifestModule.Name
Assembly.GetCallingAssembly().ManifestModule.Name
Run Code Online (Sandbox Code Playgroud)

返回第二个程序集的名称.和

Assembly.GetEntryAssembly().ManifestModule.Name
Run Code Online (Sandbox Code Playgroud)

返回NULL

有没有人知道是否有办法获得第一届大会的集会名称?

按照其他用户的要求,我把代码放在这里.这不是100%的代码,而是遵循这样的代码.

namespace FirstAssembly{
public static xcass A
{
        public static Stream OpenResource(string name)
        {
            return Reader.OpenResource(Assembly.GetCallingAssembly(), ".Resources." + name);
        }
}
}

using FirstAssembly;
namespace SecondAssembly{
public static class B 

{
public static Stream FileNameFromType(string Name)

{
return = A.OpenResource(string name);
}
}
}
Run Code Online (Sandbox Code Playgroud)

和测试项目方法

using SecondAssembly;
namespace ThirdAssembly{
public class TestC
{

 [TestMethod()]
        public void StremSizTest()
        {
            // ARRANGE
            var …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection system.reflection

32
推荐指数
5
解决办法
2万
查看次数

使用java反射实例化私有内部类

是否可以使用Java反射从另一个类实例化私有内部类.例如,如果我采用此代码

public class Main {
    public static void main(String[] args) {}
}

class OtherClass {
    private class Test {}
}
Run Code Online (Sandbox Code Playgroud)

是否可以从类main中的main方法实例化并获得对Test的访问权限.

java reflection private class inner-classes

32
推荐指数
2
解决办法
3万
查看次数

C++ 1z中的编译时反射?

C++标准化委员会中有一个研究小组,在C++ 1z或之后提供编译时反射.我想知道预期工具的目的是什么以及功能有多强大?

例如,是否可以使用这些工具命名函数或类?

struct A {int f() {return 42;}};
struct B {int (std::reflect<A>::member<0>::declname)() {return 43;}};
// equivalent to struct B {int f() {return 43;}};
Run Code Online (Sandbox Code Playgroud)

如果它没有这个那么强大,那么典型的用例是什么?

c++ reflection standard-library c++17

32
推荐指数
1
解决办法
8621
查看次数

Integer.class与int.class

有什么区别Integer.class,Integer.TYPE和int.class?

适合我

  1. Integer.class 是Integer(Wrapper)类对象的引用
  2. 但什么是那么int.class的int不是一类,这是一个基本类型.那Integer.TYPE指的是什么?

java reflection primitive

32
推荐指数
2
解决办法
3万
查看次数

sun.reflect.CallerSensitive注释是什么意思?

@CallerSensitive上述方法的注释隐含了什么?

例如,注释存在于Class的getClassLoader方法中

 @CallerSensitive
    public ClassLoader getClassLoader() {
    //
    }
Run Code Online (Sandbox Code Playgroud)

java security reflection class classloader

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