在Java中,有没有办法使用JDK库来发现在另一个类中实现的私有类?或者我需要使用像asm这样的东西吗?
我正在尝试确定对象上字段的类型.当它传递给我时,我不知道对象的类型,但我需要找到longs的字段.很容易区分盒装,Long但原始图像long似乎更难.
我可以确保传递给我的对象只有Longs,而不是原语,但我宁愿不.所以我拥有的是:
for (Field f : o.getClass().getDeclaredFields()) {
Class<?> clazz = f.getType();
if (clazz.equals(Long.class)) {
// found one -- I don't get here for primitive longs
}
}
Run Code Online (Sandbox Code Playgroud)
一种似乎有用的hacky方式是:
for (Field f : o.getClass().getDeclaredFields()) {
Class<?> clazz = f.getType();
if (clazz.equals(Long.class) || clazz.getName().equals("long")) {
// found one
}
}
Run Code Online (Sandbox Code Playgroud)
如果有的话,我真的想要一个更干净的方法.如果没有更好的方法,那么我认为要求我收到的对象只使用Long(不是long)将是一个更好的API.
有任何想法吗?
我正在玩C#反射API.我可以轻松地Type在程序集中加载类,方法等信息,但是,现在我想知道如何加载和读取方法中的代码?
我有个问题.是否可以在.NET中使用反射调用泛型方法?我尝试了以下代码
var service = new ServiceClass();
Type serviceType = service.GetType();
MethodInfo method = serviceType.GetMethod("Method1", new Type[]{});
method.MakeGenericMethod(typeof(SomeClass));
var result = method.Invoke(service, null);
Run Code Online (Sandbox Code Playgroud)
但它抛出以下异常"无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作."
我试图通过反射获取静态私有属性的值,但它失败并出现错误.
Class class = home.Student.class;
Field field = studentClass.getDeclaredField("nstance");
Object obj = field.get(null);
Run Code Online (Sandbox Code Playgroud)
我得到的例外是:
java.lang.IllegalAccessException: Class com.test.ReflectionTest can not access a member of class home.Student with modifiers "private static".
Run Code Online (Sandbox Code Playgroud)
此外,我需要使用以下代码调用私有.
Method method = studentClass.getMethod("addMarks");
method.invoke(studentClass.newInstance(), 1);
Run Code Online (Sandbox Code Playgroud)
但问题是Student类是单例类,而构造函数是私有的,无法访问.
我需要一个方法,该方法接受一个MethodInfo表示具有任意签名的非泛型静态方法的实例,并返回绑定到该方法的委托,该委托稍后可以使用Delegate.DynamicInvoke方法调用.我的第一次天真尝试看起来像这样:
using System;
using System.Reflection;
class Program
{
static void Main()
{
var method = CreateDelegate(typeof (Console).GetMethod("WriteLine", new[] {typeof (string)}));
method.DynamicInvoke("Hello world");
}
static Delegate CreateDelegate(MethodInfo method)
{
if (method == null)
{
throw new ArgumentNullException("method");
}
if (!method.IsStatic)
{
throw new ArgumentNullException("method", "The provided method is not static.");
}
if (method.ContainsGenericParameters)
{
throw new ArgumentException("The provided method contains unassigned generic type parameters.");
}
return method.CreateDelegate(typeof(Delegate)); // This does not work: System.ArgumentException: Type must derive from …Run Code Online (Sandbox Code Playgroud) 我创建了一个Attribute名为的类RelatedPropertyAttribute:
[AttributeUsage(AttributeTargets.Property)]
public class RelatedPropertyAttribute: Attribute
{
public string RelatedProperty { get; private set; }
public RelatedPropertyAttribute(string relatedProperty)
{
RelatedProperty = relatedProperty;
}
}
Run Code Online (Sandbox Code Playgroud)
我用它来表示类中的相关属性.我将如何使用它的示例:
public class MyClass
{
public int EmployeeID { get; set; }
[RelatedProperty("EmployeeID")]
public int EmployeeNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想使用lambda表达式,以便我可以将强类型传递给我的属性的构造函数,而不是"魔术字符串".这样我可以利用编译器类型检查.例如:
public class MyClass
{
public int EmployeeID { get; set; }
[RelatedProperty(x => x.EmployeeID)]
public int EmployeeNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我以为我可以使用以下内容,但编译器不允许这样做:
public RelatedPropertyAttribute<TProperty>(Expression<Func<MyClass, TProperty>> propertyExpression)
{ ... } …Run Code Online (Sandbox Code Playgroud) 所以我试图在Swift中将实际变量名称作为字符串,但是没有找到方法这样做......或者我可能在一个不好的角度看待这个问题和解决方案.
所以这基本上就是我想做的事情:
var appId: String? = nil
//This is true, since appId is actually the name of the var appId
if( appId.getVarName = "appId"){
appId = "CommandoFurball"
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法在苹果文档中找到任何与此相近的内容,但是:
varobj.self or reflect(var).summary
Run Code Online (Sandbox Code Playgroud)
但是,这给出了变量本身内部或变量类型的信息,在这种情况下是String,我想要变量的实际名称.
在C#中,有人可以这样做:
MyClass myInstance = new MyClass();
dynamic mydynamicInstance = myInstance;
Run Code Online (Sandbox Code Playgroud)
然后,调用一个方法,如:
//This method takes a MyClass argument and does something.
Caller.InvokeMethod(myDynamicInstance);
Run Code Online (Sandbox Code Playgroud)
现在,这将导致在运行时确定myInstance类型,如果它有效,Caller.InvokeMethod将正常调用.
现在,我的问题是,如果这被认为是一种不好的做法dynamic,特别是在以下情况下:
1)InvokeMethod使用内部反射实例化myDynamicInstance类型的另一个实例.
2)有一个抽象基类MyBaseClass和它的许多子类,包括MyBaseClass.如果我们InvokeMethod为所有这些派生类提供了许多重载方法,我们是否可以使用它来在运行时允许类型确定,然后通过方法重载(或对该类方法调用的后期绑定)进行适当的调用)?:
public abstract class MyBaseClass {/*...*/}
public class MyClass : MyBaseClass {/*...*/}
public class MyAnotherClass : MyBaseClass {/*...*/}
MyBaseClass myBaseClassRef = new MyClass();
dynamic myDynamicInstance = myBaseClassRef;
Caller.InvokeMethod(myDynamicInstance);
Run Code Online (Sandbox Code Playgroud) 有没有办法在TypeScript中获取类的属性名称:在示例中,我想"描述"类A或任何类并获取其属性的数组(可能只是公共的?),是否可能?或者我应该首先实例化对象?
class A {
private a1;
private a2;
/** Getters and Setters */
}
class Describer<E> {
toBeDescribed:E ;
describe(): Array<string> {
/**
* Do something with 'toBeDescribed'
*/
return ['a1', 'a2']; //<- Example
}
}
let describer = new Describer<A>();
let x= describer.describe();
/** x should be ['a1', 'a2'] */
Run Code Online (Sandbox Code Playgroud) reflection ×10
c# ×4
.net ×3
java ×3
attributes ×1
delegates ×1
dynamic ×1
generics ×1
methodinfo ×1
primitive ×1
swift ×1
typescript ×1