Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 非专利药及;反射-泛型参数[0]违反类型的约束_C#_.net_Generics_Reflection_Factory Pattern - Fatal编程技术网

C# 非专利药及;反射-泛型参数[0]违反类型的约束

C# 非专利药及;反射-泛型参数[0]违反类型的约束,c#,.net,generics,reflection,factory-pattern,C#,.net,Generics,Reflection,Factory Pattern,关于这一点,我已经花了一段时间的时间,基本上我正在尝试实现一个通用存储库工厂,它被称为: var resposFactory = new RepositoryFactory<IRepository<Document>>(); public class DocumentLibraryRepository<T> : IRepository<T> where

关于这一点,我已经花了一段时间的时间,基本上我正在尝试实现一个通用存储库工厂,它被称为:

var resposFactory = new RepositoryFactory<IRepository<Document>>();
public class DocumentLibraryRepository<T> : IRepository<T>
                                                where T : class, new()
{
   public DocumentLibraryRepository(Guid listGuid, IEnumerable<IFieldToEntityPropertyMapper> fieldMappings)
   {
        ...
   }

   ...
}
我得到的错误是:

“Framework.Repositories.DocumentLibraryRepository`1[T]”上的ArgumentException-GenericArguments[0],“Framework.Repositories.IRepository`1[Apps.Documents.Entities.PerformalDocument]”违反了类型“T”的约束

你知道这里出了什么问题吗

编辑:

存储库的实现如下所示:

var resposFactory = new RepositoryFactory<IRepository<Document>>();
public class DocumentLibraryRepository<T> : IRepository<T>
                                                where T : class, new()
{
   public DocumentLibraryRepository(Guid listGuid, IEnumerable<IFieldToEntityPropertyMapper> fieldMappings)
   {
        ...
   }

   ...
}
公共类文档库存储库:IRepository 其中T:class,new() { 公共文档库存储库(Guid listGuid,IEnumerable fieldMappings) { ... } ... } IRepository看起来像:

public interface IRepository<T> where T : class
    {
        void Add(T entity);
        void Remove(T entity);
        void Update(T entity);
        T FindById(int entityId);
        IEnumerable<T> Find(string camlQuery);
        IEnumerable<T> All();
    }
公共接口i假设,其中T:class
{
无效添加(T实体);
无效删除(T实体);
无效更新(T实体);
T FindById(int entityId);
IEnumerable Find(字符串camlQuery);
i可数全部();
}

您的代码尝试创建
DocumentLibraryRepository
的实例,而不是
DocumentLibraryRepository

您想改为使用此代码:

var genericArgument = typeof(T).GetGenericArguments().FirstOrDefault();
if (tempType != null && genericArgument != null)
{
    Type newType = tempType.MakeGenericType(genericArgument);

这表明您可能对泛型类型
DocumentLibraryRepository
使用了
where
约束,并且类型
performalDocument
与该约束不匹配

也许我的答案可以帮助有相同错误的人。 我的设想是:

public class B
{
    public string Name;
}

public class A
{
    public EventHandler<B> TypedEvent;

    public void MyMethod(B item)
    {
        if (TypedEvent != null)
        {
            TypedEvent(null, item);
        }
    }
}

public class T
{
    public void Run()
    {
        A item = new A();
        item.TypedEvent += new EventHandler<B>(ItemEvent);
    }

    private void ItemEvent(object sender, B b)
    {
        b.Name = "Loaded";
    }
}

我希望这能帮助别人。

我也有同样的错误,但问题和解决方案不同我有4个模型类,1个基类,其中只有3个继承自基类,第4个没有。一旦最后一个类继承了基类,错误就消失了。

对不起,是的,我应该添加:公共接口IRepository,其中T:class{void Add(T entity);void Remove(T entity);void Update(T实体);T FindById(int entityId);IEnumerable Find(字符串camlQuery);IEnumerable All();}@Bevan:好的。请看我更新的答案。这应该可以解决您的问题。您是否缺少返回语句?您是否粘贴了该方法的完整副本?另外,当您显然打算调用带参数的构造函数时,为什么要检查是否存在无参数构造函数?如果您有无参数构造函数,这是最重要的可能是由
GetConstructors
返回的第0个构造函数,在这种情况下,使用参数调用它将失败。是的,很抱歉“return default(T)”应该在结尾。关于无参数构造函数的检查?你说的是对的,我只是想看看我是否做错了tbh。
public class B
{
    public string Name;
}

public class A
{
    public EventHandler TypedEvent;

    public void MyMethod(B item)
    {
        if (TypedEvent != null)
        {
            TypedEvent(item, null);
        }
    }
}

public class T
{
    public void Run()
    {
        A item = new A();
        item.TypedEvent += new EventHandler(ItemEvent);
    }

    private void ItemEvent(object sender, EventArgs e)
    {
        B b = sender as B;
        b.Name = "Loaded";
    }
}