[英]Set property on a derived class of List<T> instantiated at runtime using reflection
[英]Using reflection to set List<dynamic> to List<T>
说,我在一个类中有这些属性:
public class Example
{
public List<Toyota> listToyota;
public List<Honda> listHonda;
public List<Huyndai> listHuyndai;
...
...
}
如果有新的汽车品牌,将会有更多的物业。 每个品牌都是一张桌子。
通常,我会这样做以从表中获取数据:
Example result = new Example();
switch (brandname)
{
case "Toyota":
result.listToyota = * select table from context * ;
break;
case "Honda":
result.listHonda = * select table from context * ;
break;
...
}
另外,当有新品牌时,我将不得不添加更多代码。 我发现这很烦人/很耗时,因此决定改用动态方法。 我成功地动态获取了表:
tblName = "Toyota";
IEnumerable<dynamic> table = typeof(MyContext).GetProperty(tblName).GetValue(context, null) as IEnumerable<dynamic>;
但是我无法动态设置属性值,在本例中为listToyota
:
query = (from a in table select a).ToList() as List<dynamic>;
SetPropertyValue(result, "listToyota", query);
我收到此错误:
类型“ System.Collections.Generic.List1 [System.Object]”的对象不能转换为类型“ System.Collections.Generic.List1 [Toyota]”。
SetPropertyValue
是使用System.Reflection
的非常简单的函数:
static void SetPropertyValue(object p, string propName, object value)
{
Type t = p.GetType();
System.Reflection.PropertyInfo info = t.GetProperty(propName);
if (info == null)
return;
if (!info.CanWrite)
return;
info.SetValue(p, value, null);
}
任何建议,不胜感激!
请尝试这样的事情
static void SetPropertyValue(object p, string propName, object value)
{
Type t = p.GetType();
System.Reflection.PropertyInfo info = t.GetProperty(propName);
if (info == null)
return;
if (!info.CanWrite)
return;
var elemtype = info.PropertyType.GetElementType();
var castmethod = typeof(Enumerable).GetMethod("Cast", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(elemtype);
var tolist = typeof(Enumerable).GetMethod("ToList", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(elemtype);
var collection = castmethod.Invoke(null, new object[] { value });
var list = tolist.Invoke(null, new object[] { collection });
info.SetValue(p, list, null);
}
我认为主要问题是您的软件设计。 我不会以这种方式使用动态。
我会创建一个品牌级的。 如果要使用动态,请尝试以下操作:
public class DynamicBrand : DynamicObject { private IDictionary<string, object> myDynamicValues; public DynamicBrand() : base() { myDynamicValues = new Dictionary<string, object>(); } public void AddMember(string Name, object Value) { if (!myDynamicValues.ContainsKey(Name.Trim().ToLower())) { myDynamicValues.Add(Name.ToLower().Trim(), Value); } else { throw new Exception("The Member with the Name: " + Name + " already exists!"); } } public override bool TrySetMember(SetMemberBinder Binder, object Value) { if (myDynamicValues.ContainsKey(Binder.Name.ToLower())) { myDynamicValues[Binder.Name.ToLower()] = Value; return true; } else { myDynamicValues.Add(Binder.Name.ToLower(), Value); } return true; } publc override bool TryGetMember(GetMemberBinder Binder, out object Result) { if (myDynamicValues.ContainsKey(Binder.Name.ToLower())) { Result = myDynamicValues[Binder.Name.ToLower()]; return true; } else { Result = null; return false; } }
我会改变你的榜样
public class Example { // string = brand-name; list = list of dynamic brand items public Dictionary<string, List<DynamicBrand>> brands; }
填写数据时,只需在品牌列表中添加一个新的动态品牌,然后只需添加所需的成员即可。
编辑 :动态不是一个很好的解决方案,为您的问题。 我会考虑完全不同的结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.