我知道is和asfor instanceof,但是反射isInstance()方法怎么样?
说我有一节课:
public class R {
public static final int _1st = 0x334455;
}
Run Code Online (Sandbox Code Playgroud)
如何通过反射得到字段/属性"_1st"的值?
Java字符串池加上反射可以在Java中产生一些难以想象的结果:
import java.lang.reflect.Field;
class MessingWithString {
public static void main (String[] args) {
String str = "Mario";
toLuigi(str);
System.out.println(str + " " + "Mario");
}
public static void toLuigi(String original) {
try {
Field stringValue = String.class.getDeclaredField("value");
stringValue.setAccessible(true);
stringValue.set(original, "Luigi".toCharArray());
} catch (Exception ex) {
// Ignore exceptions
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码将打印:
"Luigi Luigi"
Run Code Online (Sandbox Code Playgroud)
马里奥怎么了?
我想检查对象o是否是类的实例C或子类的实例C.
举例来说,如果p是类的Point我想x.instanceOf(Point.class)是true也x.instanceOf(Object.class)为true.
我希望它也适用于盒装原始类型.例如,如果x是,Integer那么x.instanceOf(Integer.class)应该是true.
有这样的事吗?如果没有,我该如何实现这样的方法?
StringJava中是否有任何替换机制,我可以使用文本传递对象,并在发生时替换字符串.
例如,文本是:
Hello ${user.name},
Welcome to ${site.name}.
Run Code Online (Sandbox Code Playgroud)
我拥有的对象是"user"和"site".我想${}用对象中的等价值替换内部给出的字符串.这与我们替换速度模板中的对象相同.
我想在C#中这样做,但我不知道如何:
我有一个类名为-eg的字符串:FooClass我想在这个类上调用(静态)方法:
FooClass.MyMethod();
Run Code Online (Sandbox Code Playgroud)
显然,我需要通过反思找到对类的引用,但是如何?
我知道有一些问题可以解决这个问题,但答案通常遵循推荐字典或参数集合的方式,这在我的情况下不起作用.
我正在使用一个通过反射工作的库,用具有属性的对象做很多聪明的事情.这适用于已定义的类以及动态类.我需要更进一步,按照这些方针做一些事情:
public static object GetDynamicObject(Dictionary<string,object> properties) {
var myObject = new object();
foreach (var property in properties) {
//This next line obviously doesn't work...
myObject.AddProperty(property.Key,property.Value);
}
return myObject;
}
public void Main() {
var properties = new Dictionary<string,object>();
properties.Add("Property1",aCustomClassInstance);
properties.Add("Property2","TestString2");
var myObject = GetDynamicObject(properties);
//Then use them like this (or rather the plug in uses them through reflection)
var customClass = myObject.Property1;
var myString = myObject.Property2;
}
Run Code Online (Sandbox Code Playgroud)
该库适用于动态变量类型,手动分配属性.但是我不知道预先添加了多少或哪些属性.
我有MyClass<T>.
然后我有了这个string s = "MyClass<AnotherClass>";.如何从字符串中获取Type s?
一种方法(丑陋)是解析"<"和">"并执行:
Type acType = Type.GetType("AnotherClass");
Type whatIwant = typeof (MyClass<>).MakeGenericType(acType);
Run Code Online (Sandbox Code Playgroud)
但有没有更简洁的方法来获得最终类型,没有任何解析等?
示例控制台程序.
class Program
{
static void Main(string[] args)
{
// ... code to build dll ... not written yet ...
Assembly assembly = Assembly.LoadFile(@"C:\dyn.dll");
// don't know what or how to cast here
// looking for a better way to do next 3 lines
IRunnable r = assembly.CreateInstance("TestRunner");
if (r == null) throw new Exception("broke");
r.Run();
}
}
Run Code Online (Sandbox Code Playgroud)
我想动态构建一个程序集(.dll),然后加载程序集,实例化一个类,并调用该类的Run()方法.我应该尝试将TestRunner类转换为某些东西吗?不确定一个程序集中的类型(动态代码)如何知道我的(静态程序集/ shell应用程序)中的类型.使用几行反射代码只在一个对象上调用Run()会更好吗?该代码应该是什么样的?
更新:威廉埃德蒙森 - 见评论
reflection ×10
c# ×5
java ×5
instanceof ×2
string ×2
.net ×1
dynamic ×1
final ×1
properties ×1
static ×1
string-pool ×1
types ×1
velocity ×1