考虑这个方法签名:
public static void WriteLine(string input, params object[] myObjects)
{
// Do stuff.
}
Run Code Online (Sandbox Code Playgroud)
如何确定WriteLine方法的"myObjects"参数使用params关键字并且可以采用变量参数?
我有一个String数组,其中包含yyyyyy类中方法的名称
在xxxxxx课堂上,我正在制作一个yyyyyy实例(比如说obj).现在我可以调用obj.function_name(),除了我想function_name从循环中的String数组中读取.这可能吗?
我有以下代码:
[DataContract]
class TestContract {
private String _Name;
private Int32 _Age;
[DataMember( Name = "Name" )]
public String Name {
get { return _Name; }
set { _Name = value; }
}
[DataMember( Name = "Age" )]
public Int32 Age {
get { return _Age; }
set { _Age = value; }
}
}
[Serializable]
public class DNCJsonDictionary<K, V> : ISerializable {
Dictionary<K, V> dict = new Dictionary<K, V>();
public DNCJsonDictionary() { }
protected DNCJsonDictionary( SerializationInfo info, StreamingContext context ) …Run Code Online (Sandbox Code Playgroud) 是否有任何标准方法来访问Java Bean Property
class A {
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我可以使用Reflection API访问这个java bean属性名,这样当我更改property的值时,当我设置并获取该属性的值时,会自动调用getName和setName的方法.
我没有成功地试图找出做Class.cast()什么或做什么有什么好处,同时我想知道我是否能以某种方式通过反射投射对象.
首先,我认为下面的行可能是错误的:
Object o = "A string";
String str = Class.forName("java.lang.String").cast(object);
Run Code Online (Sandbox Code Playgroud)
但如果没有明确的演员,它就行不通.
那么什么是上课cast方法Class呢?它是否有可能只是通过反射来投射对象,所以你找到对象的类,使用Class.forName它并以某种方式投射它?
评估typeof(Object[,][]).Name给出Object[][,]
同样,typeof(Object[][,]).Name给出Object[,][]
好像逗号正在无缘无故地移动.
是什么赋予了?
目前我有一个执行运行程序的任务,比如
task runApp(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'com.eonmux.somepackage'
jvmArgs '-Xmx4096m'
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试将 --add-opens 添加到 JVM 参数列表中,以便允许从我的 gradle 包进行反射访问
就像是
task runApp(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'com.eonmux.somepackage'
jvmArgs '-Xmx4096m','--add-opens java.base/jdk.internal.loader=ALL-UNNAMED'
}
Run Code Online (Sandbox Code Playgroud)
但是,这会出现以下错误,指出命令行选项无法识别
2020-12-09T16:30:56.285-0500 [INFO] [org.gradle.process.internal.DefaultExecHandle] Successfully started process 'command 'C:\Program Files\AdoptOpenJDK\jdk-11.0.6.10-openj9\bin\java.exe''
2020-12-09T16:30:56.286-0500 [DEBUG] [org.gradle.process.internal.ExecHandleRunner] waiting until streams are handled...
2020-12-09T16:30:56.360-0500 [ERROR] [system.err] JVMJ9VM007E Command-line option unrecognised: --add-opens java.base/jdk.internal.loader=ALL-UNNAMED
2020-12-09T16:30:56.361-0500 [ERROR] [system.err] Error: Could not create the Java Virtual Machine.
Could not create the Java Virtual …Run Code Online (Sandbox Code Playgroud) 我想创建一个静态类,它将从XML文件加载一些设置并将这些设置应用于它自己的属性.
我试图使用以下代码,但我真的不知道如何给SetValue方法,因为我们要设置属性的类是静态的.
// some code removed ...
Type settingsType = typeof(Settings); // Settings is a static class
foreach (PropertyInfo propertyInformation in settingsType.GetProperties(BindingFlags.Public |
BindingFlags.Static))
{
//------------------------------------------------------------
// Determine if configured setting matches current setting based on name
//------------------------------------------------------------
if (propertyInformation.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
{
//------------------------------------------------------------
// Attempt to apply configured setting
//------------------------------------------------------------
try
{
if (propertyInformation.CanWrite)
{
propertyInformation.SetValue(this, Convert.ChangeType(value, propertyInformation.PropertyType, CultureInfo.CurrentCulture), null);
}
}
catch
{
}
break;
}
Run Code Online (Sandbox Code Playgroud)
}
甚至可以使用反射在静态类上设置属性吗?
如果在某处覆盖,我道歉.我在发帖前做过研究!
好吧,所以问题...我正在使用GetType().GetProperties,但它没有返回简单的实例字段,它们没有get/set ...所以我使用了.GetFields,找到它们,但我希望得到一个简单的单个对象来获取/设置一个值而不在字段和属性之间翻转......这可能吗?
我目前的代码适用于PropertyInfo,它工作得很好,但那不是我认为的字段吗?
[编辑] 这是我提出的解决方案,这是很好的.感谢大家....
// some logic borrowed from James Newton-King, http://www.newtonsoft.com
public static void SetValue(this MemberInfo member, object property, object value)
{
if (member.MemberType == MemberTypes.Property)
((PropertyInfo)member).SetValue(property, value, null);
else if (member.MemberType == MemberTypes.Field)
((FieldInfo)member).SetValue(property, value);
else
throw new Exception("Property must be of type FieldInfo or PropertyInfo");
}
public static object GetValue(this MemberInfo member, object property)
{
if (member.MemberType == MemberTypes.Property)
return ((PropertyInfo)member).GetValue(property, null);
else if (member.MemberType == MemberTypes.Field)
return ((FieldInfo)member).GetValue(property);
else
throw new Exception("Property must be …Run Code Online (Sandbox Code Playgroud) 使用reified类型参数,可以编写一个内联函数,该函数在运行时通过反射与type参数一起使用:
inline fun <reified T: Any> f() {
val clazz = T::class
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是当f使用一个本身就是泛型类的参数调用时,似乎无法通过以下方式获取其实际类型参数T::class:
f<List<Integer>>() // T::class is just kotlin.collections.List
Run Code Online (Sandbox Code Playgroud)
有没有办法通过反射获得具体通用的实际类型参数?