大多数成熟的C++项目似乎都有自己的反射和属性系统,即用于定义可以通过字符串访问的属性并且可以自动序列化.我参与的至少很多C++项目似乎重新发明了轮子.
你知道任何支持反射和属性容器的C++ 开源库,特别是:
作为我的应用程序的一部分,我有一个接收MethodInfo的函数,需要对它进行特定的操作,这取决于该方法是否为"扩展方法".
我检查了MethodInfo类,我找不到任何显示该方法是扩展名的IsExtension属性或标志.
有谁知道如何从方法的MethodInfo中找到它?
我被告知使用Reflection.Emit而不是PropertyInfo.GetValue/SetValue,因为这种方式更快.但我真的不知道Reflection.Emit是什么东西,以及如何用它来代替GetValue和SetValue.任何人都可以帮我吗?
鉴于:
public interface IA
{
void TestMethod();
}
public interface IB : IA
{
}
Run Code Online (Sandbox Code Playgroud)
为什么:
typeof(IB).GetMethods().Count() == 0;
Run Code Online (Sandbox Code Playgroud)
?
只是要清楚:
public class A
{
public void TestMethod()
{
}
}
public class B : A
{
}
typeof(B).GetMethods().Count();
Run Code Online (Sandbox Code Playgroud)
确实有效(它返回5);
作为奖励:
typeof(IB).BaseType == null
Run Code Online (Sandbox Code Playgroud) 我想得到调用方法java.lang.reflect.Method.不是方法的名称.
以下是如何获取调用者类的示例.
// find the callers class
Thread t = Thread.getCurrentThread();
Class<?> klass = Class.forName(t.getStackTrace()[2].getClassName());
// do something with the class (like processing its annotations)
...
Run Code Online (Sandbox Code Playgroud)
它仅用于测试目的!
我正在尝试使用反射创建匿名类的实例.但偶尔我会在瞬间看到奇怪的行为.
请查看这些类似的代码片段
public class HideAndSeek {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IllegalAccessException, InstantiationException{
final String finalString = "I'm final :)";
Object object2 = new Object(){
{
System.out.println("Instance initializing block");
System.out.println(finalString);
}
private void hiddenMethod() {
System.out.println("Use reflection to find me :)");
}
};
Object tmp = object2.getClass().newInstance();
}
}
Run Code Online (Sandbox Code Playgroud)
此代码运行良好,并且预期输出
Instance initializing block
I'm final :)
Instance initializing block
I'm final :)
Run Code Online (Sandbox Code Playgroud)
在此之后,我决定以简单的方式更改代码(只是添加了java.util.Calendar)
import java.util.Calendar;
public class HideAndSeek {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IllegalAccessException, InstantiationException{ …Run Code Online (Sandbox Code Playgroud) 当我运行下面的代码时,为什么会抛出此错误?
java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.Test.main(Test.java:10)
Run Code Online (Sandbox Code Playgroud)
这是我的主要课程:
package com;
public class TestMain {
private String strName = "abcdefg...";
@SuppressWarnings("unused")
private void display(){
System.out.println(strName);
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试班:
package com;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
Class<TestMain> tm = null;
try{
tm= TestMain.class;
Method m1 =tm.getDeclaredMethod("display");
m1.setAccessible(true);
m1.invoke(tm);
}catch(Exception e){
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我修改过的代码,谢谢:
package …Run Code Online (Sandbox Code Playgroud) 我有一个带有静态变量的Java类
package com.mytest
public class MyClass{
public static final TextClass TEXT_CLASS = new TextClass();
}
Run Code Online (Sandbox Code Playgroud)
如何TEXT_CLASS使用反射访问对象?
(我有字符串"com.mytest.MyClass.TEXT_CLASS".我需要访问该对象.)
我有一个类型参数为T的泛型方法,其中T是EF模型中的实体类型.我需要获得此类型的识别字段的名称.我看到这篇文章:有没有办法通过反射或其他方式获取实体id-field的名称? 但我无法理解Tevin谈论EntitySetBase和EntityTypeBase类型时所说的内容.如果EntityTypeBase是模型中某个实体的类型,那么EF6没有属性KeyMembers.
我有一个对象列表(汽车).对于列表中的每辆车,我需要遍历它并找到DateTime类型的任何属性.如果我找到DateTime的属性,我需要获取值并进行时间转换.现在,我们只需将DateTime属性值打印到控制台即可.我有一些问题需要理解我需要放在prop.GetValue函数的第一个参数中.任何帮助,将不胜感激!
foreach (var car in carList)
{
foreach (PropertyInfo car in car.GetType().GetProperties())
{
var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
if (type == typeof (DateTime))
{
Console.WriteLine(prop.GetValue(???, null).ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud) reflection ×10
c# ×5
java ×4
.net ×1
attributes ×1
c++ ×1
calendar ×1
inheritance ×1
interface ×1
methodinfo ×1
static ×1