我想通过使用反射从属性类访问类的属性.可能吗?
例如:
class MyAttribute : Attribute
{
private void AccessTargetClass()
{
// Do some operations
}
}
[MyAttribute]
class TargetClass
{
}
Run Code Online (Sandbox Code Playgroud) 那里有大量的反思例子可以让你得到:
1.班级中的所有属性
2.单个属性,前提是您知道字符串名称
有没有办法(使用反射,TypeDescriptor或其他方法)在运行时获取类中属性的字符串名称,前提是我拥有的是类和属性的实例?
编辑 我知道我可以使用反射轻松获取类中的所有属性,然后获取每个属性的名称.我要求的是一个函数来给我一个属性的名称,前提是我传递了属性的实例.换句话说,如何从class.GetType().GetProperty(myProperty)中找到PropertyInfo []数组返回给我的属性,以便从中获取PropertyInfo.Name?
我有2个班级:Father和Child
public class Father implements Serializable, JSONInterface {
private String a_field;
//setter and getter here
}
public class Child extends Father {
//empty class
}
Run Code Online (Sandbox Code Playgroud)
有了反射,我想a_field在Child课堂上设置:
Class<?> clazz = Class.forName("Child");
Object cc = clazz.newInstance();
Field f1 = cc.getClass().getField("a_field");
f1.set(cc, "reflecting on life");
String str1 = (String) f1.get(cc.getClass());
System.out.println("field: " + str1);
Run Code Online (Sandbox Code Playgroud)
但我有一个例外:
线程"main"中的异常java.lang.NoSuchFieldException:a_field
但如果我尝试:
Child child = new Child();
child.setA_field("123");
Run Code Online (Sandbox Code Playgroud)
有用.
使用setter方法我有同样的问题:
method = cc.getClass().getMethod("setA_field");
method.invoke(cc, new Object[] { "aaaaaaaaaaaaaa" });
Run Code Online (Sandbox Code Playgroud) Rust具有这种Any特性,但它也有"不支付你不使用的"政策.Rust如何实现反射?
我的猜测是Rust使用了懒惰标记.每个类型最初都是未分配的,但是稍后如果将类型的实例传递给期望Any特征的函数,则为该类型分配一个TypeId.
或者Rust TypeId可能会在其实例可能传递给该函数的每种类型上放置一个?我想前者会很贵.
我有一个自定义属性,我想应用于我的基本抽象类,以便我可以跳过在HTML中显示项目时用户不需要查看的元素.似乎覆盖基类的属性不继承属性.
重写基本属性(抽象或虚拟)是否会破坏原始属性上的属性?
从属性类定义
[AttributeUsage(AttributeTargets.Property,
Inherited = true,
AllowMultiple = false)]
public class NoHtmlOutput : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
从抽象类定义
[NoHtmlOutput]
public abstract Guid UniqueID { get; set; }
Run Code Online (Sandbox Code Playgroud)
从混凝土类定义
public override Guid UniqueID{ get{ return MasterId;} set{MasterId = value;}}
Run Code Online (Sandbox Code Playgroud)
从类检查属性
Type t = o.GetType();
foreach (PropertyInfo pi in t.GetProperties())
{
if (pi.GetCustomAttributes(typeof(NoHtmlOutput), true).Length == 1)
continue;
// processing logic goes here
}
Run Code Online (Sandbox Code Playgroud) 假设您有一个文本文件,如:
my_setting = ON
some_method = METHOD_A
verbosity = DEBUG
...
Run Code Online (Sandbox Code Playgroud)
您希望相应地更新相应的对象:
Setting my_setting = ON;
Method some_method = METHOD_A;
Verbosity verbosity = DEBUG;
...
Run Code Online (Sandbox Code Playgroud)
所有都是不同类型的枚举.
我想有一个通用的方法来实例化枚举值.也就是说,在运行时使用反射,并且事先不知道对象的枚举类型.
我会想象这样的事情:
for (ConfigLine line : lines)
{
String[] tokens = line.string.split("=", 2);
String name = tokens[0].trim();
String value = tokens[1].trim();
try
{
Field field = this.getClass().getDeclaredField(name);
if(field.getType().isEnum())
{
// doesn't work (cannot convert String to enum)
field.set(this, value);
// invalid code (some strange generics issue)
field.set(this, Enum.valueOf(field.getType().getClass(), value));
}
else
{ /*...*/ } …Run Code Online (Sandbox Code Playgroud) Assembly.GetEntryAssembly()不适用于Web应用程序.
但是......我真的需要这样的东西.我使用了一些在Web和非Web应用程序中使用的深层嵌套代码.
我目前的解决方案是浏览StackTrace以找到第一个被调用的程序集.
/// <summary>
/// Version of 'GetEntryAssembly' that works with web applications
/// </summary>
/// <returns>The entry assembly, or the first called assembly in a web application</returns>
public static Assembly GetEntyAssembly()
{
// get the entry assembly
var result = Assembly.GetEntryAssembly();
// if none (ex: web application)
if (result == null)
{
// current method
MethodBase methodCurrent = null;
// number of frames to skip
int framestoSkip = 1;
// loop until we cannot got further in …Run Code Online (Sandbox Code Playgroud) 在Scala 2.10中如何从字符串生成一个类(可能使用Toolbox api)以后用Scala的反射进行实例化?
我在String中有一段Java代码.
String javaCode = "if(polishScreenHeight >= 200 && " +
"polishScreenHeight <= 235 && polishScreenWidth >= 220) { }";
Run Code Online (Sandbox Code Playgroud)
是否可以将此Java String转换为Java语句并运行它?可能使用Java反射?
我有两节课.
public class A {
public Object method() {...}
}
public class B extends A {
@Override
public Object method() {...}
}
Run Code Online (Sandbox Code Playgroud)
我有一个B的实例.如何从b调用A.method()?基本上,与从B调用super.method()的效果相同.
B b = new B();
Class<?> superclass = b.getClass().getSuperclass();
Method method = superclass.getMethod("method", ArrayUtils.EMPTY_CLASS_ARRAY);
Object value = method.invoke(obj, ArrayUtils.EMPTY_OBJECT_ARRAY);
Run Code Online (Sandbox Code Playgroud)
但是上面的代码仍然会调用B.method()
reflection ×10
c# ×4
java ×4
attributes ×2
assemblies ×1
enums ×1
overriding ×1
propertyinfo ×1
rust ×1
scala ×1
scala-2.10 ×1
stack-frame ×1
superclass ×1
toolbox ×1