c# - Cannot Convert Item to its interface exception -
i have following class , interface:
public interface ifieldfiller { string content { get; set; } boolean nullable { get; set; } string name { get; set; } }
and
[datacontract] public class fieldfiller : ifieldfiller { [datamember] public string content { get; set; } [datamember] public boolean nullable { get; set; } [datamember] public string name { get; set; } [datamember] public storedprocedure storedprocedure { get; set; } [datamember] public string namespace { get; set; } }
i use following linq statement build list of these objects:
fields = temp.merged_fields.select(f => new fieldfiller { name = f.name, namespace = f.@namespace, storedprocedure = new storedprocedure { name = f.sproc1.name, parameters = f.field_params.todictionary(p => p.sprocparam.name, p=>p.value)}}).tolist()
and keep getting following exception:
error 1 cannot implicitly convert type 'system.collections.generic.list<services.data.emailtemplateaccess.contracts.fieldfiller>' 'system.collections.generic.list<services.data.emailtemplateaccess.contracts.ifieldfiller>'
i don't understand why getting error when fieldfiller implements ifieldfiller. have verified in correct namespaces. appreciated.
fieldfiller
ifieldfiller
list<fieldfiller>
not list<ifieldfiller>
, can cast accordingly:
fields = temp.merged_fields .select(f => (ifieldfiller)new fieldfiller { name = f.name, namespace = f.@namespace, storedprocedure = new storedprocedure { name = f.sproc1.name, parameters = f.field_params.todictionary(p => p.sprocparam.name, p=>p.value)}}) .tolist()
or, .net 4 can use this(see: covariance , contravariance):
... .select(f => new fieldfiller { ... }) .tolist<ifieldfiller>();
why it's not safe:
Comments
Post a Comment