Using only .net 3.5 i have sample class:
public class Table1
{
public IEnumerable<Table2> Items { get; set; }
}
其某种类型的办公室管理,即需要用一种计算法进行收集。
I trying find way to assign instance to property Items,
for example by List<Table2>
When i create instance by Activator, it returns object, which i cant cast to needed type
var t = typeof(List<>);
var gt = t.MakeGenericType(typeof(Table2));
object instance = Activator.CreateInstance(gt);
var table1 = new Table1();
table1.Items = instance; //canot use cast here
把物体变量划分为可计算数字的千兆克
它如何在大多数OR-地图绘制中发挥作用?
Can i use Reflection.Emit to generate concrete type?
Can i use Castle/Linfu?
EDIT:
I canot use any direct cast, because it requires reference Table2 which is i cannot harcode
内容:
在经过一段时间后,我自己找到了解决办法。 有必要进行思考,例如:
var table1 = new Table1();
var table1Type = typeof(Table1);
var prop = table1Type.GetProperty("Items");
prop.SetValue(table1, instance, null);