-2
我有一個抽象的泛型類是這樣的:使通用性質的非通用類
public abstract class ServicoAbstrato<TEntity, TId>
where TEntity : EntidadeBasica<TId>
where TId : IEquatable<TId>
{
protected IDao<TEntity, TId> dao; ...
}
public abstract class EntidadeBasica<TId>
where TId : IEquatable<TId>
{ ... }
,我想有這樣的泛型類的另一個類這樣的特性:
[AttributeUsage(AttributeTargets.Property | AttributeTargets .Field)]
public class ServicesAttribute : HandlerAttribute
{
public ServicoAbstrato<EntidadeBasica<Type>, Type> servico { get; set; }
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new ServicesHandler(servico);
}
}
但它給了我以下錯誤:
The type 'System.Type' cannot be used as type parameter 'TId' in the generic type or method 'Servico.ServicoAbstrato'. There is no implicit reference conversion from 'System.Type' to 'System.IEquatable'
我該如何解決這個問題?
很簡單,你有一個contstraint'TID:IEquatable'和'Type'不是'IEquatable ' –
Jonesopolis
就像在標記的副本中一樣,您試圖用作泛型類型參數的類型不會實現接口,您的泛型聲明需要實現的類型參數。 –