可以反射用于实例化对象的基类的属性? [英] Can reflection be used to instantiate an objects base class properties?
问题描述
这样的:
公共类remoteStatusCounts:RemoteStatus
{
公众诠释statusCount;
公共remoteStatusCounts(RemoteStatus R)
{
型T = r.GetType();
的foreach(的PropertyInfo中的p t.GetProperties())
{
this.property(P)= p.GetValue(); //例如伪
}
}
}
的例子是有点简单(这是从吉拉API - RemoteStatus有4个属性),但想象中的基类有30个属性。我不想手动设置所有这些值,尤其是当我继承类只有一对夫妇的额外属性。
反射似乎在回答提示。
我看到在的http:// stackoverflow.com/questions/3072249/using-inheritance-in-constructor-publix-xy 我可以调用基类的构造函数(我想?纠正我,如果我错了),但我的基类没有按'吨有一个构造 - 它是从JIRA WSDL派生
公共remoteStatusCounts(RemoteStatus R):碱基(r){//做东西}
修改
我可以想象2有效的解决方案:上面概述,以及一些关键字像 this.baseClass
是类型(基类)
和操纵正因为如此,作为一种指针,以这个
。因此, this.baseClass.name =约翰尼
将是完全一样的东西为 this.name =约翰尼
有关所有意图和目的,让我们假设基类有一个拷贝构造函数 - 那就是,这是有效的代码:
公共remoteStatusCounts(RemoteStatus R){
RemoteStatus MBASE = R;
//做的工作
}
EDIT2
这个问题不止一个思想的锻炼practial一员 - 我的目的,我可能已经很容易地做到了这一点:(假设我的基类可以使份)
公共类remoteStatusCounts
{
公众诠释statusCount;
公共RemoteStatus rStatus;
公共remoteStatusCounts(RemoteStatus R)
{
rStatus = R;
statusCount = getStatusCount();
}
}
是的,你可以做到这一点 - 要知道虽然,你可能会遇到,你必须处理单独只吸气的属性。
您可以使用<$筛选它。C $ C> Type.GetProperties(BindingsFlags)超载
请注意:你或许应该看看代码生成(T4将是一个想法,因为它与VS 2008/2010交付),因为反射可以像执行速度运行时的影响。随着codegeneration,你可以轻松地处理这种单调乏味的工作,仍然具有相同的运行时间等喜欢手动键入下来
例如:
//扩展方法某处
公共静态牛逼演员LT; T>(此对象o)
{
回报率(T)O;
}
公共remoteStatusCounts(RemoteStatus R)
{
型TYPER = r.GetType();
型typeThis = this.GetType();
的foreach(在typeR.GetProperties的PropertyInfo P())
{
的PropertyInfo thisProperty = typeThis.GetProperty(p.Name);
MethodInfo的castMethod = typeof运算(ExMethods).GetMethod(投)MakeGenericMethod(p.PropertyType)。
VAR castedObject = castMethod.Invoke(空,新的对象[] {p.GetValue(R,NULL)});
thisProperty.SetValue(这一点,castedObject,NULL);
}
}
Like this:
public class remoteStatusCounts : RemoteStatus
{
public int statusCount;
public remoteStatusCounts(RemoteStatus r)
{
Type t = r.GetType();
foreach (PropertyInfo p in t.GetProperties())
{
this.property(p) = p.GetValue(); //example pseudocode
}
}
}
The example is a bit simple (it's from the Jira API - RemoteStatus has 4 properties), but imagine the base class had 30 properties. I don't want to manually set all those values, especially if my inherited class only has a couple of extra properties.
Reflection seems to hint at an answer.
I saw at http://stackoverflow.com/questions/3072249/using-inheritance-in-constructor-publix-x-y that I can call the base class constructor (I think? correct me if I'm wrong), but my base class doesn't have a constructor - it's derived from the jira wsdl
public remoteStatusCounts(RemoteStatus r) : base(r) { //do stuff }
edit
I can imagine 2 valid solutions: the one outlined above, and some sort of keyword like this.baseClass
that is of type(baseclass)
and manipulated as such, acting as a sort of pointer to this
. So, this.baseClass.name = "Johnny"
would be the exact same thing as this.name = "Johnny"
For all intents and purposes, let's assume the baseclass has a copy constructor - that is, this is valid code:
public remoteStatusCounts(RemoteStatus r) {
RemoteStatus mBase = r;
//do work
}
edit2 This question is more of a thought exercise than a practial one - for my purposes, I could've just as easily done this: (assuming my "baseclass" can make copies)
public class remoteStatusCounts
{
public int statusCount;
public RemoteStatus rStatus;
public remoteStatusCounts(RemoteStatus r)
{
rStatus = r;
statusCount = getStatusCount();
}
}
Yes, you can do this - be aware though, that you might run into getter-only properties which you have to deal with seperately.
You can filter it with the Type.GetProperties(BindingsFlags)
overload.
Note: You probably should look into code generation (T4 would be an idea, as it is delivered with vs 2008/2010), because reflection could have runtime-implications like execution speed. With codegeneration, you can easily handle this tedious work and still have the same runtime etc like typing it down manually.
Example:
//extension method somewhere
public static T Cast<T>(this object o)
{
return (T)o;
}
public remoteStatusCounts(RemoteStatus r)
{
Type typeR = r.GetType();
Type typeThis = this.GetType();
foreach (PropertyInfo p in typeR.GetProperties())
{
PropertyInfo thisProperty = typeThis.GetProperty(p.Name);
MethodInfo castMethod = typeof(ExMethods).GetMethod("Cast").MakeGenericMethod(p.PropertyType);
var castedObject = castMethod.Invoke(null, new object[] { p.GetValue(r, null) });
thisProperty.SetValue(this, castedObject, null);
}
}
这篇关于可以反射用于实例化对象的基类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!