c# - Convert ICollection<Object> to another type ICollection<Person> using variable type of Type -
i try fill property type of icollection<person> or icollection<t> .i give objectlist type of list<object> or icollection<object> anyway can't set value property type of icollection<person> list of object
if (property.propertytype.isgenerictype && property.propertytype.getgenerictypedefinition() == typeof(icollection<>)) { type itemtype = property.propertytype.getgenericarguments()[0]; icollection<object> objectlist =getobjectlist(); property.setvalue(item, objectlist); } thanks.
you can't set icollection<person> icollection<object> since icollection isn't contravariant (there no in keyword in generic parameter declaration).
you explicitly have cast collection of object person
if (property.propertytype.isgenerictype && property.propertytype.getgenerictypedefinition() == typeof(icollection<>)) { type itemtype = property.propertytype.getgenericarguments()[0]; icollection<person> objectlist =getobjectlist().cast<icollection<person>>(); property.setvalue(item, objectlist); }
Comments
Post a Comment