我在这样的类中得到各个字段的名称:
Field[] f = MyClass.class.getDeclaredFields();
Sring str = f[0].toString();
MyClass cl = new MyClass();
Run Code Online (Sandbox Code Playgroud)
现在我想动态地str从对象访问(公共)字段cl.我怎么做?
我有一个scala对象定义如下:
package com.example
object Foo {
def bar(): String = "Interesting Result"
}
Run Code Online (Sandbox Code Playgroud)
我知道Foo$.MODULE$.bar()如果Foo在构建和运行时类路径中,我可以从Java 调用,但在我的情况下Foo不在构建类路径中,可能在运行时类路径中配置也可能不配置.
从我的Java代码中我想使用反射调用bar()它是否在运行时类路径上可用,否则我将回退到默认实现.
是否有可能做到这一点?
我有一个ASP.NET MVC项目,它有一个名为emails的子文件夹.这包含我的电子邮件模板的HTM文件.在网站的某些位置,我有一个控制器,需要加载其中一个模板并将其作为电子邮件发送.
我正在尝试做的是使用反射来获取当前正在执行的程序集的路径,但它没有像我期望的那样工作.我回来的路是:
var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Run Code Online (Sandbox Code Playgroud)
'C:\ Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET iles\ssl\1da130c4\f8e7810e\assembly\dl3\5f253aca\1a71f123_e83bcc01\Emails\ProductAccountConfirmation.htm'
我发现这很奇怪,因为该站点是通过Visual Studio在IIS中托管的.我原以为这会给我在dev的项目文件夹中的dll位置和生产中的IIS的deploy文件夹.
class PriceClass {
private int value;
public int Value
{
get { return this.value; }
set { this.value = value; }
}
}
struct PriceStruct
{
private int value;
public int Value
{
get { return this.value; }
set { this.value = value; }
}
}
static void Main(string[] args)
{
PriceClass _priceClass = new PriceClass();
Type type = typeof(PriceClass);
PropertyInfo info = type.GetProperty("Value");
info.SetValue(_priceClass, 32, null);
Console.WriteLine(_priceClass.Value);
PriceStruct _priceStruct = new PriceStruct();
type = typeof(PriceStruct);
info = type.GetProperty("Value");
info.SetValue(_priceStruct, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用此帖子中找到的类,但它需要一个MethodBase才能运行.
我读了什么是获取MethodBase对象的最快方法?但我无法得到任何解决方案.
我需要做的是从函数中获取MethodBase对象.
例如,为Console类的静态函数WriteLine()获取MethodBase,或者为List <>的非静态函数Add()获取MethodBase.
谢谢你的帮助!
我该如何构建表达式树string.IndexOf("substring", StringComparison.OrdinalIgnoreCase)?
没有第二个论点,我可以让它工作:StringComparison.OrdinalIgnoreCase.这些是我到目前为止的尝试:
var methodCall = typeof (string).GetMethod("IndexOf", new[] {typeof (string)});
Expression[] parms = new Expression[]{right, Expression.Constant("StringComparison.OrdinalIgnoreCase", typeof (Enum))};
var exp = Expression.Call(left, methodCall, parms);
return exp;
Run Code Online (Sandbox Code Playgroud)
还试过这个:
var methodCall = typeof (string).GetMethod(method, new[] {typeof (string)});
Expression[] parms = new Expression[]{right, Expression.Parameter(typeof(Enum) , "StringComparison.OrdinalIgnoreCase")};
var exp = Expression.Call(left, methodCall, parms);
return exp;
Run Code Online (Sandbox Code Playgroud)
请记住,如果我忽略OrdinalIgnoreCase参数,我可以使它工作.
谢谢
在"Thinking in Java"一书中,有一个如何通过Reflection/Introspection获取bean信息的例子.
BeanInfo bi = Introspector.getBeanInfo(Car.class, Object.class);
for (PropertyDescriptor d: bi.getPropertyDescriptors()) {
Class<?> p = d.getPropertyType();
if (p == null) continue;
[...]
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例的第4行中,检查PropertyType是否为null.何时以及在何种情况下会发生这种情况?你能给我举个例子吗?
我想知道是否有可能在Java中使用以下内容:
public class MyClass {
private String name;
private Integer age;
private Date dateOfBirth;
// constructors, getters, setters
public void setField(String aFieldName, Object aValue) {
Field aField = getClass().getDeclaredField(aFieldName);
// use: aField.set(...) with proper type handling
}
}
Run Code Online (Sandbox Code Playgroud)
我真的陷入了setField方法,任何想法都会非常有用.
谢谢!
编辑:这是因为我希望在另一个类中有一个方法,如下所示
public static MyClass setAll(List<String> fieldNames, List<Object> fieldValues) {
MyClass anObject = new MyClass();
// iterate fieldNames and fieldValues and set for each fieldName
// the corresponding field value
return anObject;
}
Run Code Online (Sandbox Code Playgroud) 我需要在运行IEnumerable<IEnumerable<T>>时才知道T.
我已经建立了我的收藏品:
new List<List<object>>()
Run Code Online (Sandbox Code Playgroud)
内部列表中的所有对象都是a T
但是,由于CO /逆变的(永远记住它是!)我List的List小号心不是一个IEnumerable的IEnumerable秒.
我该怎么办?
我已经尝试使用Convert.ChangeType,但它的呻吟声是List不是IConvertible
线索:阅读问题.再次.我说我只T在运行时知道.
出于我的应用程序的目的,我需要能够在运行时找到类型(不是实例)的字段列表和这些字段的类型.到目前为止,我只能得到一个包含getter的case类的方法列表,classOf[MyCaseClass].getMethods并且从简单的类中没有任何用处.我错过了什么吗?有没有针对这种目的的反射库?怎么做得好?
reflection ×10
java ×4
c# ×3
scala ×2
.net ×1
asp.net ×1
asp.net-mvc ×1
c#-4.0 ×1
generics ×1
javabeans ×1
methodbase ×1
path ×1
properties ×1
struct ×1