所以我有一类这样的重载方法:
class Foo {
public void test(Object value) {
...
}
public void test(String value) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我需要根据其类型将bean的属性值传递给其中一个方法,但在运行时之前我不知道实际的属性类型.例如
public void run(Object bean, String propertyName) {
Foo foo = new Foo();
foo.test(PropertyUtils.getProperty(bean, propertyName));
}
Run Code Online (Sandbox Code Playgroud)
BTW,PropertyUtils.getProperty()是一个辅助方法,它返回bean上指定属性的值.PropertyUtils.getProperty()返回一个Object,因此 test(Object value)将始终调用它,并忽略实际的属性类型.
我可以在运行时中找出propery类型,即使它的值为null.在Java中有动态转换这样的东西吗?如果没有,是否有一种方法可以使用正确的参数类型调用重载方法?
我想知道是否有人可以演示如何使用Type的GetMethod()方法来检索以下签名的MethodInfo对象:
Class.StaticMethod<T>(T arg1, IInterface1 arg2, IEnumerable<IInterface2> arg3)
Run Code Online (Sandbox Code Playgroud)
谢谢,
XAM
我写了C#,而来自高处的口头禅似乎"永远不会在生产代码中使用反射".我已将它用于测试代码,但从未在野外运行过.所有的论点看起来都是合理的,而且总有一种方法可以通过添加另一层抽象或设计模式或其他来实现.
现在我开始编写一些严肃的Python代码,我想知道是否适用相同的原则.似乎python的设计考虑了反思.模块和类将成员存储在易于访问的字典中.Django的模型的Meta类,例如将字符串作为引用成员.
我可以在Python中编写C#/ Java,但我真的不想这样做.我仍然坚信所说的语言"没有反思".Python的方式是根本不同的吗?
这与Java类似但不完全相同:使用反射实例化枚举
我有一个Map<Enum<?>, FooHandler>我想用来映射Enums(我不关心哪种类型,或者即使它们是同一类型,只要它们是枚举常量)到我的FooHandler班级.
我想使用我阅读的文本文件填充此地图.我可以让它工作,但我有两个警告我想绕过:
static private <E extends Enum<E>> E getEnum(String enumFullName) {
// see https://stackoverflow.com/questions/4545937/
String[] x = enumFullName.split("\\.(?=[^\\.]+$)");
if (x.length == 2)
{
String enumClassName = x[0];
String enumName = x[1];
try {
Class<E> cl = (Class<E>)Class.forName(enumClassName);
// #1
return Enum.valueOf(cl, enumName);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
public void someMethod(String enumName, String fooHandlerName)
{
FooHandler fooHandler = getFooHandler(fooHandlerName);
Enum e = getEnum(enumName);
// …Run Code Online (Sandbox Code Playgroud) 我正在进行运行时程序集加载,但我不知道任何类或方法的名称.我不想用我们声明的方法列出我的程序集中的所有类,而不是从System.Object继承的那些类.
这是代码:
string str = "";
Assembly assembly = Assembly.LoadFile(@"c:\components.dll");
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass == true)
{
str += type.Name + "\n";
MethodInfo[] methodInfo = type.GetMethods(BindingFlags.DeclaredOnly);
foreach (MethodInfo mi in methodInfo)
{
str += "\t" + mi.Name + "\n";
}
}
}
MessageBox.Show(str);
Run Code Online (Sandbox Code Playgroud)
这是components.dll:
public class component01
{
public string myName = "component01";
public string getMyName()
{
return myName;
}
}
public class component02
{
public string myName = "component02";
public string getMyName()
{
return myName; …Run Code Online (Sandbox Code Playgroud) 我不确定我是否有可能看到:
在运行时更改属性的参数.
我的情况非常相似,但我试图在运行时更改类的属性:
[Category("Change me")]
public class Classic
{
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
其中一个答案是:
Dim prop As PropertyDescriptor = TypeDescriptor
.GetProperties(GetType(UserInfo))("Age")
Dim att As CategoryAttribute = DirectCast(
prop.Attributes(GetType(CategoryAttribute)),
CategoryAttribute)
Dim cat As FieldInfo = att.GetType.GetField(
"categoryValue",
BindingFlags.NonPublic Or BindingFlags.Instance)
cat.SetValue(att, "A better description")
Run Code Online (Sandbox Code Playgroud)
更改为更易阅读的格式,感谢Marc Gravell:
TypeDescriptor.AddAttributes(table, new Category{ Name = "Changed" });
Run Code Online (Sandbox Code Playgroud)
使用TypeDescriptor但使用时,一切都很好:
var attrs = (Category[])typeof(Classic).GetCustomAttributes(
typeof(Category),
true);
attrs[0].Name
Run Code Online (Sandbox Code Playgroud)
名称包含"更改我"文本.
有没有办法在运行时更改此属性?
编辑:
我在设计器中为Linq2Sql需要这个,生成的代码具有DB模式.我想使用用户的默认架构而不使用XML映射或更改生成的代码(表仍处于开发阶段并经常更改).
设计师代码是:
[global::System.Data.Linq.Mapping.TableAttribute(Name="DbSchema.MyTable")]
public partial class MyTable
Run Code Online (Sandbox Code Playgroud)
我希望该属性为:
[TableAttribute(Name="MyTable")]
Run Code Online (Sandbox Code Playgroud)
现在我已经挖掘了框架代码,我认为linq2sql使用:
TableAttribute[] attrs = …Run Code Online (Sandbox Code Playgroud) 我有一个递归函数,BaseClass它依赖于protected virtual函数的返回条件.
子类可能会错误地覆盖此函数并导致StackOverFlow异常.最糟糕的是网络呼叫速度很慢,而且不会很快发生异常(许多资源浪费很长时间).
我正在寻找StackOverFlow一种在基类中以某种方式检查早期阶段的方法(可能使用Reflection和当前的递归级别).
任何的想法 ?
我想使用反射将项添加到通用列表.在方法"DoSomething"中,我试图完成以下行,
pi.PropertyType.GetMethod("Add").Invoke(??????)
Run Code Online (Sandbox Code Playgroud)
但我得到了不同的错误.
以下是我的完整代码
public class MyBaseClass
{
public int VechicleId { get; set; }
}
public class Car:MyBaseClass
{
public string Make { get; set; }
}
public class Bike : MyBaseClass
{
public int CC { get; set; }
}
public class Main
{
public string AgencyName { get; set; }
public MyBaseCollection<Car> lstCar {get;set;}
public void DoSomething()
{
PropertyInfo[] p =this.GetType().GetProperties();
foreach (PropertyInfo pi in p)
{
if (pi.PropertyType.Name.Contains("MyBaseCollection"))
{
//Cln contains List<Car>
IEnumerable<MyBaseClass> cln = pi.GetValue(this, …Run Code Online (Sandbox Code Playgroud) 我正在使用Activator基于程序集的短名称来实例化一个新类(例如'CustomModule').它抛出一个FileNotFoundException,因为装配不在那里.有没有办法检查是否存在某个程序集名称?
我正在使用以下代码:
System.Runtime.Remoting.ObjectHandle obj =
System.Activator.CreateInstance(assemblyName, className);
Run Code Online (Sandbox Code Playgroud)
主要目标是测试组件的存在而不是等待异常发生.
我在这样的类中得到各个字段的名称:
Field[] f = MyClass.class.getDeclaredFields();
Sring str = f[0].toString();
MyClass cl = new MyClass();
Run Code Online (Sandbox Code Playgroud)
现在我想动态地str从对象访问(公共)字段cl.我怎么做?
reflection ×10
c# ×7
java ×4
assemblies ×2
generics ×2
.net ×1
.net-3.5 ×1
activator ×1
attributes ×1
c#-2.0 ×1
enums ×1
linq-to-sql ×1
load ×1
python ×1