locked
System.TypeLoadException: GenericArguments[0] [...] violates the constraints of type T RRS feed

  • Question

  • Hey everyone

    I`m getting this error and I don`t know how to resolve it.

    Well this is my class EntityBase:

    public class SPEntityBase
        {
            public int ID { get; set; }
    
            [SPEntityBaseField(Guid = "fa564e0f-0c70-4ab9-b863-0177e6ddd247", FieldType = SPFieldType.Text, InternalName = "Title")]
            public string Title { get; set; }
    
            [SPEntityBaseField(Guid = "1df5e554-ec7e-46a6-901d-d85a3881cb18", FieldType = SPFieldType.User, InternalName = "Author")]
            public SPFieldUserValue Author { get; set; }
    
            [SPEntityBaseField(Guid = "28cf69c5-fa48-462a-b5cd-27b6f9d2bd5f", FieldType = SPFieldType.DateTime, InternalName = "Modified")]
            public DateTime Modified { get; set; }
    
            [SPEntityBaseField(Guid = "8c06beca-0777-48f7-91c7-6da68bc07b69", FieldType = SPFieldType.DateTime, InternalName = "Created")]
            public DateTime Created { get; set; }
    
            [SPEntityBaseField(Guid = "d31655d1-1d5b-4511-95a1-7a09e9b75bf2", FieldType = SPFieldType.User, InternalName = "Editor")]
            public SPFieldUserValue ModifiedBy { get; set; }
        }

    this is my Repository class:

    public class SPRepository<T> where T : global::PI.Framework.SharePoint.DataMapper.SPEntityBase, new()
        { .... }


    So, i created a UnitTest Project on the same Project that i'm creating this Repositories libraries and everything is working just fine. But when i add this .DLL library on a second Project, and implements it like on UnitTests i got this error when i try to contstruct the EntityRepository that implements Repository<T> from my generic DLL.

    This is my class with EntityBase inherited

        [SPEntityClass(ListName = "Empreendimento", RelativeWebUrl = "/")]
        public class Empreendimento : SPEntityBase
        {
            [SPEntityBaseField(Guid = "{5bd4aad2-0c55-4b87-a223-aed50205a084}", FieldType = SPFieldType.User, InternalName = "GestorEContrapartesDoEmpreendimento")]
            public IList<Usuario> Gestores_Contrapartes { get; set; }
    
            [SPEntityBaseField(Guid = "{70e8f7ff-ea29-49df-a9d6-1d3db2b09640}", FieldType = SPFieldType.User, InternalName = "LeitoresDoEmpreendimento")]
            public IList<Usuario> Leitores { get; set; }
    
            [SPEntityBaseField(Guid = "{11134f3a-4f84-409a-a507-200b481bd024}", FieldType = SPFieldType.Text, InternalName = "NomeDoEmpreendimento")]
            public string Nome { get; set; }
        }

    and this is my "EmpreendimentoRepository" implemented

    public class EmpreendimentoRepository : SPRepository<Empreendimento>
        {
     public EmpreendimentoRepository(SPWeb web)
                : base(web)
            {
             
            }
        }

    So, when i try to construct "EmpreendimentoRepository(SPweb web)", it throws this exception

    Test Name: Can_Get_Empreendimentos
    Test FullName: MetroRegistroMudancas.Tests.EmpreendimentoRepositoryTest.Can_Get_Empreendimentos
    Test Source: c:\folder\projectname\MetroRegistroMudancas.Tests\EmpreendimentoRepositoryTest.cs : line 42
    Test Outcome: Failed
    Test Duration: 0:00:04,811

    Result Message: System.TypeLoadException : GenericArguments[0], 'MetroRegistroMudancas.UI.Entity.Empreendimento', on 'PI.Framework.SharePoint.SPRepository`1[T]' violates the constraint of type parameter 'T'.
    Result StackTrace: at MetroRegistroMudancas.Tests.EmpreendimentoRepositoryTest.Obtem_Lista_De_Empreendimentos()

    Any help would be appreciated!!

    Thanks in advance.


    Sunday, May 17, 2015 12:49 PM

Answers

  • Well your generic constraint : 

    where T : global::PI.Framework.SharePoint.DataMapper.SPEntityBase, new()

    From your class 

    SPRepository<T>

    States that you require any type derived from PI.Framework.SharePoint.DataMapper.SPEntityBase that supports a constructor with an empty signature.

    Your class EmpreendimentoRepository : SPRepository<Empreendimento> implement a constructor that takes one arqument 

    public EmpreendimentoRepository(SPWeb web)

    So the implicit empty constructor is not generated. 

    You should either change your generic constaint or explicitly define an empty constructor in EmpreendimentoRepository class.

    Monday, May 18, 2015 1:33 AM

All replies

  • Well your generic constraint : 

    where T : global::PI.Framework.SharePoint.DataMapper.SPEntityBase, new()

    From your class 

    SPRepository<T>

    States that you require any type derived from PI.Framework.SharePoint.DataMapper.SPEntityBase that supports a constructor with an empty signature.

    Your class EmpreendimentoRepository : SPRepository<Empreendimento> implement a constructor that takes one arqument 

    public EmpreendimentoRepository(SPWeb web)

    So the implicit empty constructor is not generated. 

    You should either change your generic constaint or explicitly define an empty constructor in EmpreendimentoRepository class.

    Monday, May 18, 2015 1:33 AM
  • Thanks David,

    But I don't think this was the problem. I created an empty Project to start all over again and now it's working. weird!

    I'm trying to figure it out what I did wrong in the old Project, creating class by class, testing and comparing both projects.

    Anyway, i'll keep this information that u said here in mind for futures references.

    Thanks again!!

    Cya!

    Monday, May 18, 2015 12:26 PM