首页 > 解决方案 > Constraints are not allowed on non-generic declarations when define delegate

问题描述

I want to define delegate as the following:

public delegate void DataRequestingEventHandler(object sender, 
CommandEventArgs e) where Entity : IEntityId<EntityKey>, new() where 
EntityKey : IEntityKey, new();

But I am receiving this error:

Constraints are not allowed on non-generic declarations

Any ideas?

thanks

标签: asp.netentity-frameworkdelegates

解决方案


That's because Constraints are not allowed on non-generic declarations. So you need to add Generic Type Parameters to your declaration to make it a generic declaration.

Looks like you intended Entity and EntityKey to be generic type parameters, but failed to declare them. Also by convention generic type parameters start with T, eg:

public delegate void DataRequestingEventHandler<TEntity, TEntityKey>(object sender, CommandEventArgs e) 
    where TEntity : IEntityId<TEntityKey>, new() where TEntityKey : IEntityKey, new();

推荐阅读