我有一个Java对象'ChildObj',它从'ParentObj'扩展而来.现在,如果可以使用Java反射机制检索ChildObj的所有属性名称和值,包括继承的属性?
Class.getFields为我提供了公共属性数组,Class.getDeclaredFields为我提供了所有字段的数组,但它们都没有包含继承的字段列表.
有没有办法检索继承的属性呢?
我可以通过反思设置私有财产吗?
public abstract class Entity
{
private int _id;
private DateTime? _createdOn;
public virtual T Id
{
get { return _id; }
private set { ChangePropertyAndNotify(ref _id, value, x => Id); }
}
public virtual DateTime? CreatedOn
{
get { return _createdOn; }
private set { ChangePropertyAndNotify(ref _createdOn, value, x => CreatedOn); }
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下它并不起作用,其中t代表一种类型Entity:
var t = typeof(Entity);
var mi = t.GetMethod("set_CreatedOn", BindingFlags.Instance | BindingFlags.NonPublic);
Run Code Online (Sandbox Code Playgroud)
我想我可以做到这一点,但我无法解决这个问题.
IC#我们通过反思来做到这一点.在Javascript中它很简单:
for(var propertyName in objectName)
var currentPropertyValue = objectName[propertyName];
Run Code Online (Sandbox Code Playgroud)
怎么用Python做?
如果我有这样的课程:
public class Whatever
{
public void aMethod(int aParam);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法知道,aMethod使用一个名为参数aParam,即类型的int?
斯威夫特是否支持反思?例如,有什么样valueForKeyPath:和setValue:forKeyPath:对雨燕的对象?
实际上它甚至有一个动态类型系统,类似于obj.classObjective-C?
我有一个抽象类:
abstract class AbstractDataExport
{
public string name;
public abstract bool ExportData();
}
Run Code Online (Sandbox Code Playgroud)
我有从AbstractDataExport派生的类:
class XmlExport : AbstractDataExport
{
new public string name = "XmlExporter";
public override bool ExportData()
{
...
}
}
class CsvExport : AbstractDataExport
{
new public string name = "CsvExporter";
public override bool ExportData()
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?(伪代码:)
foreach (Implementation imp in Reflection.GetInheritedClasses(AbstractDataExport)
{
AbstractDataExport derivedClass = Implementation.CallConstructor();
Console.WriteLine(derivedClass.name)
}
Run Code Online (Sandbox Code Playgroud)
输出像
CsvExporter
XmlExporter
Run Code Online (Sandbox Code Playgroud)
?
这背后的想法是创建一个派生自AbstractDataExport的新类,这样我就可以自动遍历所有实现并将名称添加到Dropdown-List.我只是想编译派生类而不改变项目中的任何其他内容,重新编译,宾果游戏!
如果您有其他解决方案:告诉他们.
谢谢
我有一个匿名类型对象,我从一个方法接收动态,我想检查该对象上存在的属性.
....
var settings = new {
Filename="temp.txt",
Size=10
}
...
function void Settings(dynamic settings) {
var exists = IsSettingExist(settings,"Filename")
}
Run Code Online (Sandbox Code Playgroud)
我该如何实现IsSettingExist?
我想知道,因为可以使用反射完成很多事情,我可以在构造函数完成执行后更改私有只读字段吗?
(注意:只是好奇心)
public class Foo
{
private readonly int bar;
public Foo(int num)
{
bar = num;
}
public int GetBar()
{
return bar;
}
}
Foo foo = new Foo(123);
Console.WriteLine(foo.GetBar()); // display 123
// reflection code here...
Console.WriteLine(foo.GetBar()); // display 456
Run Code Online (Sandbox Code Playgroud) 我正在做一个反思项目,现在我被卡住了.如果我有一个可以容纳List的"myclass"对象,那么如果属性myclass.SomList为空,是否有人知道如何获取下面代码中的类型?
List<myclass> myList = dataGenerator.getMyClasses();
lbxObjects.ItemsSource = myList;
lbxObjects.SelectionChanged += lbxObjects_SelectionChanged;
private void lbxObjects_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Reflect();
}
Private void Reflect()
{
foreach (PropertyInfo pi in lbxObjects.SelectedItem.GetType().GetProperties())
{
switch (pi.PropertyType.Name.ToLower())
{
case "list`1":
{
// This works if the List<T> contains one or more elements.
Type tTemp = GetGenericType(pi.GetValue(lbxObjects.SelectedItem, null));
// but how is it possible to get the Type if the value is null?
// I need to be able to create a new object of the …Run Code Online (Sandbox Code Playgroud) 我通过反射调用一个静态方法Parse,因为我不知道编译时对象的类型(但我知道它有一个Parse方法,带一个字符串).
但是,我得到了一个模糊的匹配异常,大概是因为有很多重载的Parse方法,每个方法都占用一个对象(string,int,double等).
如何在我的方法调用中更具体,以确保我到达正确的方法(Parse(string s))并且不抛出异常.
我的代码看起来像这样:
Type returnType = p.PropertyType;
object value = returnType.GetMethod("Parse").Invoke(null, new string[] { "1" });
Run Code Online (Sandbox Code Playgroud) reflection ×10
c# ×5
java ×2
properties ×2
.net ×1
.net-4.0 ×1
c#-4.0 ×1
dynamic ×1
field ×1
generic-list ×1
inheritance ×1
ios ×1
private ×1
python ×1
readonly ×1
swift ×1