如何获取MemberInfo对象的值?.Name返回变量的名称,但我需要该值.
我认为你可以这样做,FieldInfo但我没有一个片段,如果你知道如何做到这一点你能提供一个片段吗?
谢谢!
谷歌搜索和登陆后,阅读了另一个问题
如果您在编译时不知道参数的数量或类型,是否可以从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) 我正在使用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?
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反射调用这个方法:
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)
这是如何完成的?
我有一个我正在研究的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) 是否可以使用Java反射从另一个类实例化私有内部类.例如,如果我采用此代码
public class Main {
public static void main(String[] args) {}
}
class OtherClass {
private class Test {}
}
Run Code Online (Sandbox Code Playgroud)
是否可以从类main中的main方法实例化并获得对Test的访问权限.
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)
如果它没有这个那么强大,那么典型的用例是什么?
有什么区别Integer.class,Integer.TYPE和int.class?
适合我
Integer.class 是Integer(Wrapper)类对象的引用int.class的int不是一类,这是一个基本类型.那Integer.TYPE指的是什么?@CallerSensitive上述方法的注释隐含了什么?
例如,注释存在于Class的getClassLoader方法中
@CallerSensitive
public ClassLoader getClassLoader() {
//
}
Run Code Online (Sandbox Code Playgroud) reflection ×10
java ×6
.net ×2
c# ×2
class ×2
c++ ×1
c++17 ×1
classloader ×1
delegates ×1
methodinfo ×1
primitive ×1
private ×1
security ×1