EDIT: I found out that I can get it to compile if I cast the IMetadataType object to the TMetadata type. Why do I need to do this?
EDIT #2: The ‘Values’ property is a .NET dictionary of type <TMetadata, TData>.
I have this generic method:
private void FillMetadata<TMetadata, TData> (Metadata<TMetadata, TData> oMetadata) where TMetadata : IMetadataType { IMetadataType o; oMetadata.Values.Add(o, (TData)(object)GetValue()); }
I have stripped the implementation to simplify it (I actually use a real object, not the IMetadataType declared here).
My question is, why doesn’t this compile? The compile error is on the Add() method: ‘cannot convert from ‘IMetadataType’ to ‘TMetadata’.’ Isn’t that what the ‘where’ clause on the method is for?
What am I missing?
The
where TMetadata : IMetadataTypeis a constraint for the generic type parameterTMetadatasaying it should derive fromIMetadataType. SinceoMetadataknows onlyTMetadataandTDataas types to work with, you have to use them in your methods body. This should work: