how to get child members value from dynamically casted class?
Posted
by
Baka-Maru Lama
on Stack Overflow
See other posts from Stack Overflow
or by Baka-Maru Lama
Published on 2012-04-04T10:28:35Z
Indexed on
2012/04/05
5:29 UTC
Read the original article
Hit count: 240
Well I'm tring to get class members values from a dynamically casted class but I'm unable to find its child class members values.
Right now I'm getting TotalWeight members property, but I also want to get child member property of AnotherClass like AnotherClass.child. How can I get those members?
string ClassName="something";
Type types = Type.GetType(ClassName, false);
var d = from source in types.GetMembers().ToList()
where source.MemberType == MemberTypes.Property
select source;
List<MemberInfo> members = d.Where(memberInfo =>
d.Select(c => c.Name)
.ToList()
.Contains(memberInfo.Name))
.ToList();
PropertyInfo propertyInfo;
object value;
foreach (var memberInfo in members)
{
propertyInfo = typeof(T).GetProperty(memberInfo.Name);
if (myobj.GetType().GetProperty(memberInfo.Name) != null)
{
value = myobj.GetType()
.GetProperty(memberInfo.Name)
.GetValue(myobj, null);
//how to get child members value here?
}
}
//Where class something has member
public class something
{
private decimal _totalWeight;
private Anotherclass _another;
public decimal TotalWeight
{
get
{
return this._totalWeight;
}
set
{
this._totalWeight = value;
}
}
public Anotherclass Another
{
get
{
return this._another;
}
set
{
this._another= value;
}
}
}
© Stack Overflow or respective owner