I have some classes and need to call a function on all "String" type (recursively). For example:
public class A : ILo
{
public string Prop1 { get; set; }
public Guid Id { get; set; }
public B BInfo { get; set; }
public List<C> ListOfC { get; set; }
}
public class B : ILo
{
public string Prop2 { get; set; }
public Guid Id { get; set; }
public D DInfo { get; set; }
public List<E> ListOfE { get; set; }
}
public class C : ILo
{
public string Prop3 { get; set; }
public Guid Id { get; set; }
}
public class D : ILo
{
public string Prop4 { get; set; }
public Guid Id { get; set; }
}
public class E : ILo
{
public string Prop5 { get; set; }
public Guid Id { get; set; }
}
--------------
public T DoSomthing<T>(T need) where T: class
{
if (need == null) return default(T);
var t = need.GetType();
var props = t.GetProperties();
foreach (var propInfo in props)
{
if (propInfo.PropertyType == typeof(string))
{
ParameterExpression pe = Expression.Parameter(t);
Expression propExp = Expression.Property(pe, propInfo);
Expression<Func<T, string>> eFunc = Expression.Lambda<Func<T, string>>(propExp, pe);
var locVal = MyCustomMethod1(need, eFunc, ((ILo)need).GetId());
propInfo.SetValue(need, locVal);
}
else if (propInfo.PropertyType.IsClass)
{
var temp = Convert.ChangeType(propInfo.GetValue(need), propInfo.PropertyType);
DoSomthing(temp);
}
else if (propInfo.PropertyType.IsArray)
{
// do DoSomthing for all items
}
}
return need;
}
I have a method that named 'DoSomething' and when I call it, I gets Error. My error is: "ParameterExpression of type 'B' cannot be used for delegate parameter of type 'System.Object'". Someone please help me.
Aucun commentaire:
Enregistrer un commentaire